Skip to content

Attrs plugin mishandles typing.Self in generated inits #14685

Description

@FasterSpeeding

Bug Report

MyPy mishandles type comparisons when an argument in an attrs generated init uses typing.Self.

To Reproduce

importattrsimporttypingfromtyping_extensionsimportSelf@attrs.define()classDataclass:
child: typing.Optional[Self] =NoneDataclass(child=Dataclass())

Expected Behavior

The attrs version of this code should pass type checking like MyPy does with the following dataclasses and normal init examples

importtypingimportdataclassesfromtyping_extensionsimportSelf@dataclasses.dataclassclassDataclass:
child: typing.Optional[Self] =NoneDataclass(child=Dataclass())
importtypingfromtyping_extensionsimportSelfclassDataclass:
def__init__(self, child: typing.Optional[Self] =None) ->None:
self.child=childDataclass(child=Dataclass())

Actual Behavior

MyPy false-positive reports an incompatible type for the argument only when using attrs

test.py:12: error: Argument"child"to"Dataclass"hasincompatibletype"Dataclass"; expected"Optional[Self]" [arg-type]
Found1errorin1file (checked1sourcefile)

Your Environment

  • Mypy version used: 1.0.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.10.9
  • Attrs version used: 22.2.0

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions