Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Sachaa-Thanasius opened this issue Mar 20, 2024 · 2 comments
Closed
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@Sachaa-Thanasius
Copy link

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

@Sachaa-Thanasius Sachaa-Thanasius added the bug Something isn't working label Mar 20, 2024
erictraut added a commit that referenced this issue Mar 20, 2024
…TypeVar with a default that references another class-scoped TypeVar is used in a method signature in that class. This addresses #7525.
@erictraut
Copy link
Collaborator

erictraut commented Mar 20, 2024

Thanks for the bug report. This will be addressed in the next release.

I'm delighted to see that you're trying out the new functionality introduced in PEP 696!

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Mar 20, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.356, which I just published.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants