ovld 0.3.1__py3-none-any.whl → 0.3.3__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
CHANGED
@@ -7,10 +7,19 @@ import textwrap
|
|
7
7
|
import typing
|
8
8
|
from types import FunctionType
|
9
9
|
|
10
|
+
try:
|
11
|
+
from types import UnionType
|
12
|
+
except ImportError: # pragma: no cover
|
13
|
+
UnionType = None
|
14
|
+
|
10
15
|
from .mro import compose_mro
|
11
16
|
from .utils import BOOTSTRAP, MISSING, keyword_decorator
|
12
17
|
|
13
18
|
|
19
|
+
def is_type_of_type(t):
|
20
|
+
return getattr(t, "__origin__", None) is type
|
21
|
+
|
22
|
+
|
14
23
|
class TypeMap(dict):
|
15
24
|
"""Represents a mapping from types to handlers.
|
16
25
|
|
@@ -31,7 +40,8 @@ class TypeMap(dict):
|
|
31
40
|
def register(self, obj_t, handler):
|
32
41
|
"""Register a handler for the given object type."""
|
33
42
|
self.clear()
|
34
|
-
|
43
|
+
if not is_type_of_type(obj_t):
|
44
|
+
self.types.add(obj_t)
|
35
45
|
s = self.entries.setdefault(obj_t, set())
|
36
46
|
s.add(handler)
|
37
47
|
|
@@ -42,7 +52,10 @@ class TypeMap(dict):
|
|
42
52
|
the next time getitem is called.
|
43
53
|
"""
|
44
54
|
results = {}
|
45
|
-
|
55
|
+
if is_type_of_type(obj_t):
|
56
|
+
mro = [type[t] for t in compose_mro(obj_t.__args__[0], self.types)]
|
57
|
+
else:
|
58
|
+
mro = compose_mro(obj_t, self.types)
|
46
59
|
for lvl, cls in enumerate(reversed(mro)):
|
47
60
|
handlers = self.entries.get(cls, None)
|
48
61
|
if handlers:
|
@@ -77,9 +90,14 @@ class MultiTypeMap(dict):
|
|
77
90
|
def __init__(self, key_error=KeyError):
|
78
91
|
self.maps = {}
|
79
92
|
self.empty = MISSING
|
80
|
-
self.transform = type
|
81
93
|
self.key_error = key_error
|
82
94
|
|
95
|
+
def transform(self, obj):
|
96
|
+
if isinstance(obj, type):
|
97
|
+
return type[obj]
|
98
|
+
else:
|
99
|
+
return type(obj)
|
100
|
+
|
83
101
|
def register(self, obj_t_tup, nargs, handler):
|
84
102
|
"""Register a handler for a tuple of argument types.
|
85
103
|
|
@@ -326,6 +344,9 @@ class _Ovld:
|
|
326
344
|
def clsname(cls):
|
327
345
|
if cls is object:
|
328
346
|
return "*"
|
347
|
+
elif is_type_of_type(cls):
|
348
|
+
arg = clsname(cls.__args__[0])
|
349
|
+
return f"type[{arg}]"
|
329
350
|
elif hasattr(cls, "__name__"):
|
330
351
|
return cls.__name__
|
331
352
|
else:
|
@@ -489,11 +510,15 @@ class _Ovld:
|
|
489
510
|
|
490
511
|
def _normalize_type(t, force_tuple=False):
|
491
512
|
origin = getattr(t, "__origin__", None)
|
492
|
-
if
|
513
|
+
if UnionType and isinstance(t, UnionType):
|
514
|
+
return _normalize_type(t.__args__)
|
515
|
+
elif origin is type:
|
516
|
+
return (t,) if force_tuple else t
|
517
|
+
elif origin is typing.Union:
|
493
518
|
return _normalize_type(t.__args__)
|
494
519
|
elif origin is not None:
|
495
520
|
raise TypeError(
|
496
|
-
"ovld does not accept generic types except Union or Optional"
|
521
|
+
"ovld does not accept generic types except type, Union or Optional"
|
497
522
|
)
|
498
523
|
elif isinstance(t, tuple):
|
499
524
|
return tuple(_normalize_type(t2) for t2 in t)
|
@@ -651,6 +676,11 @@ class _Ovld:
|
|
651
676
|
return f"<Ovld {self.name or hex(id(self))}>"
|
652
677
|
|
653
678
|
|
679
|
+
def is_ovld(x):
|
680
|
+
"""Return whether the argument is an ovld function/method."""
|
681
|
+
return isinstance(x, _Ovld)
|
682
|
+
|
683
|
+
|
654
684
|
class OvldCall:
|
655
685
|
"""Context for an Ovld call."""
|
656
686
|
|
@@ -770,13 +800,12 @@ class OvldMC(type):
|
|
770
800
|
|
771
801
|
for name in names:
|
772
802
|
values = [getattr(base, name, None) for base in bases]
|
803
|
+
ovlds = [v for v in values if is_ovld(v)]
|
773
804
|
mixins = [
|
774
|
-
v
|
775
|
-
for v in values
|
776
|
-
if is_ovld(v) and getattr(v, "_extend_super", False)
|
805
|
+
v for v in ovlds[1:] if getattr(v, "_extend_super", False)
|
777
806
|
]
|
778
807
|
if mixins:
|
779
|
-
o =
|
808
|
+
o = ovlds[0].copy(mixins=mixins)
|
780
809
|
others = [v for v in values if v is not None and not is_ovld(v)]
|
781
810
|
for other in others:
|
782
811
|
o.register(other)
|
@@ -786,6 +815,10 @@ class OvldMC(type):
|
|
786
815
|
return d
|
787
816
|
|
788
817
|
|
818
|
+
class OvldBase(metaclass=OvldMC):
|
819
|
+
"""Base class that allows overloading of methods."""
|
820
|
+
|
821
|
+
|
789
822
|
def _find_overload(fn, **kwargs):
|
790
823
|
mod = __import__(fn.__module__, fromlist="_")
|
791
824
|
dispatch = getattr(mod, fn.__qualname__, None)
|
@@ -915,7 +948,10 @@ class Conformer:
|
|
915
948
|
|
916
949
|
def rename_code(co, newname): # pragma: no cover
|
917
950
|
if hasattr(co, "replace"):
|
918
|
-
|
951
|
+
if hasattr(co, "co_qualname"):
|
952
|
+
return co.replace(co_name=newname, co_qualname=newname)
|
953
|
+
else:
|
954
|
+
return co.replace(co_name=newname)
|
919
955
|
else:
|
920
956
|
return type(co)(
|
921
957
|
co.co_argcount,
|
@@ -946,14 +982,10 @@ def rename_function(fn, newname):
|
|
946
982
|
return new_fn
|
947
983
|
|
948
984
|
|
949
|
-
def is_ovld(x):
|
950
|
-
"""Return whether the argument is an ovld function/method."""
|
951
|
-
return isinstance(x, _Ovld)
|
952
|
-
|
953
|
-
|
954
985
|
__all__ = [
|
955
986
|
"MultiTypeMap",
|
956
987
|
"Ovld",
|
988
|
+
"OvldBase",
|
957
989
|
"OvldCall",
|
958
990
|
"OvldMC",
|
959
991
|
"TypeMap",
|
ovld/version.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
version = "0.3.
|
1
|
+
version = "0.3.3"
|
@@ -1,18 +1,19 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: ovld
|
3
|
-
Version: 0.3.
|
3
|
+
Version: 0.3.3
|
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=uLRvhoXb0d8ide648_e1EsYdqDZZZtz3rLqVAsOdisw,32571
|
3
|
+
ovld/mro.py,sha256=Dof3wj-BLcV-T-Jm9tK6zqptp_JGo5lxgyRq99AvY1k,5006
|
4
|
+
ovld/utils.py,sha256=JT44u-OiFC2QBUylaubwKlgvvRS0EYPfykxgMbbIrMY,3521
|
5
|
+
ovld/version.py,sha256=nlGXcobmeAmNzRkhqDLZxGFLcidRwZSZPlWcdlVyxvw,18
|
6
|
+
ovld-0.3.3.dist-info/LICENSE,sha256=cSwNTIzd1cbI89xt3PeZZYJP2y3j8Zus4bXgo4svpX8,1066
|
7
|
+
ovld-0.3.3.dist-info/METADATA,sha256=vf9bZaiRGKrZzreiu_jvSFLkIzd4cyP6Agz4qarM4Zc,9439
|
8
|
+
ovld-0.3.3.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
9
|
+
ovld-0.3.3.dist-info/RECORD,,
|
ovld-0.3.1.dist-info/RECORD
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
ovld/__init__.py,sha256=Wa7MHfDw0_AV7jwSwepntCeXzZheELkb8paxVnjp58Q,85
|
2
|
-
ovld/core.py,sha256=NOxlxUFe8NFpdAm_UyMlZihs-zXHTZFaXwc4jgfZtrY,31553
|
3
|
-
ovld/mro.py,sha256=Dof3wj-BLcV-T-Jm9tK6zqptp_JGo5lxgyRq99AvY1k,5006
|
4
|
-
ovld/utils.py,sha256=JT44u-OiFC2QBUylaubwKlgvvRS0EYPfykxgMbbIrMY,3521
|
5
|
-
ovld/version.py,sha256=sEAhGxRzEBE5t0VjAcJ-336II62pGIQ0eLrs42I-sGU,18
|
6
|
-
ovld-0.3.1.dist-info/LICENSE,sha256=cSwNTIzd1cbI89xt3PeZZYJP2y3j8Zus4bXgo4svpX8,1066
|
7
|
-
ovld-0.3.1.dist-info/WHEEL,sha256=SrtnPGVTMeYWttls9xnWA01eUhCZ3ufFdJUYb1J3r-U,83
|
8
|
-
ovld-0.3.1.dist-info/METADATA,sha256=2n-dwrZZf8ZP_8MiSJzRSPXdWZvPBtOmTFluB0Yktxk,9386
|
9
|
-
ovld-0.3.1.dist-info/RECORD,,
|
File without changes
|