Compatibility helpers for creating and inspecting TypedDict types.
Project description
create-inspect-typeddict
Compatibility helpers for creating and inspecting TypedDict types.
Example
from typing import List
from create_inspect_typeddict import (
create_typeddict,
get_hints,
get_required_and_optional_keys,
)
Item = create_typeddict(
"Item",
{
"name": str,
"tags": List[str],
"description": str,
},
optional_keys=["description"],
)
print(get_hints(Item))
required_keys, optional_keys = get_required_and_optional_keys(Item)
print(sorted(required_keys))
print(sorted(optional_keys))
Expected result:
nameandtagsare requireddescriptionis optional
Main Functions
is_typeddict(typ)
Returns True when typ appears to be a TypedDict type.
It prefers typing_extensions.is_typeddict() when available and otherwise falls back to structural checks.
create_typeddict(name, annotations, optional_keys=None)
Creates a TypedDict class dynamically.
Arguments:
name: class nameannotations: field name to type mappingoptional_keys: iterable of keys that should be optional
Behavior:
- validates that every optional key exists in
annotations - creates the
TypedDict - populates
__required_keys__and__optional_keys__
get_hints(typ)
Returns a dictionary of resolved type hints for typ.
It tries:
typing_extensions.get_type_hints()typing.get_type_hints()- raw
__annotations__
This is useful when consuming TypedDict definitions that may contain forward references or compatibility-layer typing objects.
get_required_and_optional_keys(typ)
Returns a (required_keys, optional_keys) tuple of frozenset values for a TypedDict.
Resolution order:
__required_keys__/__optional_keys____total__
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file create_inspect_typeddict-0.1.0a0.tar.gz.
File metadata
- Download URL: create_inspect_typeddict-0.1.0a0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1cf48e72cb8f2d9d1c7f544807e9a01acfcc742446463815fe7a5cb58f6c6af
|
|
| MD5 |
b6e34fdf7a79e67f2a654df730799fa1
|
|
| BLAKE2b-256 |
6da122756305eb27ee0fc32d3391461c77478c85b6c516b984c70ba5df4c251b
|
File details
Details for the file create_inspect_typeddict-0.1.0a0-py2.py3-none-any.whl.
File metadata
- Download URL: create_inspect_typeddict-0.1.0a0-py2.py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b8405750456f956abe80149865a4f4c6a974ffb999a29d9227d92efb9c7faa5
|
|
| MD5 |
f99f8ecfa79793420b2938780f8609c3
|
|
| BLAKE2b-256 |
be5d860d9d936b955de8f0e9a65b2553d7b03e0bc99813bc0b4ccd3d0c718fd8
|