Skip to content

Attrs plugin mishandles typing.Self in generated inits #14685

Open
@FasterSpeeding

Description

@FasterSpeeding

Bug Report

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

To Reproduce

import attrs
import typing

from typing_extensions import Self


@attrs.define()
class Dataclass:
    child: typing.Optional[Self] = None


Dataclass(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

import typing

import dataclasses
from typing_extensions import Self


@dataclasses.dataclass
class Dataclass:
    child: typing.Optional[Self] = None


Dataclass(child=Dataclass())
import typing

from typing_extensions import Self


class Dataclass:
    def __init__(self, child: typing.Optional[Self] = None) -> None:
        self.child = child
    

Dataclass(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" has incompatible type "Dataclass"; expected "Optional[Self]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

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