Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax error in generated stub when using dataclass #15966

Closed
bramstroker opened this issue Aug 26, 2023 · 2 comments · Fixed by #15625
Closed

Syntax error in generated stub when using dataclass #15966

bramstroker opened this issue Aug 26, 2023 · 2 comments · Fixed by #15625
Labels
bug mypy got something wrong topic-stubgen

Comments

@bramstroker
Copy link

bramstroker commented Aug 26, 2023

Bug Report

Stubgen generates an output file with a syntax error.
It generates def __mypy-replace which is invalid python syntax because of the dash.

To Reproduce

With the following python code, generate a stub including the --include-private flag.

stubgen --include-private test.py

from dataclasses import dataclass

@dataclass
class MyDataClass:
    foo: str
    bar: int

Expected Behavior

The stub is generated without syntax errors.

Actual Behavior

A syntax error is raised when parsing the file:

  File "out/test.pyi", line 5
    def __mypy-replace(*, foo, bar) -> None: ...
              ^
SyntaxError: invalid syntax

When I generate the file without --include-private than the stub is generated correctly.

Your Environment

  • Mypy version used: 1.5.1
  • Python version used: 3.11.4

Extra info

Also see KapJI/homeassistant-stubs#349 where this issue was raised.
These stubs are used in custom Home Assistant components, and currently this is broken.

@bramstroker bramstroker added the bug mypy got something wrong label Aug 26, 2023
@JelleZijlstra
Copy link
Member

cf. #15689 which fixed a similar bug in stubtest

@hamdanal
Copy link
Collaborator

hamdanal commented Aug 26, 2023

cf. #15689 which fixed a similar bug in stubtest

This is also fixed by #15625 which is functional but still waiting on an answer on how to test a dataclass with a _: dataclasses.KW_ONLY field on pythons <3.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-stubgen
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants