Example #1
0
 def check_default_invariant_behaviour(
     self, type_a, type_b, local_variables=None, global_variables=None
 ):
     """
     Template for performing certain type checks which use default covariant/contravariant settings
     The default is invariant
     """
     self.assertFalse(
         is_type_of_type(
             type_a,
             type_b,
             local_variables=local_variables,
             global_variables=global_variables,
         )
     )
     self.assertFalse(
         is_type_of_type(
             type_b,
             type_a,
             local_variables=local_variables,
             global_variables=global_variables,
         )
     )
     self.assertTrue(
         is_type_of_type(
             type_a,
             type_a,
             local_variables=local_variables,
             global_variables=global_variables,
         )
     )
Example #2
0
    def test_in_built_types(self):
        """
        Tests an unusual result found while testing tuples
        """
        a = (1, 1)  # Tuple
        b = 1  # Int
        c = 1.1  # Float
        d = 1 + 1j  # Complex
        e = None  # NoneType
        f = True  # Boolean
        g = {}  # Dictionary
        h = []  # List
        i = ''  # String
        k = b''  # Bytes

        self.assertTrue(is_type_of_type(type(a), Tuple))
        self.assertTrue(is_type_of_type(type(b), Integer))
        self.assertTrue(is_type_of_type(type(c), numbers.Real))
        self.assertTrue(is_type_of_type(type(d), numbers.Complex))
        self.assertTrue(is_type_of_type(type(e), type(None)))
        self.assertTrue(is_type_of_type(type(f), Boolean))
        self.assertTrue(is_type_of_type(type(g), Dict))
        self.assertTrue(is_type_of_type(type(h), List))
        self.assertTrue(is_type_of_type(type(i), str))
        self.assertTrue(is_type_of_type(type(k), bytes))
Example #3
0
    def test_in_built_types(self):
        """
        Tests an unusual result found while testing tuples
        """
        a = (1, 1)  # Tuple
        b = 1  # Int
        c = 1.1  # Float
        d = 1 + 1j  # Complex
        e = None  # NoneType
        f = True  # Boolean
        g = {}  # Dictionary
        h = []  # List
        i = ''  # String
        k = b''  # Bytes

        self.assertTrue(is_type_of_type(type(a), Tuple))
        self.assertTrue(is_type_of_type(type(b), Integer))
        self.assertTrue(is_type_of_type(type(c), numbers.Real))
        self.assertTrue(is_type_of_type(type(d), numbers.Complex))
        self.assertTrue(is_type_of_type(type(e), type(None)))
        self.assertTrue(is_type_of_type(type(f), Boolean))
        self.assertTrue(is_type_of_type(type(g), Dict))
        self.assertTrue(is_type_of_type(type(h), List))
        self.assertTrue(is_type_of_type(type(i), str))
        self.assertTrue(is_type_of_type(type(k), bytes))
Example #4
0
 def check_invariant(self,
                     type_a,
                     type_b,
                     local_variables=None,
                     global_variables=None):
     """
     Template for performing certain invariant type checks
     """
     self.assertFalse(
         is_type_of_type(type_a,
                         type_b,
                         covariant=False,
                         contravariant=False,
                         local_variables=local_variables,
                         global_variables=global_variables))
     self.assertFalse(
         is_type_of_type(type_b,
                         type_a,
                         covariant=False,
                         contravariant=False,
                         local_variables=local_variables,
                         global_variables=global_variables))
     self.assertTrue(
         is_type_of_type(type_a,
                         type_a,
                         covariant=False,
                         contravariant=False,
                         local_variables=local_variables,
                         global_variables=global_variables))
Example #5
0
 def test_type_var_default(self):
     """
     Verifies that type checking works as expected with parameterless TypeVar
     and it works invariantly
     """
     T = TypeVar('T')
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(None, T))
Example #6
0
 def test_type_var_constrained(self):
     """
     Verifies that type checking respects the TypeVar constraints
     """
     T = TypeVar('T', Animal, int)
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(int, T))
     self.assertFalse(is_type_of_type(None, T))
Example #7
0
 def test_type_var_constrained(self):
     """
     Verifies that type checking respects the TypeVar constraints
     """
     T = TypeVar('T', Animal, int)
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(int, T))
     self.assertFalse(is_type_of_type(None, T))
Example #8
0
 def test_type_var_default(self):
     """
     Verifies that type checking works as expected with parameterless TypeVar
     and it works invariantly
     """
     T = TypeVar('T')
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(None, T))
Example #9
0
 def test_any(self):
     """
     Verifies that type checking works with Any construct
     """
     self.assertTrue(is_type_of_type(Animal, Any))
     self.assertTrue(is_type_of_type(None, Any))
     self.assertTrue(is_type_of_type(12, Any))
     self.assertTrue(is_type_of_type([1, 3, 'str'], Any))
     self.assertTrue(type, Any)
Example #10
0
 def test_none(self):
     """
     Verifies that type checking automatically replaces None with NoneType
     """
     self.assertTrue(is_type_of_type(None, None))
     self.assertTrue(is_type_of_type(type(None), None))
     self.assertTrue(is_type_of_type(None, None, covariant=True))
     self.assertTrue(is_type_of_type(None, None, contravariant=True))
     self.assertTrue(is_type_of_type(None, None, covariant=True, contravariant=True))
Example #11
0
 def test_none(self):
     """
     Verifies that type checking automatically replaces None with NoneType
     """
     self.assertTrue(is_type_of_type(None, None))
     self.assertTrue(is_type_of_type(type(None), None))
     self.assertTrue(is_type_of_type(None, None, covariant=True))
     self.assertTrue(is_type_of_type(None, None, contravariant=True))
     self.assertTrue(is_type_of_type(None, None, covariant=True, contravariant=True))
Example #12
0
 def test_type_var_contravariant(self):
     """
     Verifies that type checking works with contravariant TypeVars
     """
     T = TypeVar('T', Pet, int, contravariant=True)
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(Pet, T))
     self.assertFalse(is_type_of_type(Chihuahua, T))
     self.assertTrue(is_type_of_type(int, T))
     self.assertFalse(is_type_of_type(None, T))
Example #13
0
 def test_type_var_contravariant(self):
     """
     Verifies that type checking works with contravariant TypeVars
     """
     T = TypeVar('T', Pet, int, contravariant=True)
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(Pet, T))
     self.assertFalse(is_type_of_type(Chihuahua, T))
     self.assertTrue(is_type_of_type(int, T))
     self.assertFalse(is_type_of_type(None, T))
Example #14
0
 def test_enhanced_type_var(self):
     """
     Verifies that type checking behaves exactly the same with an Enhanced TypeVar
     as it would with a default TypeVar
     """
     T = EnhancedTypeVar('T', str, int, Animal)
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(int, T))
     self.assertTrue(is_type_of_type(str, T))
     self.assertFalse(is_type_of_type(Pet, T))
     self.assertFalse(is_type_of_type(None, T))
Example #15
0
 def test_enhanced_type_var_bivariant(self):
     """
     Default TypeVars cannot be bivariant
     This test verifies if an Enhanced version of it will properly checked
     """
     T = EnhancedTypeVar('T', Pet, int, covariant=True, contravariant=True)
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(Pet, T))
     self.assertTrue(is_type_of_type(Chihuahua, T))
     self.assertTrue(is_type_of_type(int, T))
     self.assertFalse(is_type_of_type(None, T))
Example #16
0
 def test_enhanced_type_var_bivariant(self):
     """
     Default TypeVars cannot be bivariant
     This test verifies if an Enhanced version of it will properly checked
     """
     T = EnhancedTypeVar('T', Pet, int, covariant=True, contravariant=True)
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(Pet, T))
     self.assertTrue(is_type_of_type(Chihuahua, T))
     self.assertTrue(is_type_of_type(int, T))
     self.assertFalse(is_type_of_type(None, T))
Example #17
0
 def test_enhanced_type_var(self):
     """
     Verifies that type checking behaves exactly the same with an Enhanced TypeVar
     as it would with a default TypeVar
     """
     T = EnhancedTypeVar('T', str, int, Animal)
     self.assertTrue(is_type_of_type(Animal, T))
     self.assertTrue(is_type_of_type(int, T))
     self.assertTrue(is_type_of_type(str, T))
     self.assertFalse(is_type_of_type(Pet, T))
     self.assertFalse(is_type_of_type(None, T))
Example #18
0
 def test_abc_protocols(self):
     """
     Verifies that ABC protocols are respected and working as expected
     """
     some_list = [1]
     list_type = type(some_list)
     self.assertTrue(is_type_of_type(list_type, Sized, covariant=True))
Example #19
0
 def test_abc_protocols(self):
     """
     Verifies that ABC protocols are respected and working as expected
     """
     some_list = [1]
     list_type = type(some_list)
     self.assertTrue(is_type_of_type(list_type, Sized, covariant=True))
Example #20
0
 def check_default_invariant_behaviour(self, type_a, type_b, local_variables=None, global_variables=None):
     """
     Template for performing certain type checks which use default covariant/contravariant settings
     The default is invariant
     """
     self.assertFalse(is_type_of_type(type_a,
                                     type_b,
                                     local_variables=local_variables,
                                     global_variables=global_variables))
     self.assertFalse(is_type_of_type(type_b,
                                      type_a,
                                     local_variables=local_variables,
                                     global_variables=global_variables))
     self.assertTrue(is_type_of_type(type_a,
                                     type_a,
                                     local_variables=local_variables,
                                     global_variables=global_variables))
Example #21
0
 def check_invariant(self, type_a, type_b, local_variables=None, global_variables=None):
     """
     Template for performing certain invariant type checks
     """
     self.assertFalse(is_type_of_type(type_a,
                                     type_b,
                                     covariant=False,
                                     contravariant=False,
                                     local_variables=local_variables,
                                     global_variables=global_variables))
     self.assertFalse(is_type_of_type(type_b,
                                      type_a,
                                      covariant=False,
                                      contravariant=False,
                                     local_variables=local_variables,
                                     global_variables=global_variables))
     self.assertTrue(is_type_of_type(type_a,
                                     type_a,
                                     covariant=False,
                                     contravariant=False,
                                     local_variables=local_variables,
                                     global_variables=global_variables))
Example #22
0
    def test_complex_type_var(self):
        """
        Verifies that nested types, such as Unions, can be compared
        """
        T = TypeVar('T', Union[int, str], bytes)
        K = TypeVar('K', Optional[int], str)

        self.assertTrue(is_type_of_type(Union[str, int], T))
        self.assertTrue(is_type_of_type(bytes, T))

        self.assertFalse(is_type_of_type(Union[int, str, bytes], T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(bytearray, T))

        self.assertTrue(is_type_of_type(Optional[int], K))
        self.assertTrue(is_type_of_type(Union[None, int], K))
        self.assertTrue(is_type_of_type(str, K))

        self.assertFalse(is_type_of_type(int, K))
Example #23
0
    def test_complex_type_var(self):
        """
        Verifies that nested types, such as Unions, can be compared
        """
        T = TypeVar('T', Union[int, str], bytes)
        K = TypeVar('K', Optional[int], str)
        
        self.assertTrue(is_type_of_type(Union[str, int], T))
        self.assertTrue(is_type_of_type(bytes, T))

        self.assertFalse(is_type_of_type(Union[int, str, bytes], T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(bytearray, T))

        self.assertTrue(is_type_of_type(Optional[int], K))
        self.assertTrue(is_type_of_type(Union[None, int], K))
        self.assertTrue(is_type_of_type(str, K))

        self.assertFalse(is_type_of_type(int, K))
Example #24
0
    def test_subbclasscheck(self):
        """
        Verifies that subclasscheck is always respected if present
        """
        class A:
            @classmethod
            def __subclasscheck__(cls, C):
                try:
                    if cls is C.subclass_of:
                        return True
                    else:
                        return False
                except AttributeError:
                    return NotImplemented

        class B(A):
            pass

        class C:
            subclass_of = A

        class D(A):
            subclass_of = None

        class E:
            subclass_of = None

        self.assertTrue(is_type_of_type(A, A))

        self.assertFalse(is_type_of_type(B, A))
        self.assertFalse(is_type_of_type(A, B))
        self.assertFalse(is_type_of_type(C, A))
        self.assertFalse(is_type_of_type(A, C))
        self.assertFalse(is_type_of_type(D, A))
        self.assertFalse(is_type_of_type(A, D))
        self.assertFalse(is_type_of_type(E, A))
        self.assertFalse(is_type_of_type(A, E))

        self.assertTrue(is_type_of_type(A, A, covariant=True))
        self.assertTrue(is_type_of_type(A, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(B, A, covariant=True))
        self.assertFalse(is_type_of_type(A, B, covariant=True))
        self.assertFalse(is_type_of_type(B, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, B, contravariant=True))
        self.assertTrue(is_type_of_type(B, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(B, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(C, A, covariant=True))
        self.assertFalse(is_type_of_type(A, C, covariant=True))
        self.assertFalse(is_type_of_type(C, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, C, contravariant=True))
        self.assertTrue(is_type_of_type(C, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(C, A, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(D, A, covariant=True))
        self.assertFalse(is_type_of_type(A, D, covariant=True))
        self.assertFalse(is_type_of_type(D, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, D, contravariant=True))
        self.assertFalse(is_type_of_type(D, A, covariant=True, contravariant=True))
        self.assertFalse(is_type_of_type(A, D, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(E, A, covariant=True))
        self.assertFalse(is_type_of_type(A, E, covariant=True))
        self.assertFalse(is_type_of_type(E, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, E, contravariant=True))
        self.assertFalse(is_type_of_type(E, A, covariant=True, contravariant=True))
        self.assertFalse(is_type_of_type(A, E, covariant=True, contravariant=True))
Example #25
0
    def test_subbclasshook(self):
        """
        Verifies that a subclasshook in ABC is respected and it takes precedence over ABC registry
        """
        class A(ABC):

            @classmethod
            def __subclasshook__(cls, C):
                try:
                    if cls is C.subclass_of:
                        return True
                    else:
                        return False
                except AttributeError:
                    return NotImplemented

        class B(A):
            pass

        class C:
            subclass_of = A

        class D(A):
            subclass_of = None

        class E:
            subclass_of = None

        A.register(E)

        self.assertTrue(is_type_of_type(A, A))

        self.assertFalse(is_type_of_type(B, A))
        self.assertFalse(is_type_of_type(A, B))
        self.assertFalse(is_type_of_type(C, A))
        self.assertFalse(is_type_of_type(A, C))
        self.assertFalse(is_type_of_type(D, A))
        self.assertFalse(is_type_of_type(A, D))
        self.assertFalse(is_type_of_type(E, A))
        self.assertFalse(is_type_of_type(A, E))

        self.assertTrue(is_type_of_type(A, A, covariant=True))
        self.assertTrue(is_type_of_type(A, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(B, A, covariant=True))
        self.assertFalse(is_type_of_type(A, B, covariant=True))
        self.assertFalse(is_type_of_type(B, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, B, contravariant=True))
        self.assertTrue(is_type_of_type(B, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(B, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(C, A, covariant=True))
        self.assertFalse(is_type_of_type(A, C, covariant=True))
        self.assertFalse(is_type_of_type(C, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, C, contravariant=True))
        self.assertTrue(is_type_of_type(C, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(C, A, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(D, A, covariant=True))
        self.assertFalse(is_type_of_type(A, D, covariant=True))
        self.assertFalse(is_type_of_type(D, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, D, contravariant=True))
        self.assertFalse(is_type_of_type(D, A, covariant=True, contravariant=True))
        self.assertFalse(is_type_of_type(A, D, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(E, A, covariant=True))
        self.assertFalse(is_type_of_type(A, E, covariant=True))
        self.assertFalse(is_type_of_type(E, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, E, contravariant=True))
        self.assertFalse(is_type_of_type(E, A, covariant=True, contravariant=True))
        self.assertFalse(is_type_of_type(A, E, covariant=True, contravariant=True))
Example #26
0
    def test_abc_registry(self):
        """
        Verifies that when a class is registered with ABC,
        unless a type check is invariant or subclasshook is defined on ABC,
        it would be a subclass of that ABC.
        This check must be done recursively.
        """
        # NOTE: Subclass test is a covariant check
        class A(ABC):
            pass

        class B(A):
            pass

        class C(ABC):
            pass

        class D(A):
            pass

        class E:
            pass

        class F:
            pass

        class G:
            pass

        A.register(C)
        A.register(D)
        A.register(G)
        A.register(tuple)
        C.register(F)

        self.assertTrue(is_type_of_type(A, A))

        self.assertFalse(is_type_of_type(B, A))
        self.assertFalse(is_type_of_type(A, B))

        self.assertFalse(is_type_of_type(C, A))
        self.assertFalse(is_type_of_type(A, C))

        self.assertFalse(is_type_of_type(D, A))
        self.assertFalse(is_type_of_type(A, D))

        self.assertFalse(is_type_of_type(E, A))
        self.assertFalse(is_type_of_type(A, E))

        self.assertFalse(is_type_of_type(F, A))
        self.assertFalse(is_type_of_type(A, F))

        self.assertFalse(is_type_of_type(G, A))
        self.assertFalse(is_type_of_type(A, G))

        self.assertFalse(is_type_of_type(tuple, A))
        self.assertFalse(is_type_of_type(A, tuple))

        self.assertTrue(is_type_of_type(A, A, covariant=True))
        self.assertTrue(is_type_of_type(A, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(B, A, covariant=True))
        self.assertFalse(is_type_of_type(A, B, covariant=True))
        self.assertFalse(is_type_of_type(B, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, B, contravariant=True))
        self.assertTrue(is_type_of_type(B, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(A, B, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(C, A, covariant=True))
        self.assertFalse(is_type_of_type(A, C, covariant=True))
        self.assertFalse(is_type_of_type(C, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, C, contravariant=True))
        self.assertTrue(is_type_of_type(C, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(A, C, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(D, A, covariant=True))
        self.assertFalse(is_type_of_type(A, D, covariant=True))
        self.assertFalse(is_type_of_type(D, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, D, contravariant=True))
        self.assertTrue(is_type_of_type(D, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(A, D, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(E, A, covariant=True))
        self.assertFalse(is_type_of_type(A, E, covariant=True))
        self.assertFalse(is_type_of_type(E, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, E, contravariant=True))
        self.assertFalse(is_type_of_type(E, A, covariant=True, contravariant=True))
        self.assertFalse(is_type_of_type(A, E, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(F, A, covariant=True))
        self.assertFalse(is_type_of_type(A, F, covariant=True))
        self.assertFalse(is_type_of_type(F, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, F, contravariant=True))
        self.assertTrue(is_type_of_type(F, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(A, F, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(G, A, covariant=True))
        self.assertFalse(is_type_of_type(A, G, covariant=True))
        self.assertFalse(is_type_of_type(G, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, G, contravariant=True))
        self.assertTrue(is_type_of_type(G, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(A, G, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(tuple, A, covariant=True))
        self.assertFalse(is_type_of_type(A, tuple, covariant=True))
        self.assertFalse(is_type_of_type(tuple, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, tuple, contravariant=True))
        self.assertTrue(is_type_of_type(tuple, A, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(A, tuple, covariant=True, contravariant=True))
Example #27
0
    def test_generic_type(self):
        """
        Verifies that it can correctly compare generic types
        """
        from enforce.enforcers import GenericProxy

        T = TypeVar('T')

        class A(Generic[T]):
            pass

        class B(A):
            pass

        C = GenericProxy(A)

        self.assertFalse(is_type_of_type(A, Generic))
        self.assertFalse(is_type_of_type(Generic, A))
        self.assertTrue(is_type_of_type(A, Generic, covariant=True))
        self.assertFalse(is_type_of_type(Generic, A, covariant=True))
        self.assertFalse(is_type_of_type(A, Generic, contravariant=True))
        self.assertTrue(is_type_of_type(Generic, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, Generic, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(Generic, A, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(B, Generic))
        self.assertFalse(is_type_of_type(Generic, B))
        self.assertTrue(is_type_of_type(B, Generic, covariant=True))
        self.assertFalse(is_type_of_type(Generic, B, covariant=True))
        self.assertFalse(is_type_of_type(B, Generic, contravariant=True))
        self.assertTrue(is_type_of_type(Generic, B, contravariant=True))
        self.assertTrue(is_type_of_type(B, Generic, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(Generic, B, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(C, Generic))
        self.assertFalse(is_type_of_type(Generic, C))
        self.assertTrue(is_type_of_type(C, Generic, covariant=True))
        self.assertFalse(is_type_of_type(Generic, C, covariant=True))
        self.assertFalse(is_type_of_type(C, Generic, contravariant=True))
        self.assertTrue(is_type_of_type(Generic, C, contravariant=True))
        self.assertTrue(is_type_of_type(C, Generic, covariant=True, contravariant=True))
        self.assertTrue(is_type_of_type(Generic, C, covariant=True, contravariant=True))
Example #28
0
    def test_generic_type(self):
        """
        Verifies that it can correctly compare generic types
        """
        from enforce.enforcers import GenericProxy

        T = TypeVar('T')

        class A(Generic[T]):
            pass

        class B(A):
            pass

        C = GenericProxy(A)

        self.assertFalse(is_type_of_type(A, Generic))
        self.assertFalse(is_type_of_type(Generic, A))
        self.assertTrue(is_type_of_type(A, Generic, covariant=True))
        self.assertFalse(is_type_of_type(Generic, A, covariant=True))
        self.assertFalse(is_type_of_type(A, Generic, contravariant=True))
        self.assertTrue(is_type_of_type(Generic, A, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, Generic, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(Generic, A, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(B, Generic))
        self.assertFalse(is_type_of_type(Generic, B))
        self.assertTrue(is_type_of_type(B, Generic, covariant=True))
        self.assertFalse(is_type_of_type(Generic, B, covariant=True))
        self.assertFalse(is_type_of_type(B, Generic, contravariant=True))
        self.assertTrue(is_type_of_type(Generic, B, contravariant=True))
        self.assertTrue(
            is_type_of_type(B, Generic, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(Generic, B, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(C, Generic))
        self.assertFalse(is_type_of_type(Generic, C))
        self.assertTrue(is_type_of_type(C, Generic, covariant=True))
        self.assertFalse(is_type_of_type(Generic, C, covariant=True))
        self.assertFalse(is_type_of_type(C, Generic, contravariant=True))
        self.assertTrue(is_type_of_type(Generic, C, contravariant=True))
        self.assertTrue(
            is_type_of_type(C, Generic, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(Generic, C, covariant=True, contravariant=True))
Example #29
0
    def test_type_var_bounded(self):
        """
        Verifies that type checking works with bounded TypeVars
        It uses Enhanced TypeVars for bivariant tests as default TypeVars cannot be bivariant
        """
        T = TypeVar('T', bound=Animal)
        self.assertTrue(is_type_of_type(Animal, T))
        self.assertFalse(is_type_of_type(Pet, T))
        self.assertFalse(is_type_of_type(Chihuahua, T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(None, T))

        T = TypeVar('T', covariant=True, bound=Animal)
        self.assertTrue(is_type_of_type(Animal, T))
        self.assertTrue(is_type_of_type(Pet, T))
        self.assertTrue(is_type_of_type(Chihuahua, T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(None, T))

        T = TypeVar('T', contravariant=True, bound=Pet)
        self.assertTrue(is_type_of_type(Animal, T))
        self.assertTrue(is_type_of_type(Pet, T))
        self.assertFalse(is_type_of_type(Chihuahua, T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(None, T))

        # Bivariant TypeVars are not supported by default
        # Therefore, testing it with an Enhanced version of TypeVar
        T = EnhancedTypeVar('T', covariant=True, contravariant=True, bound=Pet)
        self.assertTrue(is_type_of_type(Animal, T))
        self.assertTrue(is_type_of_type(Pet, T))
        self.assertTrue(is_type_of_type(Chihuahua, T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(None, T))
Example #30
0
 def test_any_from_str(self):
     """
     Verifies that type checking works with Any construct if it is provided as a string with the type name
     """
     self.assertTrue(is_type_of_type(Animal, 'Any'))
Example #31
0
    def test_type_var_bounded(self):
        """
        Verifies that type checking works with bounded TypeVars
        It uses Enhanced TypeVars for bivariant tests as default TypeVars cannot be bivariant
        """
        T = TypeVar('T', bound=Animal)
        self.assertTrue(is_type_of_type(Animal, T))
        self.assertFalse(is_type_of_type(Pet, T))
        self.assertFalse(is_type_of_type(Chihuahua, T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(None, T))

        T = TypeVar('T', covariant=True, bound=Animal)
        self.assertTrue(is_type_of_type(Animal, T))
        self.assertTrue(is_type_of_type(Pet, T))
        self.assertTrue(is_type_of_type(Chihuahua, T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(None, T))

        T = TypeVar('T', contravariant=True, bound=Pet)
        self.assertTrue(is_type_of_type(Animal, T))
        self.assertTrue(is_type_of_type(Pet, T))
        self.assertFalse(is_type_of_type(Chihuahua, T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(None, T))
        
        # Bivariant TypeVars are not supported by default
        # Therefore, testing it with an Enhanced version of TypeVar
        T = EnhancedTypeVar('T', covariant=True, contravariant=True, bound=Pet)
        self.assertTrue(is_type_of_type(Animal, T))
        self.assertTrue(is_type_of_type(Pet, T))
        self.assertTrue(is_type_of_type(Chihuahua, T))
        self.assertFalse(is_type_of_type(int, T))
        self.assertFalse(is_type_of_type(None, T))
Example #32
0
    def test_abc_registry(self):
        """
        Verifies that when a class is registered with ABC,
        unless a type check is invariant or subclasshook is defined on ABC,
        it would be a subclass of that ABC.
        This check must be done recursively.
        """

        # NOTE: Subclass test is a covariant check
        class A(ABC):
            pass

        class B(A):
            pass

        class C(ABC):
            pass

        class D(A):
            pass

        class E:
            pass

        class F:
            pass

        class G:
            pass

        A.register(C)
        A.register(D)
        A.register(G)
        A.register(tuple)
        C.register(F)

        self.assertTrue(is_type_of_type(A, A))

        self.assertFalse(is_type_of_type(B, A))
        self.assertFalse(is_type_of_type(A, B))

        self.assertFalse(is_type_of_type(C, A))
        self.assertFalse(is_type_of_type(A, C))

        self.assertFalse(is_type_of_type(D, A))
        self.assertFalse(is_type_of_type(A, D))

        self.assertFalse(is_type_of_type(E, A))
        self.assertFalse(is_type_of_type(A, E))

        self.assertFalse(is_type_of_type(F, A))
        self.assertFalse(is_type_of_type(A, F))

        self.assertFalse(is_type_of_type(G, A))
        self.assertFalse(is_type_of_type(A, G))

        self.assertFalse(is_type_of_type(tuple, A))
        self.assertFalse(is_type_of_type(A, tuple))

        self.assertTrue(is_type_of_type(A, A, covariant=True))
        self.assertTrue(is_type_of_type(A, A, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(B, A, covariant=True))
        self.assertFalse(is_type_of_type(A, B, covariant=True))
        self.assertFalse(is_type_of_type(B, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, B, contravariant=True))
        self.assertTrue(
            is_type_of_type(B, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, B, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(C, A, covariant=True))
        self.assertFalse(is_type_of_type(A, C, covariant=True))
        self.assertFalse(is_type_of_type(C, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, C, contravariant=True))
        self.assertTrue(
            is_type_of_type(C, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, C, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(D, A, covariant=True))
        self.assertFalse(is_type_of_type(A, D, covariant=True))
        self.assertFalse(is_type_of_type(D, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, D, contravariant=True))
        self.assertTrue(
            is_type_of_type(D, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, D, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(E, A, covariant=True))
        self.assertFalse(is_type_of_type(A, E, covariant=True))
        self.assertFalse(is_type_of_type(E, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, E, contravariant=True))
        self.assertFalse(
            is_type_of_type(E, A, covariant=True, contravariant=True))
        self.assertFalse(
            is_type_of_type(A, E, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(F, A, covariant=True))
        self.assertFalse(is_type_of_type(A, F, covariant=True))
        self.assertFalse(is_type_of_type(F, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, F, contravariant=True))
        self.assertTrue(
            is_type_of_type(F, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, F, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(G, A, covariant=True))
        self.assertFalse(is_type_of_type(A, G, covariant=True))
        self.assertFalse(is_type_of_type(G, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, G, contravariant=True))
        self.assertTrue(
            is_type_of_type(G, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, G, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(tuple, A, covariant=True))
        self.assertFalse(is_type_of_type(A, tuple, covariant=True))
        self.assertFalse(is_type_of_type(tuple, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, tuple, contravariant=True))
        self.assertTrue(
            is_type_of_type(tuple, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, tuple, covariant=True, contravariant=True))
Example #33
0
    def test_subbclasshook(self):
        """
        Verifies that a subclasshook in ABC is respected and it takes precedence over ABC registry
        """
        class A(ABC):
            @classmethod
            def __subclasshook__(cls, C):
                try:
                    if cls is C.subclass_of:
                        return True
                    else:
                        return False
                except AttributeError:
                    return NotImplemented

        class B(A):
            pass

        class C:
            subclass_of = A

        class D(A):
            subclass_of = None

        class E:
            subclass_of = None

        A.register(E)

        self.assertTrue(is_type_of_type(A, A))

        self.assertFalse(is_type_of_type(B, A))
        self.assertFalse(is_type_of_type(A, B))
        self.assertFalse(is_type_of_type(C, A))
        self.assertFalse(is_type_of_type(A, C))
        self.assertFalse(is_type_of_type(D, A))
        self.assertFalse(is_type_of_type(A, D))
        self.assertFalse(is_type_of_type(E, A))
        self.assertFalse(is_type_of_type(A, E))

        self.assertTrue(is_type_of_type(A, A, covariant=True))
        self.assertTrue(is_type_of_type(A, A, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(B, A, covariant=True))
        self.assertFalse(is_type_of_type(A, B, covariant=True))
        self.assertFalse(is_type_of_type(B, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, B, contravariant=True))
        self.assertTrue(
            is_type_of_type(B, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(B, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(C, A, covariant=True))
        self.assertFalse(is_type_of_type(A, C, covariant=True))
        self.assertFalse(is_type_of_type(C, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, C, contravariant=True))
        self.assertTrue(
            is_type_of_type(C, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(C, A, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(D, A, covariant=True))
        self.assertFalse(is_type_of_type(A, D, covariant=True))
        self.assertFalse(is_type_of_type(D, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, D, contravariant=True))
        self.assertFalse(
            is_type_of_type(D, A, covariant=True, contravariant=True))
        self.assertFalse(
            is_type_of_type(A, D, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(E, A, covariant=True))
        self.assertFalse(is_type_of_type(A, E, covariant=True))
        self.assertFalse(is_type_of_type(E, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, E, contravariant=True))
        self.assertFalse(
            is_type_of_type(E, A, covariant=True, contravariant=True))
        self.assertFalse(
            is_type_of_type(A, E, covariant=True, contravariant=True))
Example #34
0
 def test_any_from_str(self):
     """
     Verifies that type checking works with Any construct if it is provided as a string with the type name
     """
     self.assertTrue(is_type_of_type(Animal, 'Any'))
Example #35
0
    def test_subbclasscheck(self):
        """
        Verifies that subclasscheck is always respected if present
        """
        class A:
            @classmethod
            def __subclasscheck__(cls, C):
                try:
                    if cls is C.subclass_of:
                        return True
                    else:
                        return False
                except AttributeError:
                    return NotImplemented

        class B(A):
            pass

        class C:
            subclass_of = A

        class D(A):
            subclass_of = None

        class E:
            subclass_of = None

        self.assertTrue(is_type_of_type(A, A))

        self.assertFalse(is_type_of_type(B, A))
        self.assertFalse(is_type_of_type(A, B))
        self.assertFalse(is_type_of_type(C, A))
        self.assertFalse(is_type_of_type(A, C))
        self.assertFalse(is_type_of_type(D, A))
        self.assertFalse(is_type_of_type(A, D))
        self.assertFalse(is_type_of_type(E, A))
        self.assertFalse(is_type_of_type(A, E))

        self.assertTrue(is_type_of_type(A, A, covariant=True))
        self.assertTrue(is_type_of_type(A, A, contravariant=True))
        self.assertTrue(
            is_type_of_type(A, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(B, A, covariant=True))
        self.assertFalse(is_type_of_type(A, B, covariant=True))
        self.assertFalse(is_type_of_type(B, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, B, contravariant=True))
        self.assertTrue(
            is_type_of_type(B, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(B, A, covariant=True, contravariant=True))

        self.assertTrue(is_type_of_type(C, A, covariant=True))
        self.assertFalse(is_type_of_type(A, C, covariant=True))
        self.assertFalse(is_type_of_type(C, A, contravariant=True))
        self.assertTrue(is_type_of_type(A, C, contravariant=True))
        self.assertTrue(
            is_type_of_type(C, A, covariant=True, contravariant=True))
        self.assertTrue(
            is_type_of_type(C, A, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(D, A, covariant=True))
        self.assertFalse(is_type_of_type(A, D, covariant=True))
        self.assertFalse(is_type_of_type(D, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, D, contravariant=True))
        self.assertFalse(
            is_type_of_type(D, A, covariant=True, contravariant=True))
        self.assertFalse(
            is_type_of_type(A, D, covariant=True, contravariant=True))

        self.assertFalse(is_type_of_type(E, A, covariant=True))
        self.assertFalse(is_type_of_type(A, E, covariant=True))
        self.assertFalse(is_type_of_type(E, A, contravariant=True))
        self.assertFalse(is_type_of_type(A, E, contravariant=True))
        self.assertFalse(
            is_type_of_type(E, A, covariant=True, contravariant=True))
        self.assertFalse(
            is_type_of_type(A, E, covariant=True, contravariant=True))
Example #36
0
 def test_none_from_str(self):
     """
     Verifies that type checking works with None if it is provided as a string with the type name
     """
     self.assertTrue(is_type_of_type(None, 'None'))
Example #37
0
 def test_none_from_str(self):
     """
     Verifies that type checking works with None if it is provided as a string with the type name
     """
     self.assertTrue(is_type_of_type(None, 'None'))
Example #38
0
 def test_any(self):
     """
     Verifies that type checking works with Any construct
     """
     self.assertTrue(is_type_of_type(Animal, Any))
     self.assertTrue(is_type_of_type(None, Any))