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

When using dataclass_transform, __init__ does not know about attributes defined in TYPE_CHECKING #14853

Closed
jchalupka-pan opened this issue Mar 7, 2023 · 1 comment · Fixed by #14854
Labels
bug mypy got something wrong topic-dataclass-transform PEP 681

Comments

@jchalupka-pan
Copy link

Bug Report

When using typing.dataclass_transform (PEP 681) attributes defined in if TYPE_CHECKING are not treated as __init__ arguments.

On mypy==1.1.1 the same happens for all three ways of using dataclass_transform (code on playground is defining all of them).

On version mypy==1.0.0 only the Decorator function is having issues.

Pyright is reporting no issues with the code.

To Reproduce

Playground

Playground have mypy==1.0.0, so it only shows error for FunctionModel, but when running code from it on mypy==1.1.1 three errors are shown

Part of the code:

from typing import dataclass_transform, Type, TYPE_CHECKING, TypeVar


_T = TypeVar("_T")


@dataclass_transform()
def model(cls: Type[_T]) -> Type[_T]:
    cls.__init__ = lambda *_, **__: None  # type: ignore [method-assign]
    return cls


@model
class FunctionModel:
    string_: str
    if TYPE_CHECKING:
        int_: int
    else:
        int_: int


FunctionModel(string_='abc', int_=1)

Actual Behavior

test.py:22: error: Unexpected keyword argument "int_" for "FunctionModel"  [call-arg]
test.py:43: error: Unexpected keyword argument "int_" for "BaseClassModel"  [call-arg]
test.py:68: error: Unexpected keyword argument "int_" for "MetaClassModel"  [call-arg]

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
plugins = "sqlalchemy.ext.mypy.plugin"
show_error_codes = true
# Strict options
strict = true
warn_unused_configs = true
disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_decorators = true
no_implicit_optional = true
warn_redundant_casts = true
warn_unused_ignores = false  # overridden from strict
warn_return_any = true
no_implicit_reexport = true
strict_equality = true
# Additional options
disallow_untyped_globals = true
warn_no_return = true
warn_unreachable = true
  • Python version used: 3.11.1 (official release for Windows )
@jchalupka-pan jchalupka-pan added the bug mypy got something wrong label Mar 7, 2023
@KRunchPL
Copy link
Contributor

KRunchPL commented Mar 8, 2023

The issue seems to be around this line. The IfStmt in the class body is just skipped, because it is not AssignmentStmt.

I have drafted a quick, dirty fix, to look inside the IfStmt and the issue was fixed.

sobolevn pushed a commit that referenced this issue Mar 9, 2023
…in (#14854)

Fixes: #14853

Adding support for `if` statements in the `dataclass` and `dataclass_transform` decorators, so
the attributes defined conditionally are treated as those that are
directly in class body.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-dataclass-transform PEP 681
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants