-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Incorrect "Type ... is already specialized" error #5449
Comments
Pyright is correct in generating this error, so this isn't a bug. The class A(Generic[T]):
@classmethod
def m(cls) -> Self:
return cls()
# The type of `cls` in this case is `A[int]`.
A[int].m()
# The type of `cls` in this case is `A[Unknown]`.
A.m() Even if If you would like to provide more details about what you're trying to do, I might be able to suggest an alternative approach. I'm going to close this issue since pyright is doing the right thing here. |
If this is not a bug, then I think we should move this to discussion instead as I do have some questions about the correct usage here. For example, in your code, the My current code has some heavy hacking to get the type info in the code (a class decorator to inject the types to a instance attribute), as my serialization / deserialization routines require the actual type to work properly, and I wanted to make the code as "pretty" as possible. |
Could you move this issue to discussion? |
Please start a new discussion topic and link to this bug report. |
Describe the bug
It seems
pyright
thinks thecls
in generic classmethod is specialized when it's not or has the type erased.To Reproduce
Consider the code:
Running pyright gives:
Even if you replace the last line with
Tuple[int].load(...)
, the load method prints()
, which means the type is already erased at this point. If you remove the[*types]
specialization, the resulting instance ofTuple
won't have__orig_class__
attribute, which should indicate the instance was not specialized when it was created.Expected behavior
pyright should consider the class parameter in the classmethod of generic classes as not specialized.
Code or Screenshots
See above.
VS Code extension or command-line
Command-line, 1.1.316
Additional context
Actually, I'm not sure if the code shown here is the correct usage of generic types. It works for my case though.
The text was updated successfully, but these errors were encountered: