ovld 0.3.2__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
- self.types.add(obj_t)
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
- mro = compose_mro(obj_t, self.types)
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 origin is typing.Union:
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)
@@ -923,7 +948,10 @@ class Conformer:
923
948
 
924
949
  def rename_code(co, newname): # pragma: no cover
925
950
  if hasattr(co, "replace"):
926
- return co.replace(co_name=newname)
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)
927
955
  else:
928
956
  return type(co)(
929
957
  co.co_argcount,
ovld/version.py CHANGED
@@ -1 +1 @@
1
- version = "0.3.2"
1
+ version = "0.3.3"
@@ -1,18 +1,19 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ovld
3
- Version: 0.3.2
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.6,<4.0
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,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry 1.0.0
2
+ Generator: poetry-core 1.9.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -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