Skip to content

Commit

Permalink
ss
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-morel committed Jun 9, 2024
1 parent 7d81ccb commit e2170a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
36 changes: 18 additions & 18 deletions src/pyoload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
from inspect import _empty
from inspect import getmodule
from inspect import isclass
from inspect import isfunction
from inspect import ismethod
from inspect import signature
from typing import get_origin
from typing import get_args
from typing import Any
from typing import Callable
from typing import GenericAlias
from typing import Union
from typing import Type
from typing import Union
from typing import get_args
from typing import get_origin

NoneType = type(None)
try:
Expand Down Expand Up @@ -648,14 +650,25 @@ def resove_annotations(obj: Callable) -> None:
raise AnnotationResolutionError(
f"object {obj=!r} does not have `.__annotations__`",
)
if isclass(obj) or hasattr(obj, "__class__"):
if isfunction(obj):
for k, v in obj.__annotations__.items():
if isinstance(v, str):
try:
obj.__annotations__[k] = eval(v, obj.__globals__)
except Exception as e:
raise AnnotationResolutionError(
f"Exception: {k!s} while resolving"
f" annotation {v!r} of function {obj!r}",
f"globals: {obj.__globals__}",
) from e
elif isclass(obj) or hasattr(obj, "__class__"):
for k, v in obj.__annotations__.items():
if isinstance(v, str):
try:
obj.__annotations__[k] = eval(
v,
dict(vars(getmodule(obj))),
dict(vars(obj)),
dict(vars(obj)) if hasattr(obj, '__dict__') else None,
)
except Exception as e:
raise AnnotationResolutionError(
Expand All @@ -664,19 +677,6 @@ def resove_annotations(obj: Callable) -> None:
f" annotation {e}={v!r} of object {obj!r}"
),
) from e
elif callable(obj):
for k, v in obj.__annotations__.items():
if isinstance(v, str):
try:
obj.__annotations__[k] = eval(v, obj.__globals__)
except Exception as e:
raise AnnotationResolutionError(
f"Exception: {k!s} while resolving"
f" annotation {v!r} of function {obj!r}",
f"globals: {obj.__globals__}",
) from e
else:
raise AnnotationResolutionError(f"unknown resolution method for {obj}")


def annotate(
Expand Down
14 changes: 7 additions & 7 deletions src/tests/logs.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type_match vs isinstance on int:True 1.3719336ms
type_match vs isinstance on int:False 1.0354459999999999ms
type_match on dict[str, int]*50:True 267.45216439999996ms
type_match on dict[str, int]*50:False 17.6830167ms
Cast str->int: 10.8046044ms
Cast complex->int | str: 0.51518408ms
Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 12.7969466ms
type_match vs isinstance on int:True 1.5289682ms
type_match vs isinstance on int:False 1.9831642ms
type_match on dict[str, int]*50:True 267.711404ms
type_match on dict[str, int]*50:False 17.3794271ms
Cast str->int: 10.582962299999998ms
Cast complex->int | str: 0.50979833ms
Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 12.46998271ms
16 changes: 13 additions & 3 deletions src/tests/test_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def foo1(a: Cast(str)):
@annotate
class MyCLass:
__slots__ = ('a', 'b')
a: int
b: str
a: 'int'
b: 'str'


def test_annotate():
Expand All @@ -53,7 +53,17 @@ def footy(a: 'Nothing here'):
pass

footy(2)
except Exception:
except AnnotationResolutionError:
pass
else:
raise Exception()
try:
@annotate
class Footy:
a: 'Nothing here'

Footy().a = 4
except AnnotationResolutionError:
pass
else:
raise Exception()
Expand Down

0 comments on commit e2170a7

Please sign in to comment.