Skip to content

Commit

Permalink
Bugfix in dispatch: Now using qualified name, so methods with same si…
Browse files Browse the repository at this point in the history
…gnature won't collide
  • Loading branch information
erezsh committed Aug 22, 2024
1 parent 1e7d6bd commit 4f4c0ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion runtype/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 | tuple):
...

class B:
@md
def a(self, points: list | tuple):
...


class TestDataclass(TestCase):
def setUp(self):
Expand Down

0 comments on commit 4f4c0ae

Please sign in to comment.