Skip to content

PEP 695 TypeVar redefinition false-positive #10995

@kdkavanagh

Description

@kdkavanagh

Bug description

# PEP 695 type alias — `T` here is scoped to this statement only.
# Per PEP 695: "the type parameters are only visible within the scope
# of the statement."
type Alias[T] = T | list[T]


# This function's `T` is a different, locally-scoped type parameter.
# Python itself has no issue — this is valid, well-scoped code.
# pylint incorrectly warns:
#   W0621: Redefining name 'T' from outer scope (line 42)
def identity[T](x: T) -> T:
    return x


# Same issue with @overload — each overload introduces its own `T`.
@overload
def process(x: str) -> str: ...
@overload
def process[T](x: list[T]) -> T: ...  # W0621 (false positive)
def process[T](x: str | list[T]) -> str | T:  # W0621 (false positive)
    if isinstance(x, list):
        return x[0]
    return x

Configuration

Command used

pylint --disable=all --enable=W0621

Pylint output

pylint_false_positive_reproducer.py:50:13: W0621: Redefining name 'T' from outer scope (line 43) (redefined-outer-name)
pylint_false_positive_reproducer.py:58:12: W0621: Redefining name 'T' from outer scope (line 43) (redefined-outer-name)
pylint_false_positive_reproducer.py:59:12: W0621: Redefining name 'T' from outer scope (line 43) (redefined-outer-name)

Expected behavior

No error thrown

Pylint version

pylint 4.0.5
astroid 4.0.4
Python 3.12.6 (main, Oct  9 2024, 11:19:43) [GCC 13.2.0]

OS / Environment

No response

Additional dependencies

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