dycw-utilities 0.110.7__py3-none-any.whl → 0.110.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-utilities
3
- Version: 0.110.7
3
+ Version: 0.110.8
4
4
  Author-email: Derek Wan <d.wan@icloud.com>
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.12
@@ -1,4 +1,4 @@
1
- utilities/__init__.py,sha256=RH75kOPQZPuI3PzW-iy4R8ZfTSbKQHPjZwc14TPFLN8,60
1
+ utilities/__init__.py,sha256=USEDB4wi-U2inTRQAJuZ7L5i_LnX5kcGM17wMf2dOck,60
2
2
  utilities/altair.py,sha256=Gpja-flOo-Db0PIPJLJsgzAlXWoKUjPU1qY-DQ829ek,9156
3
3
  utilities/astor.py,sha256=xuDUkjq0-b6fhtwjhbnebzbqQZAjMSHR1IIS5uOodVg,777
4
4
  utilities/asyncio.py,sha256=41oQUurWMvadFK5gFnaG21hMM0Vmfn2WS6OpC0R9mas,14757
@@ -78,7 +78,7 @@ utilities/threading.py,sha256=GvBOp4CyhHfN90wGXZuA2VKe9fGzMaEa7oCl4f3nnPU,1009
78
78
  utilities/timer.py,sha256=Rkc49KSpHuC8s7vUxGO9DU55U9I6yDKnchsQqrUCVBs,4075
79
79
  utilities/traceback.py,sha256=KwHPLdEbdj0fFhXo8MBfxcvem8A-VXYDwFMNJ6f0cTM,27328
80
80
  utilities/types.py,sha256=kVY71hZkcnyYNIlYSse0mLm8yeP3OBkzhDPMME6jXxo,18126
81
- utilities/typing.py,sha256=gLg4EbE1FX52fJ1d3ji4i08qolwu9qgWt8w_w_Y5DTk,5512
81
+ utilities/typing.py,sha256=WE3UWaVYO6nTkpEo8Ke3xUuxllsJtfOxsZ0-mSH69Nw,6668
82
82
  utilities/tzdata.py,sha256=2ZsPmhTVM9Ptrxb4QrWKtKOB9RiH8IOO-A1u7ULdVbg,176
83
83
  utilities/tzlocal.py,sha256=42BCquGF54oIqIKe5RGziP4K8Nbm3Ey7uqcNn6m5ge8,534
84
84
  utilities/uuid.py,sha256=jJTFxz-CWgltqNuzmythB7iEQ-Q1mCwPevUfKthZT3c,611
@@ -87,7 +87,7 @@ utilities/warnings.py,sha256=un1LvHv70PU-LLv8RxPVmugTzDJkkGXRMZTE2-fTQHw,1771
87
87
  utilities/whenever.py,sha256=TjoTAJ1R27-rKXiXzdE4GzPidmYqm0W58XydDXp-QZM,17786
88
88
  utilities/zipfile.py,sha256=24lQc9ATcJxHXBPc_tBDiJk48pWyRrlxO2fIsFxU0A8,699
89
89
  utilities/zoneinfo.py,sha256=-DQz5a0Ikw9jfSZtL0BEQkXOMC9yGn_xiJYNCLMiqEc,1989
90
- dycw_utilities-0.110.7.dist-info/METADATA,sha256=wFw9aZuOwiyPR1o4QtTmz5fPx9y5gBbKheuvV0XKhA8,13004
91
- dycw_utilities-0.110.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
92
- dycw_utilities-0.110.7.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
93
- dycw_utilities-0.110.7.dist-info/RECORD,,
90
+ dycw_utilities-0.110.8.dist-info/METADATA,sha256=ZVWP8hzsSisjUrY3uoJqCIoh5j7pVhSy0PwDigCaJRw,13004
91
+ dycw_utilities-0.110.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
92
+ dycw_utilities-0.110.8.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
93
+ dycw_utilities-0.110.8.dist-info/RECORD,,
utilities/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.110.7"
3
+ __version__ = "0.110.8"
utilities/typing.py CHANGED
@@ -2,6 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  import datetime as dt
4
4
  from collections.abc import Mapping, Sequence
5
+ from dataclasses import dataclass
5
6
  from itertools import chain
6
7
  from pathlib import Path
7
8
  from types import NoneType, UnionType
@@ -15,6 +16,7 @@ from typing import (
15
16
  TypeGuard,
16
17
  Union, # pyright: ignore[reportDeprecated]
17
18
  get_origin,
19
+ override,
18
20
  )
19
21
  from typing import get_args as _get_args
20
22
  from typing import get_type_hints as _get_type_hints
@@ -64,7 +66,7 @@ def _get_literal_elements_inner(obj: Any, /) -> list[Any]:
64
66
 
65
67
 
66
68
  def get_type_hints(
67
- cls: Any,
69
+ obj: Any,
68
70
  /,
69
71
  *,
70
72
  globalns: StrMapping | None = None,
@@ -72,15 +74,15 @@ def get_type_hints(
72
74
  warn_name_errors: bool = False,
73
75
  ) -> dict[str, Any]:
74
76
  """Get the type hints of an object."""
75
- result: dict[str, Any] = cls.__annotations__
77
+ result: dict[str, Any] = obj.__annotations__
76
78
  _ = {Literal, Path, Sentinel, StrMapping, UUID, dt}
77
79
  globalns_use = globals() | ({} if globalns is None else dict(globalns))
78
80
  localns_use = {} if localns is None else dict(localns)
79
81
  try:
80
- hints = _get_type_hints(cls, globalns=globalns_use, localns=localns_use)
82
+ hints = _get_type_hints(obj, globalns=globalns_use, localns=localns_use)
81
83
  except NameError as error:
82
84
  if warn_name_errors:
83
- warn(f"Error getting type hints for {cls!r}; {error}", stacklevel=2)
85
+ warn(f"Error getting type hints for {obj!r}; {error}", stacklevel=2)
84
86
  else:
85
87
  result.update({
86
88
  key: value
@@ -93,6 +95,44 @@ def get_type_hints(
93
95
  ##
94
96
 
95
97
 
98
+ def get_union_type_classes(obj: Any, /) -> tuple[type[Any], ...]:
99
+ if not is_union_type(obj):
100
+ raise _GetUnionTypeClassesNotAUnionTypeError(obj=obj)
101
+ types_: Sequence[type[Any]] = []
102
+ for arg in get_args(obj):
103
+ if isinstance(arg, type):
104
+ types_.append(arg)
105
+ elif is_union_type(arg):
106
+ types_.extend(get_union_type_classes(arg))
107
+ else:
108
+ raise _GetUnionTypeClassesNotATypeError(obj=obj, inner=arg)
109
+ return tuple(types_)
110
+
111
+
112
+ @dataclass(kw_only=True, slots=True)
113
+ class GetUnionTypeClassesError(Exception):
114
+ obj: Any
115
+
116
+
117
+ @dataclass(kw_only=True, slots=True)
118
+ class _GetUnionTypeClassesNotAUnionTypeError(GetUnionTypeClassesError):
119
+ @override
120
+ def __str__(self) -> str:
121
+ return f"Object must be a Union type; got {self.obj}"
122
+
123
+
124
+ @dataclass(kw_only=True, slots=True)
125
+ class _GetUnionTypeClassesNotATypeError(GetUnionTypeClassesError):
126
+ inner: Any
127
+
128
+ @override
129
+ def __str__(self) -> str:
130
+ return f"Union type must contain types only; got {self.inner}"
131
+
132
+
133
+ ##
134
+
135
+
96
136
  def is_dict_type(obj: Any, /) -> bool:
97
137
  """Check if an object is a dict type annotation."""
98
138
  return _is_annotation_of_type(obj, dict)
@@ -207,9 +247,11 @@ def _is_annotation_of_type(obj: Any, origin: Any, /) -> bool:
207
247
 
208
248
 
209
249
  __all__ = [
250
+ "GetUnionTypeClassesError",
210
251
  "contains_self",
211
252
  "get_literal_elements",
212
253
  "get_type_hints",
254
+ "get_union_type_classes",
213
255
  "is_dict_type",
214
256
  "is_frozenset_type",
215
257
  "is_list_type",