-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Error with generic argument defaults #3130
Comments
In general, specifying a default argument for a parameter that is typed with a TypeVar presents a bunch of challenges for type safety. Mypy doesn't allow default arguments to be provided in this situation. Compilers in other typed languages like C# and Swift also disallow this. For more details, refer to this thread and this thread. Pyright handles the situation, but it makes it safe by setting the parameter to the union of the annotation (the TypeVar) and the type of the default argument. In your example, this means the type of I'm still not 100% convinced that this is the best approach. Perhaps pyright should simply give up and disallow this case entirely like other type checkers and compilers. A workaround I can suggest is the following: @overload
def instantiate_default() -> Derived: ...
@overload
def instantiate_default(cls: Type[BaseType]) -> BaseType: ...
def instantiate_default(cls = Derived) -> Base:
return cls() |
I see. Thanks for the explanation. |
… has a default argument value. Mypy doesn't allow this case. Pyright allows it but previously modified the parameter type to be a union of the TypeVar and the type of the default argument. This behavior closed some type validation holes, but it created other problems and was very unintuitive for users. See #3130. See #2507 for a discussion that led to this original change. This change reverses this behavior. It potentially allows for some unsafe "creative" uses that will go unreported.
Pyright reports an error when returning an instance from generic type arguments with default values.
I'm using version
1.1.225
with strict mode.The text was updated successfully, but these errors were encountered: