Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a couple of tests #60

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ def test_type_generic(self):
assert issubclass(typing.Type[typing.List[int]], typing.Type[typing.Sequence[int]])
assert not issubclass(typing.Type[typing.List[int]], typing.Type[typing.Sequence[str]])

assert issubclass(typing.Type[int], typing.Type[object])
assert issubclass(typing.Type[int], typing.Type[typing.Any])
assert not issubclass(typing.Type[object], typing.Type[int])
assert issubclass(typing.Type[typing.Any], typing.Type[int])

def test_any(self):
assert is_subtype(int, Any)
assert is_subtype(Any, int)
Expand Down Expand Up @@ -863,6 +868,15 @@ class A:
self.assertRaises(TypeError, A)
self.assertRaises(TypeError, A, 2)

def test_self_reference(self):
@dataclass
class A:
items: List["A"]

a1 = A([])
a2 = A([a1])
self.assertRaises(TypeError, A, [1])

def test_forward_references(self):
@dataclass
class A:
Expand Down
Loading
Loading