| 1 |
# Test cases related to the functools.singledispatch decorator |
| 2 |
# Most of these tests are marked as xfails because mypyc doesn't support singledispatch yet |
| 3 |
# (These tests will be re-enabled when mypyc supports singledispatch) |
| 4 |
|
| 5 |
[case testSpecializedImplementationUsed] |
| 6 |
from functools import singledispatch |
| 7 |
from typing import Any |
| 8 |
|
| 9 |
@singledispatch |
| 10 |
def fun(arg: Any) -> bool: |
| 11 |
return False |
| 12 |
|
| 13 |
@fun.register |
| 14 |
def fun_specialized(arg: str) -> bool: |
| 15 |
return True |
| 16 |
|
| 17 |
def test_specialize() -> None: |
| 18 |
assert fun('a') |
| 19 |
assert not fun(3) |
| 20 |
|
| 21 |
[case testSubclassesOfExpectedTypeUseSpecialized] |
| 22 |
from functools import singledispatch |
| 23 |
from typing import Any |
| 24 |
|
| 25 |
class A: pass |
| 26 |
class B(A): pass |
| 27 |
|
| 28 |
@singledispatch |
| 29 |
def fun(arg: Any) -> bool: |
| 30 |
return False |
| 31 |
|
| 32 |
@fun.register |
| 33 |
def fun_specialized(arg: A) -> bool: |
| 34 |
return True |
| 35 |
|
| 36 |
def test_specialize() -> None: |
| 37 |
assert fun(B()) |
| 38 |
assert fun(A()) |
| 39 |
|
| 40 |
[case testSuperclassImplementationNotUsedWhenSubclassHasImplementation] |
| 41 |
from functools import singledispatch |
| 42 |
from typing import Any |
| 43 |
|
| 44 |
class A: pass |
| 45 |
class B(A): pass |
| 46 |
|
| 47 |
@singledispatch |
| 48 |
def fun(arg: Any) -> bool: |
| 49 |
# shouldn't be using this |
| 50 |
assert False |
| 51 |
|
| 52 |
@fun.register |
| 53 |
def fun_specialized(arg: A) -> bool: |
| 54 |
return False |
| 55 |
|
| 56 |
@fun.register |
| 57 |
def fun_specialized2(arg: B) -> bool: |
| 58 |
return True |
| 59 |
|
| 60 |
def test_specialize() -> None: |
| 61 |
assert fun(B()) |
| 62 |
assert not fun(A()) |
| 63 |
|
| 64 |
[case testMultipleUnderscoreFunctionsIsntError] |
| 65 |
from functools import singledispatch |
| 66 |
from typing import Any |
| 67 |
|
| 68 |
@singledispatch |
| 69 |
def fun(arg: Any) -> str: |
| 70 |
return 'default' |
| 71 |
|
| 72 |
@fun.register |
| 73 |
def _(arg: str) -> str: |
| 74 |
return 'str' |
| 75 |
|
| 76 |
@fun.register |
| 77 |
def _(arg: int) -> str: |
| 78 |
return 'int' |
| 79 |
|
| 80 |
# extra function to make sure all 3 underscore functions aren't treated as one OverloadedFuncDef |
| 81 |
def a(b: Any) -> Any: pass |
| 82 |
|
| 83 |
@fun.register |
| 84 |
def _(arg: list) -> str: |
| 85 |
return 'list' |
| 86 |
|
| 87 |
def test_singledispatch() -> None: |
| 88 |
assert fun(0) == 'int' |
| 89 |
assert fun('a') == 'str' |
| 90 |
assert fun([1, 2]) == 'list' |
| 91 |
assert fun({'a': 'b'}) == 'default' |
| 92 |
|
| 93 |
[case testCanRegisterCompiledClasses] |
| 94 |
from functools import singledispatch |
| 95 |
from typing import Any |
| 96 |
|
| 97 |
class A: pass |
| 98 |
|
| 99 |
@singledispatch |
| 100 |
def fun(arg: Any) -> bool: |
| 101 |
return False |
| 102 |
@fun.register |
| 103 |
def fun_specialized(arg: A) -> bool: |
| 104 |
return True |
| 105 |
|
| 106 |
def test_singledispatch() -> None: |
| 107 |
assert fun(A()) |
| 108 |
assert not fun(1) |
| 109 |
|
| 110 |
[case testTypeUsedAsArgumentToRegister] |
| 111 |
from functools import singledispatch |
| 112 |
from typing import Any |
| 113 |
|
| 114 |
@singledispatch |
| 115 |
def fun(arg: Any) -> bool: |
| 116 |
return False |
| 117 |
|
| 118 |
@fun.register(int) |
| 119 |
def fun_specialized(arg: Any) -> bool: |
| 120 |
return True |
| 121 |
|
| 122 |
def test_singledispatch() -> None: |
| 123 |
assert fun(1) |
| 124 |
assert not fun('a') |
| 125 |
|
| 126 |
[case testUseRegisterAsAFunction] |
| 127 |
from functools import singledispatch |
| 128 |
from typing import Any |
| 129 |
|
| 130 |
@singledispatch |
| 131 |
def fun(arg: Any) -> bool: |
| 132 |
return False |
| 133 |
|
| 134 |
def fun_specialized_impl(arg: Any) -> bool: |
| 135 |
return True |
| 136 |
|
| 137 |
fun.register(int, fun_specialized_impl) |
| 138 |
|
| 139 |
def test_singledispatch() -> None: |
| 140 |
assert fun(0) |
| 141 |
assert not fun('a') |
| 142 |
|
| 143 |
[case testRegisterDoesntChangeFunction] |
| 144 |
from functools import singledispatch |
| 145 |
from typing import Any |
| 146 |
|
| 147 |
@singledispatch |
| 148 |
def fun(arg: Any) -> bool: |
| 149 |
return False |
| 150 |
|
| 151 |
@fun.register(int) |
| 152 |
def fun_specialized(arg: Any) -> bool: |
| 153 |
return True |
| 154 |
|
| 155 |
def test_singledispatch() -> None: |
| 156 |
assert fun_specialized('a') |
| 157 |
|
| 158 |
# TODO: turn this into a mypy error |
| 159 |
[case testNoneIsntATypeWhenUsedAsArgumentToRegister] |
| 160 |
from functools import singledispatch |
| 161 |
from typing import Any |
| 162 |
|
| 163 |
@singledispatch |
| 164 |
def fun(arg: Any) -> bool: |
| 165 |
return False |
| 166 |
|
| 167 |
def test_argument() -> None: |
| 168 |
try: |
| 169 |
@fun.register |
| 170 |
def fun_specialized(arg: None) -> bool: |
| 171 |
return True |
| 172 |
assert False, "expected to raise an exception" |
| 173 |
except TypeError: |
| 174 |
pass |
| 175 |
|
| 176 |
[case testRegisteringTheSameFunctionSeveralTimes] |
| 177 |
from functools import singledispatch |
| 178 |
from typing import Any |
| 179 |
|
| 180 |
@singledispatch |
| 181 |
def fun(arg: Any) -> bool: |
| 182 |
return False |
| 183 |
|
| 184 |
@fun.register(int) |
| 185 |
@fun.register(str) |
| 186 |
def fun_specialized(arg: Any) -> bool: |
| 187 |
return True |
| 188 |
|
| 189 |
def test_singledispatch() -> None: |
| 190 |
assert fun(0) |
| 191 |
assert fun('a') |
| 192 |
assert not fun([1, 2]) |
| 193 |
|
| 194 |
[case testTypeIsAnABC] |
| 195 |
# mypy: allow-untyped-defs |
| 196 |
from functools import singledispatch |
| 197 |
from collections.abc import Mapping |
| 198 |
|
| 199 |
@singledispatch |
| 200 |
def fun(arg) -> bool: |
| 201 |
# TODO: Adding an Any parameter annotation breaks the test case |
| 202 |
return False |
| 203 |
|
| 204 |
@fun.register |
| 205 |
def fun_specialized(arg: Mapping) -> bool: |
| 206 |
return True |
| 207 |
|
| 208 |
def test_singledispatch() -> None: |
| 209 |
assert not fun(1) |
| 210 |
assert fun({'a': 'b'}) |
| 211 |
|
| 212 |
[case testSingleDispatchMethod-xfail] |
| 213 |
from functools import singledispatchmethod |
| 214 |
class A: |
| 215 |
@singledispatchmethod |
| 216 |
def fun(self, arg) -> str: |
| 217 |
return 'default' |
| 218 |
|
| 219 |
@fun.register |
| 220 |
def fun_int(self, arg: int) -> str: |
| 221 |
return 'int' |
| 222 |
|
| 223 |
@fun.register |
| 224 |
def fun_str(self, arg: str) -> str: |
| 225 |
return 'str' |
| 226 |
|
| 227 |
def test_singledispatchmethod() -> None: |
| 228 |
x = A() |
| 229 |
assert x.fun(5) == 'int' |
| 230 |
assert x.fun('a') == 'str' |
| 231 |
assert x.fun([1, 2]) == 'default' |
| 232 |
|
| 233 |
[case testSingleDispatchMethodWithOtherDecorator-xfail] |
| 234 |
from functools import singledispatchmethod |
| 235 |
class A: |
| 236 |
@singledispatchmethod |
| 237 |
@staticmethod |
| 238 |
def fun(arg) -> str: |
| 239 |
return 'default' |
| 240 |
|
| 241 |
@fun.register |
| 242 |
@staticmethod |
| 243 |
def fun_int(arg: int) -> str: |
| 244 |
return 'int' |
| 245 |
|
| 246 |
@fun.register |
| 247 |
@staticmethod |
| 248 |
def fun_str(arg: str) -> str: |
| 249 |
return 'str' |
| 250 |
|
| 251 |
def test_singledispatchmethod() -> None: |
| 252 |
x = A() |
| 253 |
assert x.fun(5) == 'int' |
| 254 |
assert x.fun('a') == 'str' |
| 255 |
assert x.fun([1, 2]) == 'default' |
| 256 |
|
| 257 |
[case testSingledispatchTreeSumAndEqual] |
| 258 |
from functools import singledispatch |
| 259 |
from typing import cast |
| 260 |
|
| 261 |
class Tree: |
| 262 |
pass |
| 263 |
class Leaf(Tree): |
| 264 |
pass |
| 265 |
class Node(Tree): |
| 266 |
def __init__(self, value: int, left: Tree, right: Tree) -> None: |
| 267 |
self.value = value |
| 268 |
self.left = left |
| 269 |
self.right = right |
| 270 |
|
| 271 |
@singledispatch |
| 272 |
def calc_sum(x: Tree) -> int: |
| 273 |
raise TypeError('invalid type for x') |
| 274 |
|
| 275 |
@calc_sum.register |
| 276 |
def _(x: Leaf) -> int: |
| 277 |
return 0 |
| 278 |
|
| 279 |
@calc_sum.register |
| 280 |
def _(x: Node) -> int: |
| 281 |
return x.value + calc_sum(x.left) + calc_sum(x.right) |
| 282 |
|
| 283 |
@singledispatch |
| 284 |
def equal(to_compare: Tree, known: Tree) -> bool: |
| 285 |
raise TypeError('invalid type for x') |
| 286 |
|
| 287 |
@equal.register |
| 288 |
def _(to_compare: Leaf, known: Tree) -> bool: |
| 289 |
return isinstance(known, Leaf) |
| 290 |
|
| 291 |
@equal.register |
| 292 |
def _(to_compare: Node, known: Tree) -> bool: |
| 293 |
if isinstance(known, Node): |
| 294 |
if to_compare.value != known.value: |
| 295 |
return False |
| 296 |
else: |
| 297 |
return equal(to_compare.left, known.left) and equal(to_compare.right, known.right) |
| 298 |
return False |
| 299 |
|
| 300 |
def build(n: int) -> Tree: |
| 301 |
if n == 0: |
| 302 |
return Leaf() |
| 303 |
return Node(n, build(n - 1), build(n - 1)) |
| 304 |
|
| 305 |
def test_sum_and_equal() -> None: |
| 306 |
tree = build(5) |
| 307 |
tree2 = build(5) |
| 308 |
cast(Node, cast(Node, cast(Node, cast(Node, tree2).right).right).right).value = 10 |
| 309 |
assert calc_sum(tree) == 57 |
| 310 |
assert calc_sum(tree2) == 65 |
| 311 |
assert equal(tree, tree) |
| 312 |
assert not equal(tree, tree2) |
| 313 |
tree3 = build(4) |
| 314 |
assert not equal(tree, tree3) |
| 315 |
|
| 316 |
[case testSimulateMypySingledispatch] |
| 317 |
from functools import singledispatch |
| 318 |
from mypy_extensions import trait |
| 319 |
from typing import Iterator, Union, TypeVar, Any, List, Type |
| 320 |
# based on use of singledispatch in stubtest.py |
| 321 |
class Error: |
| 322 |
def __init__(self, msg: str) -> None: |
| 323 |
self.msg = msg |
| 324 |
|
| 325 |
@trait |
| 326 |
class Node: pass |
| 327 |
|
| 328 |
class MypyFile(Node): pass |
| 329 |
class TypeInfo(Node): pass |
| 330 |
|
| 331 |
|
| 332 |
@trait |
| 333 |
class SymbolNode(Node): pass |
| 334 |
@trait |
| 335 |
class Expression(Node): pass |
| 336 |
class TypeVarLikeExpr(SymbolNode, Expression): pass |
| 337 |
class TypeVarExpr(TypeVarLikeExpr): pass |
| 338 |
class TypeAlias(SymbolNode): pass |
| 339 |
|
| 340 |
class Missing: pass |
| 341 |
MISSING = Missing() |
| 342 |
|
| 343 |
T = TypeVar("T") |
| 344 |
|
| 345 |
MaybeMissing = Union[T, Missing] |
| 346 |
|
| 347 |
@singledispatch |
| 348 |
def verify(stub: Node, a: MaybeMissing[Any], b: List[str]) -> Iterator[Error]: |
| 349 |
yield Error('unknown node type') |
| 350 |
|
| 351 |
@verify.register(MypyFile) |
| 352 |
def verify_mypyfile(stub: MypyFile, a: MaybeMissing[int], b: List[str]) -> Iterator[Error]: |
| 353 |
if isinstance(a, Missing): |
| 354 |
yield Error("shouldn't be missing") |
| 355 |
return |
| 356 |
if not isinstance(a, int): |
| 357 |
# this check should be unnecessary because of the type signature and the previous check, |
| 358 |
# but stubtest.py has this check |
| 359 |
yield Error("should be an int") |
| 360 |
return |
| 361 |
yield from verify(TypeInfo(), str, ['abc', 'def']) |
| 362 |
|
| 363 |
@verify.register(TypeInfo) |
| 364 |
def verify_typeinfo(stub: TypeInfo, a: MaybeMissing[Type[Any]], b: List[str]) -> Iterator[Error]: |
| 365 |
yield Error('in TypeInfo') |
| 366 |
yield Error('hello') |
| 367 |
|
| 368 |
@verify.register(TypeVarExpr) |
| 369 |
def verify_typevarexpr(stub: TypeVarExpr, a: MaybeMissing[Any], b: List[str]) -> Iterator[Error]: |
| 370 |
if False: |
| 371 |
yield None |
| 372 |
|
| 373 |
def verify_list(stub: Any, a: Any, b: Any) -> List[str]: |
| 374 |
"""Helper function that converts iterator of errors to list of messages""" |
| 375 |
return list(err.msg for err in verify(stub, a, b)) |
| 376 |
|
| 377 |
def test_verify() -> None: |
| 378 |
assert verify_list(TypeAlias(), 'a', ['a', 'b']) == ['unknown node type'] |
| 379 |
assert verify_list(MypyFile(), MISSING, ['a', 'b']) == ["shouldn't be missing"] |
| 380 |
assert verify_list(MypyFile(), 5, ['a', 'b']) == ['in TypeInfo', 'hello'] |
| 381 |
assert verify_list(TypeInfo(), str, ['a', 'b']) == ['in TypeInfo', 'hello'] |
| 382 |
assert verify_list(TypeVarExpr(), 'a', ['x', 'y']) == [] |
| 383 |
|
| 384 |
|
| 385 |
[case testArgsInRegisteredImplNamedDifferentlyFromMainFunction] |
| 386 |
from functools import singledispatch |
| 387 |
from typing import Any |
| 388 |
|
| 389 |
@singledispatch |
| 390 |
def f(a: Any) -> bool: |
| 391 |
return False |
| 392 |
|
| 393 |
@f.register |
| 394 |
def g(b: int) -> bool: |
| 395 |
return True |
| 396 |
|
| 397 |
def test_singledispatch() -> None: |
| 398 |
assert f(5) |
| 399 |
assert not f('a') |
| 400 |
|
| 401 |
[case testKeywordArguments] |
| 402 |
from functools import singledispatch |
| 403 |
from typing import Any |
| 404 |
|
| 405 |
@singledispatch |
| 406 |
def f(arg: Any, *, kwarg: int = 0) -> int: |
| 407 |
return kwarg + 10 |
| 408 |
|
| 409 |
@f.register |
| 410 |
def g(arg: int, *, kwarg: int = 5) -> int: |
| 411 |
return kwarg - 10 |
| 412 |
|
| 413 |
def test_keywords() -> None: |
| 414 |
assert f('a') == 10 |
| 415 |
assert f('a', kwarg=3) == 13 |
| 416 |
assert f('a', kwarg=7) == 17 |
| 417 |
|
| 418 |
assert f(1) == -5 |
| 419 |
assert f(1, kwarg=4) == -6 |
| 420 |
assert f(1, kwarg=6) == -4 |
| 421 |
|
| 422 |
[case testGeneratorAndMultipleTypesOfIterable] |
| 423 |
from functools import singledispatch |
| 424 |
from typing import * |
| 425 |
|
| 426 |
@singledispatch |
| 427 |
def f(arg: Any) -> Iterable[int]: |
| 428 |
yield 1 |
| 429 |
|
| 430 |
@f.register |
| 431 |
def g(arg: str) -> Iterable[int]: |
| 432 |
return [0] |
| 433 |
|
| 434 |
def test_iterables() -> None: |
| 435 |
assert f(1) != [1] |
| 436 |
assert list(f(1)) == [1] |
| 437 |
assert f('a') == [0] |
| 438 |
|
| 439 |
[case testRegisterUsedAtSameTimeAsOtherDecorators] |
| 440 |
from functools import singledispatch |
| 441 |
from typing import TypeVar, Any |
| 442 |
|
| 443 |
class A: pass |
| 444 |
class B: pass |
| 445 |
|
| 446 |
T = TypeVar('T') |
| 447 |
|
| 448 |
def decorator(f: T) -> T: |
| 449 |
return f |
| 450 |
|
| 451 |
@singledispatch |
| 452 |
def f(arg: Any) -> int: |
| 453 |
return 0 |
| 454 |
|
| 455 |
@f.register |
| 456 |
@decorator |
| 457 |
def h(arg: str) -> int: |
| 458 |
return 2 |
| 459 |
|
| 460 |
def test_singledispatch() -> None: |
| 461 |
assert f(1) == 0 |
| 462 |
assert f('a') == 2 |
| 463 |
|
| 464 |
[case testDecoratorModifiesFunction] |
| 465 |
from functools import singledispatch |
| 466 |
from typing import Callable, Any |
| 467 |
|
| 468 |
class A: pass |
| 469 |
|
| 470 |
def decorator(f: Callable[[Any], int]) -> Callable[[Any], int]: |
| 471 |
def wrapper(x: Any) -> int: |
| 472 |
return f(x) * 7 |
| 473 |
return wrapper |
| 474 |
|
| 475 |
@singledispatch |
| 476 |
def f(arg: Any) -> int: |
| 477 |
return 10 |
| 478 |
|
| 479 |
@f.register |
| 480 |
@decorator |
| 481 |
def h(arg: str) -> int: |
| 482 |
return 5 |
| 483 |
|
| 484 |
|
| 485 |
def test_singledispatch() -> None: |
| 486 |
assert f('a') == 35 |
| 487 |
assert f(A()) == 10 |
| 488 |
|
| 489 |
[case testMoreSpecificTypeBeforeLessSpecificType] |
| 490 |
from functools import singledispatch |
| 491 |
from typing import Any |
| 492 |
|
| 493 |
class A: pass |
| 494 |
class B(A): pass |
| 495 |
|
| 496 |
@singledispatch |
| 497 |
def f(arg: Any) -> str: |
| 498 |
return 'default' |
| 499 |
|
| 500 |
@f.register |
| 501 |
def g(arg: B) -> str: |
| 502 |
return 'b' |
| 503 |
|
| 504 |
@f.register |
| 505 |
def h(arg: A) -> str: |
| 506 |
return 'a' |
| 507 |
|
| 508 |
def test_singledispatch() -> None: |
| 509 |
assert f(B()) == 'b' |
| 510 |
assert f(A()) == 'a' |
| 511 |
assert f(5) == 'default' |
| 512 |
|
| 513 |
[case testMultipleRelatedClassesBeingRegistered] |
| 514 |
from functools import singledispatch |
| 515 |
from typing import Any |
| 516 |
|
| 517 |
class A: pass |
| 518 |
class B(A): pass |
| 519 |
class C(B): pass |
| 520 |
|
| 521 |
@singledispatch |
| 522 |
def f(arg: Any) -> str: return 'default' |
| 523 |
|
| 524 |
@f.register |
| 525 |
def _(arg: A) -> str: return 'a' |
| 526 |
|
| 527 |
@f.register |
| 528 |
def _(arg: C) -> str: return 'c' |
| 529 |
|
| 530 |
@f.register |
| 531 |
def _(arg: B) -> str: return 'b' |
| 532 |
|
| 533 |
def test_singledispatch() -> None: |
| 534 |
assert f(A()) == 'a' |
| 535 |
assert f(B()) == 'b' |
| 536 |
assert f(C()) == 'c' |
| 537 |
assert f(1) == 'default' |
| 538 |
|
| 539 |
[case testRegisteredImplementationsInDifferentFiles] |
| 540 |
from other_a import f, A, B, C |
| 541 |
@f.register |
| 542 |
def a(arg: A) -> int: |
| 543 |
return 2 |
| 544 |
|
| 545 |
@f.register |
| 546 |
def _(arg: C) -> int: |
| 547 |
return 3 |
| 548 |
|
| 549 |
def test_singledispatch() -> None: |
| 550 |
assert f(B()) == 1 |
| 551 |
assert f(A()) == 2 |
| 552 |
assert f(C()) == 3 |
| 553 |
assert f(1) == 0 |
| 554 |
|
| 555 |
[file other_a.py] |
| 556 |
from functools import singledispatch |
| 557 |
|
| 558 |
class A: pass |
| 559 |
class B(A): pass |
| 560 |
class C(B): pass |
| 561 |
|
| 562 |
@singledispatch |
| 563 |
def f(arg: object) -> int: |
| 564 |
return 0 |
| 565 |
|
| 566 |
@f.register |
| 567 |
def g(arg: B) -> int: |
| 568 |
return 1 |
| 569 |
|
| 570 |
[case testOrderCanOnlyBeDeterminedFromMRONotIsinstanceChecks] |
| 571 |
from mypy_extensions import trait |
| 572 |
from functools import singledispatch |
| 573 |
from typing import Any |
| 574 |
|
| 575 |
@trait |
| 576 |
class A: pass |
| 577 |
@trait |
| 578 |
class B: pass |
| 579 |
class AB(A, B): pass |
| 580 |
class BA(B, A): pass |
| 581 |
|
| 582 |
@singledispatch |
| 583 |
def f(arg: Any) -> str: |
| 584 |
return "default" |
| 585 |
|
| 586 |
@f.register |
| 587 |
def fa(arg: A) -> str: |
| 588 |
return "a" |
| 589 |
|
| 590 |
@f.register |
| 591 |
def fb(arg: B) -> str: |
| 592 |
return "b" |
| 593 |
|
| 594 |
def test_singledispatch() -> None: |
| 595 |
assert f(AB()) == "a" |
| 596 |
assert f(BA()) == "b" |
| 597 |
|
| 598 |
[case testCallingFunctionBeforeAllImplementationsRegistered] |
| 599 |
from functools import singledispatch |
| 600 |
from typing import Any |
| 601 |
|
| 602 |
class A: pass |
| 603 |
class B(A): pass |
| 604 |
|
| 605 |
@singledispatch |
| 606 |
def f(arg: Any) -> str: |
| 607 |
return 'default' |
| 608 |
|
| 609 |
assert f(A()) == 'default' |
| 610 |
assert f(B()) == 'default' |
| 611 |
assert f(1) == 'default' |
| 612 |
|
| 613 |
@f.register |
| 614 |
def g(arg: A) -> str: |
| 615 |
return 'a' |
| 616 |
|
| 617 |
assert f(A()) == 'a' |
| 618 |
assert f(B()) == 'a' |
| 619 |
assert f(1) == 'default' |
| 620 |
|
| 621 |
@f.register |
| 622 |
def _(arg: B) -> str: |
| 623 |
return 'b' |
| 624 |
|
| 625 |
# TODO: Move whole testcase to a function when mypyc#1118 is fixed. |
| 626 |
def test_final() -> None: |
| 627 |
assert f(A()) == 'a' |
| 628 |
assert f(B()) == 'b' |
| 629 |
assert f(1) == 'default' |
| 630 |
|
| 631 |
|
| 632 |
[case testDynamicallyRegisteringFunctionFromInterpretedCode] |
| 633 |
from functools import singledispatch |
| 634 |
from typing import Any |
| 635 |
|
| 636 |
class A: pass |
| 637 |
class B(A): pass |
| 638 |
class C(B): pass |
| 639 |
class D(C): pass |
| 640 |
|
| 641 |
@singledispatch |
| 642 |
def f(arg: Any) -> str: |
| 643 |
return "default" |
| 644 |
|
| 645 |
@f.register |
| 646 |
def _(arg: B) -> str: |
| 647 |
return 'b' |
| 648 |
|
| 649 |
[file register_impl.py] |
| 650 |
from native import f, A, B, C |
| 651 |
|
| 652 |
@f.register(A) |
| 653 |
def a(arg) -> str: |
| 654 |
return 'a' |
| 655 |
|
| 656 |
@f.register |
| 657 |
def c(arg: C) -> str: |
| 658 |
return 'c' |
| 659 |
|
| 660 |
[file driver.py] |
| 661 |
from native import f, A, B, C |
| 662 |
from register_impl import a, c |
| 663 |
# We need a custom driver here because register_impl has to be run before we test this (so that the |
| 664 |
# additional implementations are registered) |
| 665 |
assert f(C()) == 'c' |
| 666 |
assert f(A()) == 'a' |
| 667 |
assert f(B()) == 'b' |
| 668 |
assert a(C()) == 'a' |
| 669 |
assert c(A()) == 'c' |
| 670 |
|
| 671 |
[case testMalformedDynamicRegisterCall] |
| 672 |
from functools import singledispatch |
| 673 |
from typing import Any |
| 674 |
|
| 675 |
@singledispatch |
| 676 |
def f(arg: Any) -> None: |
| 677 |
pass |
| 678 |
[file register.py] |
| 679 |
from native import f |
| 680 |
from testutil import assertRaises |
| 681 |
|
| 682 |
with assertRaises(TypeError, 'Invalid first argument to `register()`'): |
| 683 |
@f.register |
| 684 |
def _(): |
| 685 |
pass |
| 686 |
|
| 687 |
[file driver.py] |
| 688 |
import register |
| 689 |
|
| 690 |
[case testCacheClearedWhenNewFunctionRegistered] |
| 691 |
from functools import singledispatch |
| 692 |
|
| 693 |
@singledispatch |
| 694 |
def f(arg: object) -> str: |
| 695 |
return 'default' |
| 696 |
|
| 697 |
[file register.py] |
| 698 |
from native import f |
| 699 |
class A: pass |
| 700 |
class B: pass |
| 701 |
class C: pass |
| 702 |
|
| 703 |
# annotated function |
| 704 |
assert f(A()) == 'default' |
| 705 |
@f.register |
| 706 |
def _(arg: A) -> str: |
| 707 |
return 'a' |
| 708 |
assert f(A()) == 'a' |
| 709 |
|
| 710 |
# type passed as argument |
| 711 |
assert f(B()) == 'default' |
| 712 |
@f.register(B) |
| 713 |
def _(arg: B) -> str: |
| 714 |
return 'b' |
| 715 |
assert f(B()) == 'b' |
| 716 |
|
| 717 |
# 2 argument form |
| 718 |
assert f(C()) == 'default' |
| 719 |
def c(arg) -> str: |
| 720 |
return 'c' |
| 721 |
f.register(C, c) |
| 722 |
assert f(C()) == 'c' |
| 723 |
|
| 724 |
|
| 725 |
[file driver.py] |
| 726 |
import register |