Handle object as Any, and support subscripted generic protocols and TypedDicts - #958
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #958 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 8600 8671 +71
=========================================
+ Hits 8600 8671 +71 ☔ View full report in Codecov by Harness. |
|
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.

What does this PR do?
Extends the type hint support for generics and unifies the handling of
objectwith
Any.Added
TypedDict, e.g.SomeDict[int], and for aTypedDictthat inherits from one, e.g.class SubDict(SomeDict[int]), both previously unsupported. Subscripting doesn't change which keys are accepted, only the types of the keys annotated with aTypeVar, composing the substitutions along the inheritance chain. Works for parsing, the--*.helpoption and shtab completions.TypeVardefault, constraint or bound given as a forward reference is now resolved with the names of the module in which theTypeVaris defined. An unresolvable reference becomes anUnvalidated<...>instead of failing.TypedDictwhose type can't be validated now accept any value, the same as signature parameters already did.Fixed
Protocol, e.g.Proto[int], never being implementable. The type arguments are now substituted into the protocol's method signatures and return types, soProto[int]andProto[str]accept different implementations, and aTypeVarthat remains matches any type, as static type checkers do. This also applies to instance factory protocols, i.e. the return type of a subscripted protocol's__call__.RuntimeErrorwhen giving a value for aTypedDictkey annotated with aTypeVar.type[Any]rejecting every value, instead of accepting any class as baretypedoes.init_args, e.g. a subclass spec kept as is for anAnytyped parameter, being expanded into keyword arguments oninstantiate.Changed
objectas a type is now handled exactly likeAny, since in standard typing every value is an instance of it. This means it no longer only accepts the import path of a class, it accepts any value, it participates in thevalidate_subclass_spec_in_anyandinstantiate_subclass_spec_in_anysettings, it is sorted last in unions together withAnyandUnvalidated<...>, and dumping it warns about values that lose their type.Before submitting