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

Pyright does not infer generic class parameter when specified as argument to class method #9190

Closed
superlopuh opened this issue Oct 10, 2024 · 2 comments
Labels
as designed Not a bug, working as intended bug Something isn't working

Comments

@superlopuh
Copy link

Here's a simple example, T is completely specified, yet Pyright complains:

from typing import Generic, TypeVar

T = TypeVar("T")

class GenericClass(Generic[T]):

    @classmethod
    def method(cls, param: T) -> T:
        return param

val = GenericClass.method(1)

Pyright errors:

pyright xdsl/dialects/bla.py
12:1 - error: Type of "val" is unknown (reportUnknownVariableType)
12:7 - error: Type of "method" is partially unknown
    Type of "method" is "(param: Unknown) -> Unknown" (reportUnknownMemberType)
2 errors, 0 warnings, 0 informations 

With pyright 1.1.384

@erictraut
Copy link
Collaborator

Pyright's behavior here is correct, so this isn't a bug.

You've defined a class-scoped type variable here, so the type argument is associated with the class (GenericClass), not with the method (method). Any class-scoped type variables that appear within a method's signature are specialized at the time you access the method through the class. In this case, the class is GenericClass[Unknown] because you haven't provided an explicit type argument. If you had provided a default value for the type variable (as per PEP 696), then GenericClass would be specialized with that default value. Since there was no default specified, the default is implicitly Unknown. That means the type of GenericClass.method is (param: Unknown) -> Unknown. There is no type variable to solve in this case, so it doesn't matter what arguments you pass.

There's only one case specified in the typing spec where class-scoped type variables can be inferred when calling a method, and that's in the case of a constructor call. This special case doesn't apply to class methods.

@erictraut erictraut closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2024
@erictraut erictraut added bug Something isn't working as designed Not a bug, working as intended labels Oct 10, 2024
@superlopuh
Copy link
Author

Thank you for the detailed response!

Julian added a commit to python-jsonschema/referencing that referenced this issue Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants