generics: specify TypeVar-default inference when parameter default matches#2313
Open
ashishpatel26 wants to merge 1 commit into
Open
generics: specify TypeVar-default inference when parameter default matches#2313ashishpatel26 wants to merge 1 commit into
ashishpatel26 wants to merge 1 commit into
Conversation
…tches
The spec's "Function Defaults" section previously said generic function
TypeVar defaults are "unspecified" in all cases. However, there is a
concrete, well-defined case that can and should be specified: when a
TypeVar S with default D is used as the type of exactly one parameter p,
and p's default argument value is assignable to D, calling the function
without p must be type-checked as if p's default value were passed
explicitly. Type checkers must therefore infer S = D.
Additionally, the function definition itself is valid in this case:
the default argument is checked for assignability against D (the TypeVar's
default type), not against the free TypeVar S.
Spec change: docs/spec/generics.rst — "Function Defaults" section
Conformance test: new section at end of generics_defaults.py using a
Getter[T] class whose .get() method has S=None default.
Checker results:
pyright, pyrefly, pycroscope — pass all new assertions
mypy, zuban — incorrectly reject the function definition (Partial)
ty — incorrectly rejects the function definition; correctly infers
the return types (Partial)
Fixes: python#2213
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2213.
The spec's "Function Defaults" section previously left all generic-function TypeVar default semantics unspecified. However, there is a concrete, well-defined case that can and should be specified:
Additionally, the function definition is valid in this case: the default argument is checked against
D(the TypeVar's default), not against the free TypeVarSitself.This is already the behavior of pyright (and pyrefly, pycroscope) — the spec is simply catching up.
Changes
docs/spec/generics.rst— "Function Defaults" section: keep the general caveat but carve out and specify this concrete case with an exampleconformance/tests/generics_defaults.py— new test section with aGetter[T]class whose.get()hasS=NonedefaultPartialPartialChecker behaviour at a glance
def get(..., default: S = None)getter.get()→str | NoneTest plan
conformance/src/main.py --report-onlyproduces a cleanresults.htmlvalidate_results.pypasses (Fail results haveconformantfield)