You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wondering what is going on here for the cases 3-6. Running it with --strict, mypy version is 0.761.
importdataclassesfromdataclassesimportdataclass, InitVarfromfunctoolsimportpartialdataclass_alias=dataclassdataclass_partial=partial(dataclass, frozen=True)
dataclass_decorator=dataclass(frozen=True)
# 1. ok@dataclass(frozen=True)classX1:
value: InitVar[bool] =None# 2. ok@dataclasses.dataclass(frozen=True)classX2:
default: InitVar[bool] =None# 3. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")@dataclass_alias(frozen=True)classX3:
default: InitVar[bool] =None# 4. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")@dataclass_partialclassX4:
default: InitVar[bool] =None# 5a. error: Too many arguments for "object"# 5b. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")@dataclass_partial()classX5:
default: InitVar[bool] =None# 6. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")@dataclass_decoratorclassX6:
default: InitVar[bool] =None
The text was updated successfully, but these errors were encountered:
Something weird is going on. First, the alias doesn't seem to be recognized correctly, even though it might be easy enough to support. Second, the type of the InitVar initializer doesn't seem to be checked: None is not a valid initializer for attribute with type bool.
Cases 4-6 likely won't be supported -- they are too dynamic for mypy. It would be nice to generate more informative errors for them, however.
Hints for anybody who's interested in working on this: you can find relevant code in mypy.plugins.dataclasses and mypy.semanal ( apply_class_plugin_hooks). I'd recommend fixing one issue per PR, instead of trying to fix all the cases at once.
I want to work on this, So far what I have understood is I need to add a hook for checking the instance variable's datatype to default value's datatype, and if its okay then I need to modify the mypy.plugins.dataclassesfile for checking the datatype of value passed to the variable while initializing the class object. Is it Correct? @JukkaL
While the example is incorrect, even using the correct type with current version raise an error:
_filename: InitVar[str | None] = None
error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[Optional[str]]") [assignment]
Wondering what is going on here for the cases 3-6. Running it with
--strict
, mypy version is 0.761.The text was updated successfully, but these errors were encountered: