62 lines
1.4 KiB
Python
62 lines
1.4 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
|
||
|
|
from dataclasses import dataclass, field, KW_ONLY
|
||
|
|
import sys
|
||
|
|
import re
|
||
|
|
import os
|
||
|
|
import enum
|
||
|
|
import itertools
|
||
|
|
from functools import partial, partialmethod, singledispatch
|
||
|
|
from time import sleep
|
||
|
|
from operator import itemgetter
|
||
|
|
import defl
|
||
|
|
from defl import log, cl, Path
|
||
|
|
from defl import CLIError
|
||
|
|
from defl._typing_ import *
|
||
|
|
from defl._typing_ import false, true, none
|
||
|
|
from defl.testing_ import Tester, Test, TestState
|
||
|
|
|
||
|
|
tester = Tester(name=__file__)
|
||
|
|
|
||
|
|
######################################################
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass(slots=True, kw_only=True, frozen=False)
|
||
|
|
class A:
|
||
|
|
# THIS DOESNT WORK
|
||
|
|
# https://stackoverflow.com/questions/24601722/how-can-i-use-functools-singledispatch-with-instance-methods
|
||
|
|
# fine for a regular function, but not much use for an instance method, whose first argument is always going to be self.
|
||
|
|
data: list[int] = field(default_factory=list)
|
||
|
|
|
||
|
|
@singledispatch
|
||
|
|
def over(_, a: int):
|
||
|
|
_.data.append(1)
|
||
|
|
|
||
|
|
@over.register
|
||
|
|
def _(_, a: float):
|
||
|
|
_.data.append(2)
|
||
|
|
|
||
|
|
@over.register
|
||
|
|
def _(_, a: str):
|
||
|
|
_.data.append(3)
|
||
|
|
|
||
|
|
@over.register
|
||
|
|
def _(_, a: Any):
|
||
|
|
_.data.append(4)
|
||
|
|
|
||
|
|
@over.register
|
||
|
|
def _(_, a: Any, b):
|
||
|
|
_.data.append(5)
|
||
|
|
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def test1():
|
||
|
|
a = A()
|
||
|
|
a.over(int(1))
|
||
|
|
a.over(float(1.2))
|
||
|
|
assert a.data == [1, 1], a.data
|
||
|
|
|
||
|
|
|
||
|
|
log.info(tester.run())
|
||
|
|
tester.exitWithStatus()
|