Skip to content

Potential scoping issue when using TypeVar defaults in a class #7525

@Sachaa-Thanasius

Description

@Sachaa-Thanasius

Describe the bug
Assume there is a TypeVar A that depends on another TypeVar B for its default, and that there is a class generic over A and B. Any method using B but not A for annotations will show an error saying that the default type refers to a type parameter not in scope. I would've guessed that in a class context, A would be in scope, even if it isn't being used at the function level for a particular method.

Code or Screenshots

from typing import  Generic, Self, TypeVar, overload

A = TypeVar("A", default=int)
B = TypeVar("B", default=A)

class Test(Generic[A, B]):
    @property
    def start(self) -> A: ...
    @property
    def stop(self) -> B: ...  # error
    # Type parameter "B" has a default type that refers to one or more type variables that are out of scope
    # Type variable "A" is not in scope

    @overload
    def __new__(cls, stop: B, /) -> Self: ... # error
    # Type parameter "B" has a default type that refers to one or more type variables that are out of scope
    # Type variable "A" is not in scope
    @overload
    def __new__(cls, start: A, stop: B, /) -> Self: ...

VS Code extension or command-line
command-line: pyright 1.1.355

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions