Skip to content

Support for NewType, LiteralString, NamedTuple, TypedDict ReadOnly keys and subscripted generic type aliases - #967

Merged
mauvilsa merged 1 commit into
mainfrom
namedtuple-readonly-generic-aliases-newtype
Aug 31, 2026
Merged

Support for NewType, LiteralString, NamedTuple, TypedDict ReadOnly keys and subscripted generic type aliases#967
mauvilsa merged 1 commit into
mainfrom
namedtuple-readonly-generic-aliases-newtype

Conversation

@mauvilsa

Copy link
Copy Markdown
Owner

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

  • NamedTuple as 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 --*.help option shows the accepted fields, add_class_arguments accepts a NamedTuple, and the fields and their values are shell completed, all the same as for a TypedDict. A generic named tuple works both unsubscripted and subscripted, e.g. SomeTuple[int], and a field of an untyped collections.namedtuple accepts any value.

  • ReadOnly (PEP 705) for TypedDict keys. 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 by Required/NotRequired.

  • NewType and LiteralString as types. Previously they were not validated, i.e. any value was accepted. Now a NewType is validated as the supertype it stands for, including a NewType of a NewType, and a LiteralString as a str, in both cases the help showing the name as written in the source code.

Fixed

  • A subscripted generic TypeAliasType, e.g. Alias[int] for type Alias[T] = list[T], raised Unsupported 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 other TypeVar.

  • Namespace.as_dict did not convert the namespaces nested in a dict or list that also holds values which are not namespaces, e.g. a TypedDict with one key of a class type and another of a simple type. Dumping such a config as json failed with Object of type Namespace is not JSON serializable.

  • add_class_arguments given a subscripted generic class, e.g. SomeClass[int], did not instantiate it, giving a Namespace instead of an instance.

Notes

  • TypedDict and NamedTuple are 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 --*.help refers to them by name. This unifies the --*.help action, the shell completions and the jsonschema completions, where a NamedTuple is described as an object of fields or an array of values.

  • Documentation and changelog updated, tests added for all of the above.

Before submitting

  • Did you read the contributing guideline?
  • If you used a coding agent, did you fully understand and validate all generated code and ensure it follows the contributing guidelines?
  • Did you update the documentation? (readme and public docstrings)
  • Did you write unit tests such that there is 100% coverage on related code? (required for bug fixes and new features)
  • Did you verify that new and existing tests pass locally?
  • If this is a bug fix, did you verify that the tests fail without the code fix?
  • Did you make sure that all changes preserve backward compatibility?
  • Did you update the CHANGELOG including a pull request link? (not for typos, docs, test updates, or minor internal changes/refactors)

@mauvilsa mauvilsa added bug Something isn't working enhancement New feature or request labels Aug 31, 2026
@mauvilsa
mauvilsa deployed to sonarcloud August 31, 2026 05:30 — with GitHub Actions Active
@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (c9e025b) to head (7150d62).
✅ All tests successful. No failed tests found.

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.
📢 Have feedback on the report? Share it here.

@sonarqubecloud

Copy link
Copy Markdown

@mauvilsa
mauvilsa merged commit e7b7989 into main Aug 31, 2026
32 checks passed
@mauvilsa
mauvilsa deleted the namedtuple-readonly-generic-aliases-newtype branch August 31, 2026 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant