defl/tests.old/test_checkType.py

101 lines
3.1 KiB
Python
Raw Normal View History

2024-09-11 11:14:03 -04:00
#!/usr/bin/env python3
import defl
from defl import log, cl, Assert
from defl.testing_ import Tester, Test, TestState, testFail
import dataclasses, contextlib
import itertools
import inspect
from typing import *
from types import *
import typing
from defl._typeCheck_ import *
from defl._dataclass_ import *
tester = Tester(name=__file__)
@tester.add()
def test():
a = TypeCompose.FromType(list[str])
Assert(a.origin).eq(list)
Assert(a.args).eq([TypeCompose(origin=str)])
Assert(a.supertype).eq(Undefined)
Assert(a.dominent()).eq(list)
a = TypeCompose.FromType(list)
Assert(a.origin).eq(list)
Assert(a.args).eq(Undefined)
Assert(a.supertype).eq(Undefined)
Assert(a.dominent()).eq(list)
a = TypeCompose.FromType(list[None])
Assert(a.origin).eq(list)
Assert(a.args).eq([TypeCompose(origin=None)])
Assert(a.supertype).eq(Undefined)
Assert(a.dominent()).eq(list)
a = TypeCompose.FromType(list[None] | int)
Assert(a.origin).eq(Undefined)
Assert(a.args).eq([TypeCompose(origin=list, args=[TypeCompose(origin=None)]), TypeCompose(origin=int)])
Assert(a.supertype).eq(Undefined)
Assert(a.dominent()).eq(list)
a = TypeCompose.FromType(dict[list[int] | int])
Assert(a.origin).eq(dict)
Assert(a.args).eq([
TypeCompose(
args=[TypeCompose(origin=list, args=[TypeCompose(origin=int)]),
TypeCompose(origin=int)]
)
])
Assert(a.supertype).eq(Undefined)
Assert(a.dominent()).eq(dict)
@dataclass(slots=True, kw_only=True, frozen=False)
class DEFAULT_TRUE:
...
@dataclass(slots=True, kw_only=True, frozen=False)
class A:
...
@dataclass(slots=True, kw_only=True, frozen=False)
class B(A):
...
@tester.add()
def stdlib():
# https://docs.python.org/3/library/stdtypes.html#types-genericalias
assert list[int] == GenericAlias(list, (int, ))
assert dict[str, int] == GenericAlias(dict, (str, int))
assert type(re.compile(r'.')) != typing.Pattern
assert type(re.compile(r'.')) == re.Pattern
assert isinstance(re.compile(r'.'), typing.Pattern)
assert isinstance(re.compile(r'.'), re.Pattern)
t = list[str]
t([1, 2, 3])
@tester.add()
def test():
assert checkType(1, int)
assert checkType({1: 1}, dict[int, int])
assert not checkType({1: 1}, dict[str, int])
assert checkType({1: 1}, dict[str | int, int])
assert checkType({1: {'a': 1}, 'a': {}}, dict[str | int, dict[str | int, int]])
assert not checkType({1: {'a': 1}, 'a': {'a': 'a'}}, dict[str | int, dict[str | int, int]])
assert checkType({1: {'a': 1}, 'a': {'a': 1}}, dict[str | int, dict[str | int, int]])
assert checkType(A(), A)
assert checkType(B(), A)
assert checkType(B(), A | B)
assert checkType(B(), B)
@tester.add()
def test():
assert checkType({'a': [1, 2, 3]}, Mapping[Any, Iterable] | Iterable[Iterable], _debug=True)
assert checkType([[1], [2]], Mapping[Any, Iterable] | Iterable[Iterable], _debug=True)
assert not checkType([[1], 2], Mapping[Any, Iterable] | Iterable[Iterable], _debug=True)
log.info(tester.run())
tester.exitWithStatus()