-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Enhance stubgen
to include types for @dataclass
private methods
#9986
Comments
At the moment, With the current master branch, if I generate stubs for this dataclass: from dataclasses import dataclass
from typing import Optional
@dataclass
class Data:
integer: int
string: str
optional: Optional[int] = None It outputs: from typing import Optional
class Data:
integer: int
string: str
optional: Optional[int]
def __init__(self, integer, string, optional) -> None: ... The issues are:
At least the first issue (maybe the second too) is caused by the fact that The generated methods still provide "analyzed" type information and I tried naively patching stubgen to use that, which resulted in: from typing import Optional
class Data:
integer: int
string: str
optional: Optional[int]
def __init__(self, integer: builtins.int, string: builtins.str, optional: Union[builtins.int, None]) -> None: ... Now there are type annotations and they are also correct, but the right imports ( Maybe a more feasible approach would be to simply leave the from dataclasses import dataclass
from typing import Optional
@dataclass
class Data:
integer: int
string: str
optional: Optional[int] = ... ... then everything should work nicely, no? To get there, the following needs to change:
|
Thanks for the analysis! I also think leaving |
That approach works for me too, seems like a good separation of responsibilities. |
I tested following code with Mypy 0.961. The issues @teskje said still persists. # test.py
from dataclasses import dataclass
from typing import Optional
@dataclass
class Data:
required: int
optional: Optional[int] = None # test.pyi generated via stubgen
from typing import Optional
class Data:
required: int
optional: Optional[int]
def __init__(self, required, optional) -> None: ... By the way, why there is no |
Feature
Enhance
stubgen
to include types for@dataclass
private methods.Pitch
Was using interface files to help communicate between teams and noticed this (perhaps a low priority bug, but feels like a feature).
For example, currently
generates the following when passed to
stubgen
I would hope we could generate
I believe there is already logic in the dataclass plugin to implement some of these checks, but if they are they are not propagated to
stubgen
.Metadata
The text was updated successfully, but these errors were encountered: