Skip to content

False positive undefined-variable for self-referential return annotation in a PEP 695 generic class #11211

Description

@sam-s

Bug description

Pylint reports E0602: Undefined variable 'uniqdict' when a method in a PEP 695 generic class uses the enclosing class in its return annotation.

"https://github.com/pylint-dev/pylint/issues/11211"
class uniqdict[K, V](dict[K, V]):
    def __ior__(self, other, /) -> uniqdict[K, V]:
        return self

Command used

pylint --rcfile=/dev/null --disable=all --enable=undefined-variable --py-version=3.12 pylint.py

Pylint output

pylint.py:3:35: E0602: Undefined variable 'uniqdict' (undefined-variable)

Expected behavior

no diagnostics

Pylint version

pylint 4.0.6
astroid 4.1.2
Python 3.14.6

OS / Environment

MSYS_NT-10.0-26100

Larger real-world example

import collections.abc

class uniqdict[K, V](dict[K, V]):
    """A mapping whose existing keys cannot be assigned different values."""

    def __setitem__(self, key: K, value: V) -> None:
        try:
            if (old := super().__getitem__(key)) != value:
                raise ValueError(self.__class__.__name__, key, old, value)
        except KeyError:
            super().__setitem__(key, value)

    update = collections.abc.MutableMapping.update

    def __ior__(self, other, /) -> uniqdict[K, V]:
        self.update(other)
        return self

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs triage 📥Just created, needs acknowledgment, triage, and proper labelling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions