-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Constructor call within generic class confuses type arguments #7507
Comments
I am not sure if this is related, but recently this started to raise "type is partially unknown" error: from typing import Generic, Self, TypeVar
T = TypeVar("T")
class Test(Generic[T]):
@classmethod
def make(cls, type_: type[T]) -> Self:
raise NotImplementedError()
t = Test.make(int) in previous versions
and
|
This isn't related. The behavior you're seeing here was due to a recent change for spec compliance. The current behavior is correct. Since you haven't specified a type argument for the |
Okay, but isn't the Do you know what is the current approach to specialize a classmethod returning |
If you want to use a classmethod in this manner, you would need explicitly specialize the class that you're binding to the class method. t = Test[int].make(int) Or if you're using the new PEP 696 TypeVar defaults, you can rely on the default value of the TypeVar. from typing import Generic, Self
from typing_extensions import TypeVar
T = TypeVar("T", default=int)
class Test(Generic[T]):
@classmethod
def make(cls, type_: type[T]) -> Self:
raise NotImplementedError()
t = Test.make(int) |
Okay, can you provide more details (any source to read more details) on |
We're quite off topic for this issue, but since you asked... This was previously an under-specified (and somewhat ambiguous) part of the typing spec, but the recent adoption of PEP 696 clarifies how this must work. The PEP 696 specification was recently incorporated into the official Python typing spec. The latest published version of mypy (1.9) doesn't have support for PEP 696, but the next version (1.10) will have partial (possibly complete) support. |
This is addressed in pyright 1.1.373. |
A regression introduced in 1.1.337 breaks the following:
This should have been caught by the constructor26.py unit test, but it was not.
This is related to #7369.
The text was updated successfully, but these errors were encountered: