From e2170a79f343f04d67d0bd704c668d74c452807a Mon Sep 17 00:00:00 2001 From: ken-morel Date: Sun, 9 Jun 2024 08:31:18 +0100 Subject: [PATCH] ss --- src/pyoload/__init__.py | 36 ++++++++++++++++++------------------ src/tests/logs.yaml | 14 +++++++------- src/tests/test_annotate.py | 16 +++++++++++++--- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/pyoload/__init__.py b/src/pyoload/__init__.py index f522072..19c2d11 100644 --- a/src/pyoload/__init__.py +++ b/src/pyoload/__init__.py @@ -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: @@ -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( @@ -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( diff --git a/src/tests/logs.yaml b/src/tests/logs.yaml index 9c71659..00bd303 100644 --- a/src/tests/logs.yaml +++ b/src/tests/logs.yaml @@ -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 diff --git a/src/tests/test_annotate.py b/src/tests/test_annotate.py index 45508e4..945f665 100644 --- a/src/tests/test_annotate.py +++ b/src/tests/test_annotate.py @@ -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(): @@ -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()