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

stubgen with attrs class not producing expected __init__.py output #9327

Closed
cazador481 opened this issue Aug 19, 2020 · 2 comments
Closed

stubgen with attrs class not producing expected __init__.py output #9327

cazador481 opened this issue Aug 19, 2020 · 2 comments

Comments

@cazador481
Copy link

When I generate stubs using mypy's stubgen I find that I am forced to use the attrib = attr.ib(...) to get the stub for init to have the attributes in them. Even then the typing information for the attributes are wrong.

When I do

@attr.s(auto_attribs = True)
class Dummy:
  var: str

and run stubgen against it.
The stub I get looks like

def __init__(self)->None

I expect

def __init__(self, var: str)->None

If I do

@attr.s()
class Dummy:
  var: str = attr.ib()
  var2 = attr.ib(type:str)

I get closer to the expected output, but it is missing the type information

def __init__(self, var: Any, var2: Any)->None
@ilevkivskyi
Copy link
Member

Hm, this looks like a bug. But maybe a better behavior would be to preserve the class decorator completely, since attrs are treated in quite special way by mypy.

@cdce8p
Copy link
Collaborator

cdce8p commented Jan 8, 2025

This was fixed in #18418 (Included in the next release.) and will be further improved by #18430.

Note that you cannot use the shorthand since mypy doesn't resolve it. It will work with attr.attrs / attrs.define and attr.attrib / attrs.field.

@cdce8p cdce8p closed this as completed Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants