From 0780925d1b19b7767174a7f6b59c84e1f9bbea48 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Fri, 23 Aug 2024 00:12:47 +0200 Subject: [PATCH] Bugfix in dispatch: Now using qualified name, so methods with same signature won't collide --- README.md | 13 +++++++------ runtype/dispatch.py | 2 +- tests/test_basic.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 02320cc..046a972 100644 --- a/README.md +++ b/README.md @@ -121,20 +121,21 @@ assert mul([1, 2], [3, 4]) == [3, 8] # list, list Dispatch can also be used for extending the dataclass builtin `__init__`: ```python -@dataclass(frozen=False) +@dataclass class Point: x: int = 0 y: int = 0 - + @md def __init__(self, points: list | tuple): - self.x, self.y = points + # Call default constructor + self.__init__(*points) @md def __init__(self, points: dict): - self.x = points['x'] - self.y = points['y'] - + # Call default constructor + self.__init__(points['x'], points['y']) + # Test constructors p0 = Point() # Default constructor assert p0 == Point(0, 0) # Default constructor diff --git a/runtype/dispatch.py b/runtype/dispatch.py index 2472ee1..7528f5a 100644 --- a/runtype/dispatch.py +++ b/runtype/dispatch.py @@ -50,7 +50,7 @@ def __call__(self, func=None, *, priority=None): "Must either provide a function to decorate, or set a priority" ) - fname = func.__name__ + fname = func.__qualname__ try: tree = self.fname_to_tree[fname] except KeyError: diff --git a/tests/test_basic.py b/tests/test_basic.py index 03b923d..91e2eb2 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -704,6 +704,19 @@ def f(t: int): assert f(2) == "int" assert f(None) == "none" + def test_qualified_name(self): + md = Dispatch() + + class A: + @md + def a(self, points: list): + ... + + class B: + @md + def a(self, points: list): + ... + class TestDataclass(TestCase): def setUp(self):