From 944aca0198f6c3bba0c6029be9efb9e2d0cfe47b Mon Sep 17 00:00:00 2001 From: Wessel Bruinsma Date: Sun, 5 Jun 2022 16:44:56 +0200 Subject: [PATCH] Fix bug in `unquote` --- plum/util.py | 8 ++++++-- tests/dispatcher/test_future_annotations.py | 12 ++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/plum/util.py b/plum/util.py index 6c53f43a..9e8f51f8 100644 --- a/plum/util.py +++ b/plum/util.py @@ -136,12 +136,16 @@ def check_future_annotations(frame=None): def unquote(x): """Remove quotes from a string at the outermost level. + If `x` is not a string, `x` is returned immediately. + Args: - x (str): String to remove quotes from. + x (str or object): String to remove quotes from. Return: - str: `x` but without quotes. + str or object: `x` but without quotes. """ + if not isinstance(x, str): + return x while len(x) >= 2 and x[0] == x[-1] and x[0] in {'"', "'"}: x = x[1:-1] return x diff --git a/tests/dispatcher/test_future_annotations.py b/tests/dispatcher/test_future_annotations.py index c753445b..f40aeed4 100644 --- a/tests/dispatcher/test_future_annotations.py +++ b/tests/dispatcher/test_future_annotations.py @@ -49,10 +49,18 @@ def f(x: int): with pytest.raises(NotFoundLookupError): f("1") - @f.dispatch - def f(x: str): + def g(x: str): return "str" + f.dispatch(g) + + assert f(1) == "int" + assert f("1") == "str" + + # Extending `f` again will cause `g`s type hints to be processed again, which should + # now be types rather than strings. We check that this also works. + f.dispatch(g) + assert f(1) == "int" assert f("1") == "str"