-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Unable to infer correct type for generic TypeVars using TypeVar defaults (PEP 696) #16233
Comments
Mypy doesn't fully support PEP 696 yet. Closing as a duplicate of #14851. |
@insilications, as noted in #14851, PEP 696 is still in draft form and has not yet been accepted by the steering council. That means this feature could change or be removed in the future. If you want to play around with the functionality and provide feedback on the draft PEP, that's great, but I caution against using this functionality in production code until after the PEP is approved in final form. |
In your view, is this a safe pattern, using @overload, in order to avoid using PEP 696?
|
Yes, that looks fine to me and works with pyright. However, mypy doesn't yet support the unpack syntax that you're using in this example. You'd need to use Here's a variant that works with mypy: class MyClass(Generic[T1, T2]):
@overload
def __init__(self: MyClass[None, None]) -> None:
pass
@overload
def __init__(self: MyClass[T1, None], type1: type[T1]) -> None:
pass
@overload
def __init__(self: MyClass[T1, T2], type1: type[T1], type2: type[T2]) -> None:
pass
def __init__(
self: MyClass[T1, T2],
type1: type[T1] | None = None,
type2: type[T2] | None = None,
) -> None:
pass |
Bug Report
Pylance is able to infer the correct type for MyClass even in the absence of explicit typing of all its generic TypeVars when using TypeVar defaults. I don't know if this is the intended behavior and who is implementing PEP 696 (https://peps.python.org/pep-0696/) correctly or fully.
To Reproduce
Playground URL: https://mypy-play.net/?mypy=master&python=3.11&gist=f685e0af58eaceb6506ce129da4392c3
Expected Behavior
Maybe match how Pylance is implementing PEP 696 – Type defaults for TypeVarLikes
Actual Behavior
Your Environment
The text was updated successfully, but these errors were encountered: