-
Notifications
You must be signed in to change notification settings - Fork 290
Open
Labels
topic: typing specFor improving the typing specFor improving the typing spec
Description
I want to suggest adding the following test to the conformance test suite:
class Getter[T]:
def get[S=None](self, default: S = None) -> T | S: ... # OK
def test(arg: Getter[str]) -> None:
result: str = arg.get() # type: ignore- Since the default argument value is assignable to the default type var type,
def getis well-defined arg.get()implicitly usesNoneof typeNoneas the argument. Therefore, we have an implicit constraintS <: Nonewhen.getis called without explicit default.
Therefore, result: str = arg.get() should raise an assignment type error (str | None not assignalbe to str).
Currently, only pyright succeeds on this test, and type checker results diverge.
pyright: no errors (true negative ondef get, true positive onresult) (pyright playground)mypy: false positive ondef get, false negative onresult.ty: false positive ondef get, true positive onresult. (playground)pyrefly: false positive ondef get, false negative onresult(playground)zuban: false postive ondef get, false negative onresult.
Related Issues
- Unable to specify a default value for a generic parameter mypy#3737 (one of the most upvotes issues on mypy)
invalid-parameter-defaulterror from generic with default ofNoneastral-sh/ty#592
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
topic: typing specFor improving the typing specFor improving the typing spec