ovld 0.5.4__py3-none-any.whl → 0.5.6__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/codegen.py CHANGED
@@ -45,13 +45,22 @@ def transfer_function(
45
45
  globals=MISSING,
46
46
  name=MISSING,
47
47
  ):
48
- new_fn = FunctionType(
49
- argdefs=func.__defaults__ if argdefs is MISSING else argdefs,
50
- closure=func.__closure__ if closure is MISSING else closure,
51
- code=func.__code__ if code is MISSING else code,
52
- globals=func.__globals__ if globals is MISSING else globals,
53
- name=func.__name__ if name is MISSING else name,
54
- )
48
+ closure = func.__closure__ if closure is MISSING else closure
49
+ if closure:
50
+ new_fn = FunctionType(
51
+ argdefs=func.__defaults__ if argdefs is MISSING else argdefs,
52
+ closure=func.__closure__ if closure is MISSING else closure,
53
+ code=func.__code__ if code is MISSING else code,
54
+ globals=func.__globals__ if globals is MISSING else globals,
55
+ name=func.__name__ if name is MISSING else name,
56
+ )
57
+ else:
58
+ new_fn = FunctionType(
59
+ argdefs=func.__defaults__ if argdefs is MISSING else argdefs,
60
+ code=func.__code__ if code is MISSING else code,
61
+ globals=func.__globals__ if globals is MISSING else globals,
62
+ name=func.__name__ if name is MISSING else name,
63
+ )
55
64
  new_fn.__kwdefaults__ = func.__kwdefaults__
56
65
  new_fn.__annotations__ = func.__annotations__
57
66
  new_fn.__dict__.update(func.__dict__)
ovld/dependent.py CHANGED
@@ -15,22 +15,15 @@ from .types import (
15
15
  Intersection,
16
16
  Order,
17
17
  clsstring,
18
- get_args,
19
18
  normalize_type,
20
19
  subclasscheck,
21
20
  typeorder,
22
21
  )
23
22
 
24
23
 
25
- def is_dependent(t):
26
- if isinstance(t, DependentType):
27
- return True
28
- elif any(is_dependent(subt) for subt in get_args(t)):
29
- return True
30
- return False
31
-
32
-
33
24
  class DependentType(type):
25
+ __dependent__ = True
26
+
34
27
  exclusive_type = False
35
28
  keyable_type = False
36
29
  bound_is_name = False
ovld/mro.py CHANGED
@@ -3,7 +3,7 @@ from enum import Enum
3
3
  from graphlib import TopologicalSorter
4
4
  from typing import get_args, get_origin
5
5
 
6
- from .utils import UnionTypes
6
+ from .utils import UnionTypes, is_dependent
7
7
 
8
8
 
9
9
  class Order(Enum):
@@ -123,6 +123,11 @@ def subclasscheck(t1, t2):
123
123
  ):
124
124
  return result
125
125
 
126
+ if is_dependent(t2):
127
+ # t2's instancecheck could return anything, and unless it defines
128
+ # __is_supertype__ or __is_subtype__ the bound devolves to object
129
+ return True
130
+
126
131
  if t2 in UnionTypes:
127
132
  return isinstance(t1, t2)
128
133
 
ovld/recode.py CHANGED
@@ -12,7 +12,7 @@ from .codegen import (
12
12
  rename_function,
13
13
  transfer_function,
14
14
  )
15
- from .utils import MISSING, NameDatabase, SpecialForm, UsageError, subtler_type
15
+ from .utils import MISSING, NameDatabase, SpecialForm, UsageError, is_dependent, subtler_type
16
16
 
17
17
  recurse = SpecialForm("recurse")
18
18
  call_next = SpecialForm("call_next")
@@ -160,8 +160,6 @@ def generate_dispatch(ov, arganal):
160
160
 
161
161
 
162
162
  def generate_dependent_dispatch(tup, handlers, next_call, slf, name, err, nerr):
163
- from .dependent import is_dependent
164
-
165
163
  def to_dict(tup):
166
164
  return dict(
167
165
  entry if isinstance(entry, tuple) else (i, entry) for i, entry in enumerate(tup)
ovld/typemap.py CHANGED
@@ -6,7 +6,7 @@ from types import CodeType
6
6
 
7
7
  from .mro import sort_types
8
8
  from .recode import generate_dependent_dispatch
9
- from .utils import MISSING, CodegenInProgress, subtler_type
9
+ from .utils import MISSING, CodegenInProgress, is_dependent, subtler_type
10
10
 
11
11
 
12
12
  class TypeMap(dict):
@@ -213,8 +213,6 @@ class MultiTypeMap(dict):
213
213
  sig: A Signature object.
214
214
  handler: A function to handle the tuple.
215
215
  """
216
- from .dependent import is_dependent
217
-
218
216
  self.clear()
219
217
 
220
218
  obj_t_tup = sig.types
@@ -251,8 +249,6 @@ class MultiTypeMap(dict):
251
249
  print(f"{'':{width - 2}} @ {co.co_filename}:{co.co_firstlineno}")
252
250
 
253
251
  def display_resolution(self, *args, **kwargs):
254
- from .dependent import is_dependent
255
-
256
252
  def dependent_match(tup, args):
257
253
  for t, a in zip(tup, args):
258
254
  if isinstance(t, tuple):
ovld/types.py CHANGED
@@ -10,14 +10,9 @@ from .codegen import Code
10
10
  from .mro import Order, TypeRelationship, subclasscheck, typeorder
11
11
  from .recode import generate_checking_code
12
12
  from .typemap import TypeMap
13
- from .utils import UnionType, UnionTypes, UsageError, clsstring
13
+ from .utils import UnionType, UnionTypes, UsageError, clsstring, get_args
14
14
 
15
-
16
- def get_args(tp):
17
- args = getattr(tp, "__args__", None)
18
- if not isinstance(args, tuple):
19
- args = ()
20
- return args
15
+ NoneType = type(None)
21
16
 
22
17
 
23
18
  def eval_annotation(t, ctx, locals, catch=False):
@@ -90,6 +85,8 @@ class TypeNormalizer:
90
85
  raise UsageError(
91
86
  f"Dependent type {t} has not been given a type bound. Please use Dependent[<bound>, {t}] instead."
92
87
  )
88
+ elif t is None:
89
+ return NoneType
93
90
  else:
94
91
  return t
95
92
 
@@ -103,6 +100,8 @@ def _(self, t, fn):
103
100
 
104
101
 
105
102
  class MetaMC(type):
103
+ __dependent__ = False
104
+
106
105
  def __new__(T, name, handler):
107
106
  return super().__new__(T, name, (), {"_handler": handler})
108
107
 
ovld/utils.py CHANGED
@@ -1,10 +1,15 @@
1
1
  """Miscellaneous utilities."""
2
2
 
3
+ import builtins
3
4
  import functools
4
5
  import re
5
6
  import typing
7
+ from abc import ABCMeta
6
8
  from itertools import count
7
9
 
10
+ _builtins_dict = vars(builtins)
11
+
12
+
8
13
  try:
9
14
  from types import UnionType
10
15
 
@@ -152,7 +157,7 @@ class NameDatabase:
152
157
  i = 1
153
158
  name = desired_name
154
159
  while name in self.registered or (
155
- name in __builtins__ and __builtins__[name] != value
160
+ name in _builtins_dict and _builtins_dict[name] != value
156
161
  ):
157
162
  name = f"{desired_name}{i}"
158
163
  i += 1
@@ -179,3 +184,31 @@ class NameDatabase:
179
184
  return name
180
185
 
181
186
  __getitem__ = get
187
+
188
+
189
+ def get_args(tp):
190
+ args = getattr(tp, "__args__", None)
191
+ if not isinstance(args, tuple):
192
+ args = ()
193
+ return args
194
+
195
+
196
+ _standard_instancechecks = {
197
+ type.__instancecheck__,
198
+ GenericAlias.__instancecheck__,
199
+ type(list[object]).__instancecheck__,
200
+ ABCMeta.__instancecheck__,
201
+ type(typing.Protocol).__instancecheck__,
202
+ }
203
+
204
+
205
+ def is_dependent(t):
206
+ if any(is_dependent(subt) for subt in get_args(t)):
207
+ return True
208
+ elif hasattr(t, "__dependent__"):
209
+ return t.__dependent__
210
+ elif not isinstance(t, type):
211
+ return False
212
+ elif type(t).__instancecheck__ not in _standard_instancechecks:
213
+ return True
214
+ return False
ovld/version.py CHANGED
@@ -1 +1 @@
1
- version = "0.5.4"
1
+ version = "0.5.6"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ovld
3
- Version: 0.5.4
3
+ Version: 0.5.6
4
4
  Summary: Overloading Python functions
5
5
  Project-URL: Homepage, https://ovld.readthedocs.io/en/latest/
6
6
  Project-URL: Documentation, https://ovld.readthedocs.io/en/latest/
@@ -251,13 +251,13 @@ f3 = Total(punctuation="!", factor=3)
251
251
 
252
252
  Time relative to the fastest implementation (1.00) (lower is better).
253
253
 
254
- | Benchmark | custom | [ovld](https://github.com/breuleux/ovld) | [plum](https://github.com/beartype/plum) | [multim](https://github.com/coady/multimethod) | [multid](https://github.com/mrocklin/multipledispatch) | [runtype](https://github.com/erezsh/runtype) | [sd](https://docs.python.org/3/library/functools.html#functools.singledispatch) |
254
+ | Benchmark | custom | [ovld](https://github.com/breuleux/ovld) | [plum](https://github.com/beartype/plum) | [multim](https://github.com/coady/multimethod) | [multid](https://github.com/mrocklin/multipledispatch/) | [runtype](https://github.com/erezsh/runtype) | [sd](https://docs.python.org/3/library/functools.html#functools.singledispatch) |
255
255
  | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
256
- |[trivial](https://github.com/breuleux/ovld/tree/master/benchmarks/test_trivial.py)|1.45|1.00|3.32|4.63|2.04|2.41|1.91|
257
- |[multer](https://github.com/breuleux/ovld/tree/master/benchmarks/test_multer.py)|1.13|1.00|11.05|4.53|8.31|2.19|7.32|
258
- |[add](https://github.com/breuleux/ovld/tree/master/benchmarks/test_add.py)|1.08|1.00|3.73|5.21|2.37|2.79|x|
259
- |[ast](https://github.com/breuleux/ovld/tree/master/benchmarks/test_ast.py)|1.00|1.08|23.14|3.09|1.68|1.91|1.66|
260
- |[calc](https://github.com/breuleux/ovld/tree/master/benchmarks/test_calc.py)|1.00|1.23|54.61|29.32|x|x|x|
261
- |[regexp](https://github.com/breuleux/ovld/tree/master/benchmarks/test_regexp.py)|1.00|1.87|19.18|x|x|x|x|
262
- |[fib](https://github.com/breuleux/ovld/tree/master/benchmarks/test_fib.py)|1.00|3.30|444.31|125.77|x|x|x|
263
- |[tweaknum](https://github.com/breuleux/ovld/tree/master/benchmarks/test_tweaknum.py)|1.00|2.09|x|x|x|x|x|
256
+ |[trivial](https://github.com/breuleux/ovld/tree/master/benchmarks/test_trivial.py)|1.56|1.00|3.38|4.92|2.00|2.38|2.15|
257
+ |[multer](https://github.com/breuleux/ovld/tree/master/benchmarks/test_multer.py)|1.22|1.00|11.06|4.67|9.22|2.24|3.92|
258
+ |[add](https://github.com/breuleux/ovld/tree/master/benchmarks/test_add.py)|1.27|1.00|3.61|4.93|2.24|2.62|x|
259
+ |[ast](https://github.com/breuleux/ovld/tree/master/benchmarks/test_ast.py)|1.01|1.00|22.98|2.72|1.52|1.70|1.57|
260
+ |[calc](https://github.com/breuleux/ovld/tree/master/benchmarks/test_calc.py)|1.00|1.28|57.86|29.79|x|x|x|
261
+ |[regexp](https://github.com/breuleux/ovld/tree/master/benchmarks/test_regexp.py)|1.00|2.28|22.71|x|x|x|x|
262
+ |[fib](https://github.com/breuleux/ovld/tree/master/benchmarks/test_fib.py)|1.00|3.39|403.38|114.69|x|x|x|
263
+ |[tweaknum](https://github.com/breuleux/ovld/tree/master/benchmarks/test_tweaknum.py)|1.00|1.86|x|x|x|x|x||[tweaknum](https://github.com/breuleux/ovld/tree/master/benchmarks/test_tweaknum.py)|1.00|1.86|x|x|x|x|x|
@@ -0,0 +1,18 @@
1
+ ovld/__init__.py,sha256=JuCM8Sj65gobV0KYyLr95cSI23Pi6RYZ7X3_F3fdsSw,1821
2
+ ovld/abc.py,sha256=4qpZyYwI8dWgY1Oiv5FhdKg2uzNcyWxIpGmGJVcjXrs,1177
3
+ ovld/codegen.py,sha256=27tmamlanuTPDT-x31ISyqP0wGKW9BCFZJGVyq9qLg8,9728
4
+ ovld/core.py,sha256=HEREHblKcjM9dhFBr0FNwUCyec7o-9XjCsCfJ23SnNw,17544
5
+ ovld/dependent.py,sha256=JIgsc_5ddPH51_2IrZ6JW6bWE5RyrrrOwR2e9UvDhZ4,8922
6
+ ovld/medley.py,sha256=0fseIntzJRCPYXq-tmnxgy5ipNa4ZxR0D_6So0xstdQ,12729
7
+ ovld/mro.py,sha256=Aw1r5Zz7V9cVBDwWzQ-WNnbpBwoGztgbw3wLAyS6Y60,4863
8
+ ovld/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ ovld/recode.py,sha256=vXg9XLExp_9LdAHO0JWR4wvwHhpOLu2Xcrg9ZYg1nms,16407
10
+ ovld/signatures.py,sha256=Q8JucSOun0ESGx14aWtHtBzLEiM6FxY5HP3imyqXoDo,8984
11
+ ovld/typemap.py,sha256=EH3oM_QmX-SHLEz14saVrQtRlqG_ltPyJGORLvcGOFk,13520
12
+ ovld/types.py,sha256=CRL6Vuzg5moXgAAhIj2698GvZoyF4HWbUDYz2hKt6us,13373
13
+ ovld/utils.py,sha256=cyy9pcuMhmo1_UdPonH9JT6B9QlI4oH6_JK89cM3_gk,5046
14
+ ovld/version.py,sha256=FtfC8ptaFH0Unc72bPRynMH_N62bxa4tnazGgIcgnTY,18
15
+ ovld-0.5.6.dist-info/METADATA,sha256=NuqYyUraNAzGzrl8XUVET_xO6xBSzbPhy5F5AtzITWk,9383
16
+ ovld-0.5.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
17
+ ovld-0.5.6.dist-info/licenses/LICENSE,sha256=cSwNTIzd1cbI89xt3PeZZYJP2y3j8Zus4bXgo4svpX8,1066
18
+ ovld-0.5.6.dist-info/RECORD,,
@@ -1,18 +0,0 @@
1
- ovld/__init__.py,sha256=JuCM8Sj65gobV0KYyLr95cSI23Pi6RYZ7X3_F3fdsSw,1821
2
- ovld/abc.py,sha256=4qpZyYwI8dWgY1Oiv5FhdKg2uzNcyWxIpGmGJVcjXrs,1177
3
- ovld/codegen.py,sha256=iiGgWSpHQEB8QoMDqCx3Jup8bBGpDsESFeKtzGVAolM,9298
4
- ovld/core.py,sha256=HEREHblKcjM9dhFBr0FNwUCyec7o-9XjCsCfJ23SnNw,17544
5
- ovld/dependent.py,sha256=h3j4oQYTQfGqMzggWlLV6TpojX_GtYRFWAO0GcMB0Zs,9085
6
- ovld/medley.py,sha256=0fseIntzJRCPYXq-tmnxgy5ipNa4ZxR0D_6So0xstdQ,12729
7
- ovld/mro.py,sha256=LXHkP_28J9EwM9IrXYraYI0qm4dHWkDKCgkXn0u_YCo,4655
8
- ovld/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- ovld/recode.py,sha256=ZVqD3sCu_myaFj3lKPCMjFBf1pTdTAAd3NJdsqtLYuU,16434
10
- ovld/signatures.py,sha256=Q8JucSOun0ESGx14aWtHtBzLEiM6FxY5HP3imyqXoDo,8984
11
- ovld/typemap.py,sha256=5Pro1Ee60fH4L7NW7k5nbN5EfDygA0LFHcI6o3mCagI,13596
12
- ovld/types.py,sha256=0hkhAR5_5793NABdrM-fP1dSJBhYof85FILKqVP2YMg,13392
13
- ovld/utils.py,sha256=MAPQUhZc3_AcLrU4jFQrkVu4nvMJJKiw10N7zFk5QPc,4294
14
- ovld/version.py,sha256=yCowtNX3TKFIKd4P4DFW3MKaLU51L0lyEs-QRX2uVPs,18
15
- ovld-0.5.4.dist-info/METADATA,sha256=heLELAtYnCT1C6lVPIaZ5709iMDzYJxBhegZAr8uHC8,9276
16
- ovld-0.5.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
17
- ovld-0.5.4.dist-info/licenses/LICENSE,sha256=cSwNTIzd1cbI89xt3PeZZYJP2y3j8Zus4bXgo4svpX8,1066
18
- ovld-0.5.4.dist-info/RECORD,,
File without changes