Skip to content

Commit

Permalink
Change to evaluate ForwardRefs use typing._eval_type
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Jun 19, 2024
1 parent f6ab204 commit 013657b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 18 deletions.
6 changes: 2 additions & 4 deletions runtype/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from copy import copy
import dataclasses
import typing
from typing import Union, Any, Callable, TypeVar, Dict, Optional, overload
from typing import TYPE_CHECKING, ClassVar, Type
from typing import Union, Callable, TypeVar, Dict, Optional, overload
from typing import TYPE_CHECKING, Type, ForwardRef
from abc import ABC, abstractmethod
import inspect
import types
Expand All @@ -16,12 +16,10 @@
if TYPE_CHECKING:
from typing_extensions import dataclass_transform
else:

def dataclass_transform(*a, **kw):
return lambda f: f


from .utils import ForwardRef
from .common import CHECK_TYPES
from .validation import TypeMismatchError, ensure_isa as default_ensure_isa
from .pytypes import TypeCaster, SumType, NoneType, ATypeCaster, PythonType, type_caster
Expand Down
13 changes: 2 additions & 11 deletions runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,11 @@
from datetime import datetime, date, time, timedelta
from types import FrameType

from .utils import ForwardRef
from .base_types import DataType, Validator, TypeMismatchError, dp, Variance
from . import base_types
from . import datetime_parse


if sys.version_info < (3, 9):
_orig_eval = ForwardRef._evaluate

def _forwardref_evaluate(self, glob, loc, _):
return _orig_eval(self, glob, loc)
else:
_forwardref_evaluate = ForwardRef._evaluate

try:
import typing_extensions
except ImportError:
Expand Down Expand Up @@ -526,10 +517,10 @@ def _to_canon(self, t):
if isinstance(t, (base_types.Type, Validator)):
return t

if isinstance(t, ForwardRef):
if isinstance(t, typing.ForwardRef):
if self.frame is None:
raise RuntimeError("Cannot resolve ForwardRef: TypeCaster initialized without a frame")
t = _forwardref_evaluate(t, self.frame.f_globals, self.frame.f_locals, frozenset())
t = typing._eval_type(t, self.frame.f_globals, self.frame.f_locals)

if isinstance(t, tuple):
return SumType.create([to_canon(x) for x in t])
Expand Down
3 changes: 0 additions & 3 deletions runtype/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import inspect
import sys
import contextvars
from contextlib import contextmanager

from typing import ForwardRef as ForwardRef

def get_func_signatures(typesystem, f):
sig = inspect.signature(f)
typesigs = []
Expand Down

0 comments on commit 013657b

Please sign in to comment.