Skip to content

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

Closed
@jchalupka-pan

Description

@jchalupka-pan

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 )

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions