Skip to content

Commit

Permalink
Fix bug in unquote
Browse files Browse the repository at this point in the history
  • Loading branch information
wesselb committed Jun 5, 2022
1 parent e335f4c commit 944aca0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions plum/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions tests/dispatcher/test_future_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 944aca0

Please sign in to comment.