ovld 0.3.2__py3-none-any.whl → 0.3.4__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.
- ovld/core.py +51 -6
- ovld/utils.py +13 -0
- ovld/version.py +1 -1
- {ovld-0.3.2.dist-info → ovld-0.3.4.dist-info}/METADATA +5 -4
- ovld-0.3.4.dist-info/RECORD +9 -0
- {ovld-0.3.2.dist-info → ovld-0.3.4.dist-info}/WHEEL +1 -1
- ovld-0.3.2.dist-info/RECORD +0 -9
- {ovld-0.3.2.dist-info → ovld-0.3.4.dist-info}/LICENSE +0 -0
ovld/core.py
CHANGED
@@ -7,10 +7,27 @@ import textwrap
|
|
7
7
|
import typing
|
8
8
|
from types import FunctionType
|
9
9
|
|
10
|
+
try:
|
11
|
+
from types import GenericAlias, UnionType
|
12
|
+
except ImportError: # pragma: no cover
|
13
|
+
UnionType = None
|
14
|
+
|
15
|
+
class GenericAliasMC(type):
|
16
|
+
def __instancecheck__(cls, obj):
|
17
|
+
return hasattr(obj, "__origin__")
|
18
|
+
|
19
|
+
class GenericAlias(metaclass=GenericAliasMC):
|
20
|
+
pass
|
21
|
+
|
22
|
+
|
10
23
|
from .mro import compose_mro
|
11
24
|
from .utils import BOOTSTRAP, MISSING, keyword_decorator
|
12
25
|
|
13
26
|
|
27
|
+
def is_type_of_type(t):
|
28
|
+
return getattr(t, "__origin__", None) is type
|
29
|
+
|
30
|
+
|
14
31
|
class TypeMap(dict):
|
15
32
|
"""Represents a mapping from types to handlers.
|
16
33
|
|
@@ -30,8 +47,14 @@ class TypeMap(dict):
|
|
30
47
|
|
31
48
|
def register(self, obj_t, handler):
|
32
49
|
"""Register a handler for the given object type."""
|
50
|
+
if isinstance(obj_t, str):
|
51
|
+
obj_t = eval(obj_t, getattr(handler[0], "__globals__", {}))
|
52
|
+
|
33
53
|
self.clear()
|
34
|
-
|
54
|
+
if is_type_of_type(obj_t):
|
55
|
+
self.types.add(obj_t.__args__[0])
|
56
|
+
else:
|
57
|
+
self.types.add(obj_t)
|
35
58
|
s = self.entries.setdefault(obj_t, set())
|
36
59
|
s.add(handler)
|
37
60
|
|
@@ -42,7 +65,12 @@ class TypeMap(dict):
|
|
42
65
|
the next time getitem is called.
|
43
66
|
"""
|
44
67
|
results = {}
|
45
|
-
|
68
|
+
if is_type_of_type(obj_t):
|
69
|
+
mro = [type[t] for t in compose_mro(obj_t.__args__[0], self.types)]
|
70
|
+
mro.append(type)
|
71
|
+
mro.append(object)
|
72
|
+
else:
|
73
|
+
mro = compose_mro(obj_t, self.types)
|
46
74
|
for lvl, cls in enumerate(reversed(mro)):
|
47
75
|
handlers = self.entries.get(cls, None)
|
48
76
|
if handlers:
|
@@ -77,9 +105,16 @@ class MultiTypeMap(dict):
|
|
77
105
|
def __init__(self, key_error=KeyError):
|
78
106
|
self.maps = {}
|
79
107
|
self.empty = MISSING
|
80
|
-
self.transform = type
|
81
108
|
self.key_error = key_error
|
82
109
|
|
110
|
+
def transform(self, obj):
|
111
|
+
if isinstance(obj, GenericAlias):
|
112
|
+
return type[obj.__origin__]
|
113
|
+
elif isinstance(obj, type):
|
114
|
+
return type[obj]
|
115
|
+
else:
|
116
|
+
return type(obj)
|
117
|
+
|
83
118
|
def register(self, obj_t_tup, nargs, handler):
|
84
119
|
"""Register a handler for a tuple of argument types.
|
85
120
|
|
@@ -326,6 +361,9 @@ class _Ovld:
|
|
326
361
|
def clsname(cls):
|
327
362
|
if cls is object:
|
328
363
|
return "*"
|
364
|
+
elif is_type_of_type(cls):
|
365
|
+
arg = clsname(cls.__args__[0])
|
366
|
+
return f"type[{arg}]"
|
329
367
|
elif hasattr(cls, "__name__"):
|
330
368
|
return cls.__name__
|
331
369
|
else:
|
@@ -489,11 +527,15 @@ class _Ovld:
|
|
489
527
|
|
490
528
|
def _normalize_type(t, force_tuple=False):
|
491
529
|
origin = getattr(t, "__origin__", None)
|
492
|
-
if
|
530
|
+
if UnionType and isinstance(t, UnionType):
|
531
|
+
return _normalize_type(t.__args__)
|
532
|
+
elif origin is type:
|
533
|
+
return (t,) if force_tuple else t
|
534
|
+
elif origin is typing.Union:
|
493
535
|
return _normalize_type(t.__args__)
|
494
536
|
elif origin is not None:
|
495
537
|
raise TypeError(
|
496
|
-
"ovld does not accept generic types except Union or Optional"
|
538
|
+
"ovld does not accept generic types except type, Union or Optional"
|
497
539
|
)
|
498
540
|
elif isinstance(t, tuple):
|
499
541
|
return tuple(_normalize_type(t2) for t2 in t)
|
@@ -923,7 +965,10 @@ class Conformer:
|
|
923
965
|
|
924
966
|
def rename_code(co, newname): # pragma: no cover
|
925
967
|
if hasattr(co, "replace"):
|
926
|
-
|
968
|
+
if hasattr(co, "co_qualname"):
|
969
|
+
return co.replace(co_name=newname, co_qualname=newname)
|
970
|
+
else:
|
971
|
+
return co.replace(co_name=newname)
|
927
972
|
else:
|
928
973
|
return type(co)(
|
929
974
|
co.co_argcount,
|
ovld/utils.py
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
import functools
|
4
4
|
import sys
|
5
|
+
from dataclasses import dataclass
|
6
|
+
from typing import Protocol, runtime_checkable
|
5
7
|
|
6
8
|
|
7
9
|
class Named:
|
@@ -143,9 +145,20 @@ def has_attribute(*attrs):
|
|
143
145
|
return check
|
144
146
|
|
145
147
|
|
148
|
+
@runtime_checkable
|
149
|
+
@dataclass
|
150
|
+
class Dataclass(Protocol):
|
151
|
+
@classmethod
|
152
|
+
def __subclasshook__(cls, subclass):
|
153
|
+
return hasattr(subclass, "__dataclass_fields__") and hasattr(
|
154
|
+
subclass, "__dataclass_params__"
|
155
|
+
)
|
156
|
+
|
157
|
+
|
146
158
|
__all__ = [
|
147
159
|
"BOOTSTRAP",
|
148
160
|
"MISSING",
|
161
|
+
"Dataclass",
|
149
162
|
"Named",
|
150
163
|
"deferred",
|
151
164
|
"exactly",
|
ovld/version.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
version = "0.3.
|
1
|
+
version = "0.3.4"
|
@@ -1,18 +1,19 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: ovld
|
3
|
-
Version: 0.3.
|
3
|
+
Version: 0.3.4
|
4
4
|
Summary: Overloading Python functions
|
5
5
|
Home-page: https://github.com/breuleux/ovld
|
6
6
|
License: MIT
|
7
7
|
Author: Olivier Breuleux
|
8
8
|
Author-email: breuleux@gmail.com
|
9
|
-
Requires-Python: >=3.
|
9
|
+
Requires-Python: >=3.8,<4.0
|
10
10
|
Classifier: License :: OSI Approved :: MIT License
|
11
11
|
Classifier: Programming Language :: Python :: 3
|
12
|
-
Classifier: Programming Language :: Python :: 3.6
|
13
|
-
Classifier: Programming Language :: Python :: 3.7
|
14
12
|
Classifier: Programming Language :: Python :: 3.8
|
15
13
|
Classifier: Programming Language :: Python :: 3.9
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
16
17
|
Project-URL: Repository, https://github.com/breuleux/ovld
|
17
18
|
Description-Content-Type: text/markdown
|
18
19
|
|
@@ -0,0 +1,9 @@
|
|
1
|
+
ovld/__init__.py,sha256=Wa7MHfDw0_AV7jwSwepntCeXzZheELkb8paxVnjp58Q,85
|
2
|
+
ovld/core.py,sha256=Iz9VMQAqRGsgZqwBkR8uNoBdnC0vp4RYhVDcMydcPlo,33078
|
3
|
+
ovld/mro.py,sha256=Dof3wj-BLcV-T-Jm9tK6zqptp_JGo5lxgyRq99AvY1k,5006
|
4
|
+
ovld/utils.py,sha256=8kINtHrWdC0bNVccxYIkHBaGFk_YFpjtnjAa9GyXNmA,3861
|
5
|
+
ovld/version.py,sha256=gFnDcL_GoJXrglbZytpscjJFX7Ge1G2_exIvsJP20D4,18
|
6
|
+
ovld-0.3.4.dist-info/LICENSE,sha256=cSwNTIzd1cbI89xt3PeZZYJP2y3j8Zus4bXgo4svpX8,1066
|
7
|
+
ovld-0.3.4.dist-info/METADATA,sha256=1JtepDt_ZRKv07KGQb0DhymtG5OnYXXUyy1UEg4XHy4,9439
|
8
|
+
ovld-0.3.4.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
9
|
+
ovld-0.3.4.dist-info/RECORD,,
|
ovld-0.3.2.dist-info/RECORD
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
ovld/__init__.py,sha256=Wa7MHfDw0_AV7jwSwepntCeXzZheELkb8paxVnjp58Q,85
|
2
|
-
ovld/core.py,sha256=Ejuu5q6K-OfI_C9xoyLul-P1P0ORTeitwSnu8Ud8wU0,31667
|
3
|
-
ovld/mro.py,sha256=Dof3wj-BLcV-T-Jm9tK6zqptp_JGo5lxgyRq99AvY1k,5006
|
4
|
-
ovld/utils.py,sha256=JT44u-OiFC2QBUylaubwKlgvvRS0EYPfykxgMbbIrMY,3521
|
5
|
-
ovld/version.py,sha256=zjuPzQ2OlxNtrLXb5A3OlPsaRb_b_Ln51AWN6r9VRho,18
|
6
|
-
ovld-0.3.2.dist-info/LICENSE,sha256=cSwNTIzd1cbI89xt3PeZZYJP2y3j8Zus4bXgo4svpX8,1066
|
7
|
-
ovld-0.3.2.dist-info/WHEEL,sha256=SrtnPGVTMeYWttls9xnWA01eUhCZ3ufFdJUYb1J3r-U,83
|
8
|
-
ovld-0.3.2.dist-info/METADATA,sha256=9yq7sErgeIWMU0yRJfx-PskGlg2oZlCmlRA25aUS1X0,9386
|
9
|
-
ovld-0.3.2.dist-info/RECORD,,
|
File without changes
|