Skip to main content

A package for extended built-in types

Project description

typing_ex

A python package for extended typing (python 3.8+)

Notice: yet to be heavily tested, only simple tests are passed.

Frozen frozen.py

  • FrozenList: Immutable list
  • FrozenDict: Immutable dict
  • frozen_copy: Create frozen copy of supported types (Sequence, Set, Mapping)

TypeInfo type_info.py

  • TypeInfo: provide type information and run-time type checking
    • TypeInfo[...].type: type
    • TypeInfo[...]..origin: unsubscripted type
    • TypeInfo[...]..args: type arguments
    • TypeInfo[...]..name: name of type (including arguments)
    • TypeInfo.check_union(t1, t2): check if union type t1 fulfill union type t2
    • TypeInfo[...].check_value(value): check if value matches the type in type info
    • TypeInfo[...].is_*: check if type is _
# type_a | type_b is only supported when python >= 3.10
TypeInfo[list[int | str]].name # list[int | str]
TypeInfo.check_union(int | str, str | int) # True
TypeInfo.check_union(int, int | str) # True
TypeInfo.check_union(int, Union[int | str]) # True
TypeInfo.check_union(Union[int, str], Union[str, int]) # True
TypeInfo.check_union(int | str, str) # False
TypeInfo.check_union(int | str, int | float) # False
TypeInfo.check_union(Union[int, str], int | float) # False
TypeInfo[int | str].check_value(123) # True
TypeInfo[list[int | str]].check_value([1, "2", 3]) # True
TypeInfo[list[int]].check_value([1, "2", 3]) # False
TypeInfo[list[int | str]].check_value([1.0, "2", 3]) # False

TypedDefaultDict typed_defaultdict.py

  • TypedDefaultDict: combining features of TypedDict and defaultdict with type checking in __init__, __setitem__ and __setattr__

  • Good for writing schema as a class for JSON/YAML/XML/etc. data which provides both static and runtime type checking.

class TestDict(TypedDefaultDict):
    foo: str | None = "bar" # default value is "bar"
    bar: list[str] = [] # default value is []

testdict = TestDict()
testdict["foo"] # "bar"
testdict.foo # "bar"
testdict.bar # []
# check type and unexpected properties on assignment
testdict.abcd # raises `UnknownPropertyError`
testdict.foo = 123 # raises `PropertyValueError`
testdict = TestDict( # raises `PropertyValueError`
    foo = 123
)

# example of loading from JSON
with open("test.json", "r") as f:
    # will also check on init
    testdict = TestDict(json.load(f))

EnumEx enum_ex.py

  • EnumEx is a Enum like class that support enum aliasing (keeping same value but different name).
    • EnumEx.__iter__: a generator of all non-alias enum instances.
    • EnumEx.names: a tuple of all enum names.
    • EnumEx.values: a tuple of all enum values.
    • EnumEx.enums: a tuple of all enum instances.
    • EnumEx.value_type: returns __value_type__ if defined, int otherwise.
class AliasedEnum(EnumEx):
    __value_type__ = str # default is int
    foo = "1"
    foo2 = foo # alias
    bar = "2"
    bar2 = bar # alias

foo.name # "foo"
foo.value # 1

foo2.name # "foo2"
foo2.value # 1
foo2.orig_name # "foo"

bar2.name # "bar2"
bar2.value # 2
bar2.orig_name # "bar"

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

typing_ex-0.1.3.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

typing_ex-0.1.3-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file typing_ex-0.1.3.tar.gz.

File metadata

  • Download URL: typing_ex-0.1.3.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.12.3 CPython/3.10.12

File hashes

Hashes for typing_ex-0.1.3.tar.gz
Algorithm Hash digest
SHA256 bf5f0edec1749f3d6f47cd4088d429b5b9b018f8aca02193bfa40c44779f9504
MD5 221ddd6b707986e42d407462e5f91519
BLAKE2b-256 335cda92d81e0aa4dd31717c5b83140dff7052342cc351672fa7234978846d36

See more details on using hashes here.

File details

Details for the file typing_ex-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: typing_ex-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.12.3 CPython/3.10.12

File hashes

Hashes for typing_ex-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d269dc853cc15b8fe7f351fd0643302ee2a3d59bc82c39f4fdce6b5f17e337ec
MD5 c79c37f89136816168b2f6634b3a4256
BLAKE2b-256 0d4ffd5097683e4803f21b921036cca2e563bce817d459711218bd2a66892acb

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