Skip to content

Commit

Permalink
Fix annotated #123 (#126)
Browse files Browse the repository at this point in the history
* added failing test case for annotated

* Fix issues with Annotated

* fix failing CI checks

* cleaned up pr

---------

Co-authored-by: Samuel Hodges <[email protected]>
Co-authored-by: Wessel <[email protected]>
  • Loading branch information
3 people authored Jan 21, 2024
1 parent 68f5110 commit 9b42fae
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion plum/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import typing
from functools import wraps
from typing import get_args
from .plum_typing import get_args

__all__ = ["activate_union_aliases", "deactivate_union_aliases", "set_union_alias"]

Expand Down
22 changes: 22 additions & 0 deletions plum/plum_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys
from typing import Literal

if sys.version_info < (3, 9):
import typing_extensions

get_type_hints = typing_extensions.get_type_hints
get_origin = typing_extensions.get_origin
get_args = typing_extensions.get_args

def is_literal(x):
return x == Literal and not isinstance(x, typing_extensions.Annotated)

else:
import typing

get_type_hints = typing.get_type_hints
get_origin = typing.get_origin
get_args = typing.get_args

def is_literal(x):
return x == Literal
5 changes: 3 additions & 2 deletions plum/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from . import _is_bearable
from .repr import repr_short, rich_repr
from .type import is_faithful, resolve_type_hint
from .util import Comparable, Missing, TypeHint, multihash, wrap_lambda
from .plum_typing import get_type_hints
from .util import Comparable, Missing, TypeHint, multihash, repr_short, wrap_lambda

__all__ = ["Signature", "append_default_args"]

Expand Down Expand Up @@ -285,7 +286,7 @@ def resolve_pep563(f: Callable):
beartype_resolve_pep563(f) # This mutates `f`.
# Override the `__annotations__` attribute, since `resolve_pep563` modifies
# `f` too.
for k, v in typing.get_type_hints(f).items():
for k, v in get_type_hints(f, include_extras=True).items():
f.__annotations__[k] = v


Expand Down
12 changes: 8 additions & 4 deletions plum/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import sys
import typing
import warnings
from typing import Literal, get_args, get_origin
from .plum_typing import get_args, get_origin, is_literal

from beartype.vale._core._valecore import BeartypeValidator

try: # pragma: specific no cover 3.8 3.9
from types import UnionType
Expand Down Expand Up @@ -128,7 +130,8 @@ def _is_hint(x):
return x.__module__ in {
"types", # E.g., `tuple[int]`
"typing",
"collections.abc", # E.g., `Callable`
"collections.abc", # E.g., `Callable`,
"typing_extensions",
}
except AttributeError:
return False
Expand Down Expand Up @@ -183,7 +186,7 @@ def resolve_type_hint(x):
return y
else:
# Do not resolve the arguments for `Literal`s.
if origin != Literal:
if not is_literal(origin):
args = resolve_type_hint(args)
try:
return origin[args]
Expand Down Expand Up @@ -218,7 +221,8 @@ def resolve_type_hint(x):
return resolve_type_hint(x.resolve())
else:
return x

elif isinstance(x, BeartypeValidator):
return x
else:
warnings.warn(
f"Could not resolve the type hint of `{x}`. "
Expand Down
29 changes: 29 additions & 0 deletions tests/advanced/test_annotated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys

if sys.version_info < (3, 9):
from typing_extensions import Annotated
else:
from typing import Annotated

import pytest
from beartype.vale import Is

from plum import Dispatcher, NotFoundLookupError


def test_simple_annotated():
dispatch = Dispatcher()

positive_int = Annotated[int, Is[lambda value: value > 0]]

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

assert f(1) == 1

with pytest.raises(NotFoundLookupError):
f("my string")

with pytest.raises(NotFoundLookupError):
f(-1)

0 comments on commit 9b42fae

Please sign in to comment.