Support for NewType, LiteralString, NamedTuple, TypedDict ReadOnly keys and subscripted generic type aliases - #967
Merged
Conversation
…ys and subscripted generic type aliases
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #967 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 28 28
Lines 9250 9374 +124
==========================================
+ Hits 9250 9374 +124 ☔ 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?
Adds support for several typing constructs that were either unsupported or silently unvalidated, and fixes a few related problems found along the way.
Added
NamedTupleas a type. The value is either an object with the fields as keys or an array of positional values, fields with a default can be omitted, and parsing gives an instance of the named tuple. It is always dumped as an object, so that the fields are named. A--*.helpoption shows the accepted fields,add_class_argumentsaccepts aNamedTuple, and the fields and their values are shell completed, all the same as for aTypedDict. A generic named tuple works both unsubscripted and subscripted, e.g.SomeTuple[int], and a field of an untypedcollections.namedtupleaccepts any value.ReadOnly(PEP 705) forTypedDictkeys. It only marks a key as not mutable, so it changes neither the type of a key nor its requiredness, and it can wrap or be wrapped byRequired/NotRequired.NewTypeandLiteralStringas types. Previously they were not validated, i.e. any value was accepted. Now aNewTypeis validated as the supertype it stands for, including aNewTypeof aNewType, and aLiteralStringas astr, in both cases the help showing the name as written in the source code.Fixed
A subscripted generic
TypeAliasType, e.g.Alias[int]fortype Alias[T] = list[T], raisedUnsupported type hint. Now it is parsed as its target with the type parameters substituted, and unsubscripted its type parameters stand for their default, constraints or bound, the same as any otherTypeVar.Namespace.as_dictdid not convert the namespaces nested in adictorlistthat also holds values which are not namespaces, e.g. aTypedDictwith one key of a class type and another of a simple type. Dumping such a config as json failed withObject of type Namespace is not JSON serializable.add_class_argumentsgiven a subscripted generic class, e.g.SomeClass[int], did not instantiate it, giving aNamespaceinstead of an instance.Notes
TypedDictandNamedTupleare now handled together as "structured value types", i.e. structures of named keys or fields whose value is not a class to instantiate, so it is never given as a class path and--*.helprefers to them by name. This unifies the--*.helpaction, the shell completions and the jsonschema completions, where aNamedTupleis described as an object of fields or an array of values.Documentation and changelog updated, tests added for all of the above.
Before submitting