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: ImmutablelistFrozenDict: Immutabledictfrozen_copy: Create frozen copy of supported types (Sequence,Set,Mapping)
TypeInfo type_info.py
TypeInfo: provide type information and run-time type checkingTypeInfo[...].type: typeTypeInfo[...]..origin: unsubscripted typeTypeInfo[...]..args: type argumentsTypeInfo[...]..name: name of type (including arguments)TypeInfo.check_union(t1, t2): check if union type t1 fulfill union type t2TypeInfo[...].check_value(value): check if value matches the type in type infoTypeInfo[...].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 ofTypedDictanddefaultdictwith type checking in__init__,__setitem__,__setattr__andupdateTypedDefaultDict.schema: the schema dictionary:MappingProxyType[str, NamedTuple[default: Any, type:TypeInfo]]TypedDefaultDict.on_get_unknown_property: called on getting unknown propertyTypedDefaultDict.on_set_unknown_property: called on setting unknown propertyTypedDefaultDict.set: set a property to a value without property check and type check
-
Attributes without type annotation or starting with underscore "_" will be treated as class variables
-
You can override
on_get_unknown_propertyandon_set_unknown_propertyto override the behavior when getting or setting unknown property, raisesUnknownPropertyErrorby default.on_get_unknown_property(k): either raise an error or return a value based on kon_set_unknown_property(k, v): raise an error / return a value based on(k, v)to setkto new value / process and returnNoneto ignore
-
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))
# example overriding on_*_unknown_property
class TestDict(TypedDefaultDict):
foo: str = "foo"
bar: str = "bar"
_extra_props: dict[str, Any] = {} # class variable
@override
def on_get_unknown_property(self, k) -> Any:
raise AttributeError(k)
@override
def on_set_unknown_property(self, k: str, v: Any) -> Any:
self._extra_props[k] = v
return None
EnumEx enum_ex.py
EnumExis aEnumlike 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. -
EnumEx.X.value: value of enum X -
EnumEx.X.name: name of enum X (i.e. X) -
EnumEx.X_ALIAS.orig_name: original name of enum X_ALIAS (i.e. X) -
EnumEx.X_ALIAS.origin: origin enum of X_ALIAS (i.e. EnumEx.X)
-
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
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 typing_ex-0.1.32.tar.gz.
File metadata
- Download URL: typing_ex-0.1.32.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.12.3 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b718f404f3c7f0541a8bb3aeea52cd2c807f339e27b433d8b43c9df70b57004
|
|
| MD5 |
f6434e50bf4f8d6eaf552e5e71fdaf97
|
|
| BLAKE2b-256 |
47072f15101db6ed3359e6f6fe13d2ddd0894dd030ad0e1ba3c7ab8e2666bc52
|
File details
Details for the file typing_ex-0.1.32-py3-none-any.whl.
File metadata
- Download URL: typing_ex-0.1.32-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.12.3 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b0b905631a7acb3fbd3a552bdbb00b3e608ae979e14e05078789abe1721fe14
|
|
| MD5 |
4f47aae9cbbf63a827f8233ec61e6ccc
|
|
| BLAKE2b-256 |
026a32a988e63317676df5dd1dfb0e074de7eb6e0e3444b09ce37a0569123f7d
|