@@ -34,7 +34,7 @@ def test_specialize() -> None:
34
34
assert fun(B())
35
35
assert fun(A())
36
36
37
- [case testSuperclassImplementationNotUsedWhenSubclassHasImplementation-xfail ]
37
+ [case testSuperclassImplementationNotUsedWhenSubclassHasImplementation]
38
38
from functools import singledispatch
39
39
class A: pass
40
40
class B(A): pass
@@ -122,15 +122,15 @@ def test_singledispatch() -> None:
122
122
assert fun(0)
123
123
assert not fun('a')
124
124
125
- [case testRegisterDoesntChangeFunction-xfail ]
125
+ [case testRegisterDoesntChangeFunction]
126
126
from functools import singledispatch
127
127
128
128
@singledispatch
129
129
def fun(arg) -> bool:
130
130
return False
131
131
132
- @fun.register
133
- def fun_specialized(arg: int ) -> bool:
132
+ @fun.register(int)
133
+ def fun_specialized(arg) -> bool:
134
134
return True
135
135
136
136
def test_singledispatch() -> None:
@@ -183,25 +183,6 @@ def test_singledispatch() -> None:
183
183
assert not fun(1)
184
184
assert fun({'a': 'b'})
185
185
186
- [case testArgumentDoesntMatchTypeOfAnySpecializedImplementationsOrDefaultImplementation-xfail]
187
- from functools import singledispatch
188
- class A: pass
189
- class B(A): pass
190
-
191
- @singledispatch
192
- def fun(arg: A) -> bool:
193
- return False
194
-
195
- @fun.register
196
- def fun_specialized(arg: B) -> bool:
197
- return True
198
-
199
- def test_singledispatch() -> None:
200
- assert fun(B())
201
- assert fun(A())
202
- assert not fun([1, 2])
203
-
204
-
205
186
[case testSingleDispatchMethod-xfail]
206
187
from functools import singledispatchmethod
207
188
class A:
@@ -390,23 +371,23 @@ def test_singledispatch():
390
371
from functools import singledispatch
391
372
392
373
@singledispatch
393
- def f(arg, *, kwarg: bool = False ) -> bool :
394
- return not kwarg
374
+ def f(arg, *, kwarg: int = 0 ) -> int :
375
+ return kwarg + 10
395
376
396
377
@f.register
397
- def g(arg: int, *, kwarg: bool = True ) -> bool :
398
- return kwarg
378
+ def g(arg: int, *, kwarg: int = 5 ) -> int :
379
+ return kwarg - 10
399
380
400
381
def test_keywords():
401
- assert f('a')
402
- assert f('a', kwarg=False)
403
- assert not f('a', kwarg=True)
382
+ assert f('a') == 10
383
+ assert f('a', kwarg=3) == 13
384
+ assert f('a', kwarg=7) == 17
404
385
405
- assert f(1)
406
- assert f(1, kwarg=True)
407
- assert not f(1, kwarg=False)
386
+ assert f(1) == -5
387
+ assert f(1, kwarg=4) == -6
388
+ assert f(1, kwarg=6) == -4
408
389
409
- [case testGeneratorAndMultipleTypesOfIterable-xfail ]
390
+ [case testGeneratorAndMultipleTypesOfIterable]
410
391
from functools import singledispatch
411
392
from typing import *
412
393
@@ -422,3 +403,77 @@ def test_iterables():
422
403
assert f(1) != [1]
423
404
assert list(f(1)) == [1]
424
405
assert f('a') == [0]
406
+
407
+ [case testRegisterUsedAtSameTimeAsOtherDecorators]
408
+ from functools import singledispatch
409
+ from typing import TypeVar
410
+
411
+ class A: pass
412
+ class B: pass
413
+
414
+ T = TypeVar('T')
415
+
416
+ def decorator1(f: T) -> T:
417
+ return f
418
+
419
+ def decorator2(f: T) -> T:
420
+ return f
421
+
422
+ @singledispatch
423
+ def f(arg) -> int:
424
+ return 0
425
+
426
+ @decorator1
427
+ @f.register
428
+ def g(arg: int) -> int:
429
+ return 1
430
+
431
+ @f.register
432
+ @decorator1
433
+ def h(arg: str) -> int:
434
+ return 2
435
+
436
+ @decorator2
437
+ @f.register
438
+ @decorator1
439
+ def i(arg: A) -> int:
440
+ return 3
441
+
442
+ def test_singledispatch():
443
+ assert f(1) == 1
444
+ assert f('a') == 2
445
+ assert f(A()) == 3
446
+ assert f(B()) == 0
447
+
448
+ [case testDecoratorModifiesFunction]
449
+ from functools import singledispatch
450
+ from typing import Callable, Any
451
+
452
+ class A: pass
453
+
454
+ def decorator(f: Callable[[Any], int]) -> Callable[[Any], int]:
455
+ def wrapper(x) -> int:
456
+ return f(x) * 7
457
+ return wrapper
458
+
459
+ @singledispatch
460
+ def f(arg) -> int:
461
+ return 10
462
+
463
+ @decorator
464
+ @f.register
465
+ def g(arg: int) -> int:
466
+ return 3
467
+
468
+ @f.register
469
+ @decorator
470
+ def h(arg: str) -> int:
471
+ return 5
472
+
473
+
474
+ def test_singledispatch():
475
+ # should be registered before decorator is run
476
+ assert f(1) == 3
477
+ # should be registered after decorator is run
478
+ assert f('a') == 35
479
+ assert f(A()) == 10
0 commit comments