Пример #1
0
    def test_advanced_isinstance_to_generic_class_bound(self):
        TB = typing.TypeVar("TB", bound=BaseClass1)

        class GenericClassBound(typing.Generic[TB]):
            pass

        assert advanced_isinstance(GenericClassBound[DerivedClass1](), GenericClassBound[TB])
        assert advanced_isinstance(GenericClassBound[DerivedClass1](), GenericClassBound)
Пример #2
0
 def test_advanced_isinstance_to_tuples(self):
     assert advanced_isinstance((DerivedClass1(),), Tuple[BaseClass1])
     assert advanced_isinstance((DerivedClass1(),), tuple)
     assert advanced_isinstance((DerivedClass1(), DerivedClass2()), Tuple[BaseClass1, BaseClass2])
     assert advanced_isinstance((DerivedClass1(), BaseClass2()), Tuple[BaseClass1, BaseClass2])
     assert not advanced_isinstance((BaseClass1(), BaseClass2()), Tuple[DerivedClass1, DerivedClass2])
     assert not advanced_isinstance((BaseClass1(), BaseClass2()), Tuple[DerivedClass1, BaseClass2])
Пример #3
0
    def test_advanced_isinstance_to_generic_class_constraint(self):
        TC = typing.TypeVar("TC", BaseClass1, int)

        class GenericClassConstraint(typing.Generic[TC]):
            pass

        assert advanced_isinstance(GenericClassConstraint[BaseClass1](), GenericClassConstraint[TC])
        # Different from can_substitute due to type erasure!
        assert advanced_isinstance(GenericClassConstraint[DerivedClass1](), GenericClassConstraint[TC])
        assert not advanced_isinstance(GenericClassConstraint[TC](), GenericClassConstraint[DerivedClass1])
        assert advanced_isinstance(GenericClassConstraint[BaseClass1](), GenericClassConstraint)
        # Different from can_substitute due to type erasure!
        assert advanced_isinstance(GenericClassConstraint[DerivedClass1](), GenericClassConstraint)
        assert not advanced_isinstance(GenericClassConstraint(), GenericClassConstraint[DerivedClass1])
Пример #4
0
    def test_advanced_isinstance_to_callable_callable(self):
        def _fun1(arg: BaseClass1) -> BaseClass2:
            raise NotImplementedError()

        assert advanced_isinstance(
            _fun1,
            typing.Callable[[DerivedClass1], BaseClass2]
        )

        def _fun2(arg: DerivedClass1) -> BaseClass2:
            raise NotImplementedError()

        assert not advanced_isinstance(
            _fun2,
            typing.Callable[[BaseClass1], BaseClass2]
        )

        def _fun3(arg: BaseClass1) -> DerivedClass2:
            raise NotImplementedError()

        assert advanced_isinstance(
            _fun3,
            typing.Callable[[BaseClass1], BaseClass2]
        )

        def _fun3(arg: BaseClass1) -> BaseClass2:
            raise NotImplementedError()

        assert not advanced_isinstance(
            _fun3,
            typing.Callable[[BaseClass1], DerivedClass2]
        )

        def _fun4(arg: BaseClass1) -> DerivedClass2:
            raise NotImplementedError()

        assert advanced_isinstance(
            _fun4,
            typing.Callable[[DerivedClass1], BaseClass2]
        )

        def _fun5(arg: DerivedClass1) -> BaseClass2:
            raise NotImplementedError()

        assert not advanced_isinstance(
            _fun5,
            typing.Callable[[BaseClass1], DerivedClass2]
        )
Пример #5
0
 def test_advanced_isinstance_to_callable_class(self):
     assert advanced_isinstance(CallableClass(), typing.Callable[[str, float], int])
Пример #6
0
 def test_advanced_isinstance_to_optional(self):
     assert advanced_isinstance(DerivedClass1(), Optional[DerivedClass1])
     assert advanced_isinstance(DerivedClass1(), Optional[BaseClass1])
Пример #7
0
 def test_advanced_isinstance_to_generic_subclass(self):
     print(type(DerivedGenericClass[str, int]()).__bases__)
     assert advanced_isinstance(DerivedGenericClass[str, int](), GenericClass[int])
     # Python erases type information by default.
     # TODO: Handle @generic_aware classes.
     assert advanced_isinstance(DerivedGenericClass[int, str](), GenericClass[int])
Пример #8
0
 def test_advanced_isinstance_to_generic_class(self):
     assert advanced_isinstance(GenericClass[int](), GenericClass[T1])
     assert advanced_isinstance(GenericClass[int](), GenericClass)
     assert not advanced_isinstance(GenericClass(), GenericClass[int])
     assert not advanced_isinstance(GenericClass[str](), GenericClass[int])
Пример #9
0
 def test_advanced_isinstance_to_none_optional(self):
     assert advanced_isinstance(None, Optional[str])
     assert advanced_isinstance("", Optional[str])
Пример #10
0
 def test_advanced_isinstance_to_any(self):
     assert advanced_isinstance("", Any)
     assert advanced_isinstance(str, Any)
     assert advanced_isinstance(None, Any)
     assert advanced_isinstance((DerivedClass1(), DerivedClass2()), Any)
Пример #11
0
 def _assert_isinstance(value):
     if not advanced_isinstance(value, clazz):
         message = f"The Configurable specifies that the value should be an instance of {clazz}, but it's" \
                   f"an instance of {type(value)}."
         raise ConfigurationValidationException(
             message, TypeError(message))