Skip to main content

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:

  • name and tags are required
  • description is 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 name
  • annotations: field name to type mapping
  • optional_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:

  1. typing_extensions.get_type_hints()
  2. typing.get_type_hints()
  3. 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:

  1. __required_keys__ / __optional_keys__
  2. __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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

create_inspect_typeddict-0.1.0a0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

create_inspect_typeddict-0.1.0a0-py2.py3-none-any.whl (4.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file create_inspect_typeddict-0.1.0a0.tar.gz.

File metadata

File hashes

Hashes for create_inspect_typeddict-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 b1cf48e72cb8f2d9d1c7f544807e9a01acfcc742446463815fe7a5cb58f6c6af
MD5 b6e34fdf7a79e67f2a654df730799fa1
BLAKE2b-256 6da122756305eb27ee0fc32d3391461c77478c85b6c516b984c70ba5df4c251b

See more details on using hashes here.

File details

Details for the file create_inspect_typeddict-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for create_inspect_typeddict-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0b8405750456f956abe80149865a4f4c6a974ffb999a29d9227d92efb9c7faa5
MD5 f99f8ecfa79793420b2938780f8609c3
BLAKE2b-256 be5d860d9d936b955de8f0e9a65b2553d7b03e0bc99813bc0b4ccd3d0c718fd8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page