Skip to content

Commit 1515309

Browse files
committed
Update singledispatch tests
This makes several changes to the singledispatch run tests: - in testRegisterDoesntChangeFunction, pass the type as an argument to register to avoid a mypy error - remove a test where we pass in an invalid type to a singledispatch function, because mypy correctly treats that as an error - in testKeywordArguments, change the keyword argument type and return value to make it easier to tell what's failing - add 2 new tests related to registering functions while applying other decorators to the registered functions - change several previously xfailed tests to non-xfailed tests because they are now able to pass due to previous changes (like generating a separate dispatch function and using non-native calls)
1 parent 59555e4 commit 1515309

File tree

1 file changed

+89
-34
lines changed

1 file changed

+89
-34
lines changed

mypyc/test-data/run-singledispatch.test

Lines changed: 89 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_specialize() -> None:
3434
assert fun(B())
3535
assert fun(A())
3636

37-
[case testSuperclassImplementationNotUsedWhenSubclassHasImplementation-xfail]
37+
[case testSuperclassImplementationNotUsedWhenSubclassHasImplementation]
3838
from functools import singledispatch
3939
class A: pass
4040
class B(A): pass
@@ -122,15 +122,15 @@ def test_singledispatch() -> None:
122122
assert fun(0)
123123
assert not fun('a')
124124

125-
[case testRegisterDoesntChangeFunction-xfail]
125+
[case testRegisterDoesntChangeFunction]
126126
from functools import singledispatch
127127

128128
@singledispatch
129129
def fun(arg) -> bool:
130130
return False
131131

132-
@fun.register
133-
def fun_specialized(arg: int) -> bool:
132+
@fun.register(int)
133+
def fun_specialized(arg) -> bool:
134134
return True
135135

136136
def test_singledispatch() -> None:
@@ -183,25 +183,6 @@ def test_singledispatch() -> None:
183183
assert not fun(1)
184184
assert fun({'a': 'b'})
185185

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-
205186
[case testSingleDispatchMethod-xfail]
206187
from functools import singledispatchmethod
207188
class A:
@@ -390,23 +371,23 @@ def test_singledispatch():
390371
from functools import singledispatch
391372

392373
@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
395376

396377
@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
399380

400381
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
404385

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
408389

409-
[case testGeneratorAndMultipleTypesOfIterable-xfail]
390+
[case testGeneratorAndMultipleTypesOfIterable]
410391
from functools import singledispatch
411392
from typing import *
412393

@@ -422,3 +403,77 @@ def test_iterables():
422403
assert f(1) != [1]
423404
assert list(f(1)) == [1]
424405
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

Comments
 (0)