-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Initializer not checked against declared type when annotating using self.x #7398
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
Comments
Thanks for reporting this! The initialization in class Foo:
def __init__(self):
self.bar: int = 'x' # No error even though this is clearly wrong! |
None
to a different type
Adding |
#7291 is also related (I think it was discussed whether we should just assume the |
Oops, #3948 bites again. |
Thanks for the quick response! Just for completeness sake: my assumption was, that I can do from dataclasses import dataclass, field
class Foo:
def __init__(self):
self.bar: int = None
def bob(self, a: int = None):
print(a)
@dataclass
class Foo2:
bar: int = None
bar2: int = field(default=None) |
Implicit optional may be allowed in some cases but it's deprecated. It's preferable to explicitly add Optional. |
Thanks for clarification! |
mypy
accepts instance variables that are instantiated asNone
even though the type indicates int. This is not the case for equivalentdataclasses
. I'm not sure if this is a bug or intended behavior.Here is a minimal example:
This is how I check the fiel:
mypy mypy_demo.py
.Here the resulting error msgs:
I did not find anything helpful in the dataclass section: https://mypy.readthedocs.io/en/latest/additional_features.html#dataclasses
I'm testing with Mypy version 0.701.
The text was updated successfully, but these errors were encountered: