Skip to content

Commit

Permalink
Safely handle C-defined functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wesselb committed Jun 5, 2022
1 parent 1f8b1ef commit e335f4c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plum/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def extract_signature(f, get_type_hints=False):
tuple[:class:`.signature.Signature`, ptype]: Signature and return type of the
function.
"""
if get_type_hints:
if get_type_hints and hasattr(f, "__annotations__"):
# Unquote type hints so that they are resolved to the right types.
f.__annotations__ = {k: unquote(v) for k, v in f.__annotations__.items()}
f.__annotations__ = typing.get_type_hints(f)
Expand Down
18 changes: 18 additions & 0 deletions tests/dispatcher/test_future_annotations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import Union
import math

import pytest

Expand Down Expand Up @@ -54,3 +55,20 @@ def f(x: str):

assert f(1) == "int"
assert f("1") == "str"


def test_extension_c():
dispatch = Dispatcher()

@dispatch
def f(x: int):
return x

assert f(1) == 1
with pytest.raises(NotFoundLookupError):
f(4.0)

f.dispatch(math.sqrt)

assert f(1) == 1
assert f(4.0) == 2

0 comments on commit e335f4c

Please sign in to comment.