Skip to content

feat: Implement NewTypedDict[*Ps: Member] type operator#23

Merged
AliiiBenn merged 4 commits intomainfrom
feature/newtypedict
Mar 5, 2026
Merged

feat: Implement NewTypedDict[*Ps: Member] type operator#23
AliiiBenn merged 4 commits intomainfrom
feature/newtypedict

Conversation

@AliiiBenn
Copy link
Member

Summary

Implements the NewTypedDict[*Ps: Member] type operator as specified in PEP 827 for creating TypedDict types dynamically from Member type arguments.

Changes

  • New class (typing.py): Added NewTypedDict class definition with docstring and usage examples
  • Evaluator (_eval_operators.py): Implemented _eval_NewTypedDict function that:
    • Extracts member information (name, type, qualifiers) from Member types
    • Handles NotRequired qualifier for optional fields
    • Creates a TypedDict with the appropriate annotations
    • Supports dynamic naming from the type alias context
  • Tests (test_type_eval.py): Added 7 comprehensive test cases

Usage Examples

from typemap_extensions import NewTypedDict, Member
from typing import Literal
from typemap import eval_typing

# Create a TypedDict with multiple fields
result = eval_typing(
    NewTypedDict[
        Member[Literal[\"name\"], str],
        Member[Literal[\"age\"], int],
    ]
)
# result is a TypedDict with name: str, age: int

# With optional field (NotRequired)
result = eval_typing(
    NewTypedDict[
        Member[Literal[\"required_field\"], str],
        Member[Literal[\"optional_field\"], int, Literal[\"NotRequired\"]],
    ]
)

Test Cases

  1. test_newtypeddict_basic - Basic TypedDict creation
  2. test_newtypeddict_single_field - Single field creation
  3. test_newtypeddict_preserves_types - Type preservation
  4. test_newtypeddict_with_complex_types - Complex types (list, dict)
  5. test_newtypeddict_multiple_fields - Multiple fields
  6. test_newtypeddict_optional_field - NotRequired qualifier handling
  7. test_newtypeddict_with_iter_attrs - Integration with Iter/Attrs

Related Issue

Closes #18


🤖 Generated with Claude Code

AliiiBenn and others added 4 commits March 5, 2026 14:45
Add the NewTypedDict type operator as specified in PEP 827 for
creating TypedDict types from Member type arguments.

Changes:
- Add NewTypedDict class definition to typing.py
- Add evaluator in _eval_operators.py
- Add comprehensive tests (7 test cases)
- Handle NotRequired qualifier for optional fields

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use typing.cast to make mypy understand that TypedDict is callable
when creating a NewTypedDict type.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Handle ReadOnly qualifier in addition to NotRequired
- Support multiple qualifiers (ReadOnly + NotRequired)
- Add tests for ReadOnly functionality

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@AliiiBenn AliiiBenn merged commit a036991 into main Mar 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Implement NewTypedDict[*Ps: Member]

1 participant