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

Strange behavior with dataclass InitVar #8439

Open
gsakkis opened this issue Feb 25, 2020 · 3 comments
Open

Strange behavior with dataclass InitVar #8439

gsakkis opened this issue Feb 25, 2020 · 3 comments

Comments

@gsakkis
Copy link

gsakkis commented Feb 25, 2020

Wondering what is going on here for the cases 3-6. Running it with --strict, mypy version is 0.761.

import dataclasses
from dataclasses import dataclass, InitVar
from functools import partial

dataclass_alias = dataclass
dataclass_partial = partial(dataclass, frozen=True)
dataclass_decorator = dataclass(frozen=True)

# 1. ok
@dataclass(frozen=True)
class X1:
    value: InitVar[bool] = None


# 2. ok
@dataclasses.dataclass(frozen=True)
class X2:
    default: InitVar[bool] = None


# 3. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")
@dataclass_alias(frozen=True)
class X3:
    default: InitVar[bool] = None


# 4. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")
@dataclass_partial
class X4:
    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()
class X5:
    default: InitVar[bool] = None


# 6. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")
@dataclass_decorator
class X6:
    default: InitVar[bool] = None
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 28, 2020

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.

@pr4k
Copy link

pr4k commented Mar 1, 2020

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

@ssbarnea
Copy link
Sponsor Contributor

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]

No idea how to avoid this.

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