@@ -120,7 +120,7 @@ class Sub(Any): pass
120
120
121
121
def test_errors (self ):
122
122
with self .assertRaises (TypeError ):
123
- issubclass (42 , Any )
123
+ isinstance (42 , Any )
124
124
with self .assertRaises (TypeError ):
125
125
Any [int ] # Any is not a generic type.
126
126
@@ -135,6 +135,9 @@ class Something: pass
135
135
136
136
class MockSomething (Something , Mock ): pass
137
137
self .assertTrue (issubclass (MockSomething , Any ))
138
+ self .assertTrue (issubclass (MockSomething , MockSomething ))
139
+ self .assertTrue (issubclass (MockSomething , Something ))
140
+ self .assertTrue (issubclass (MockSomething , Mock ))
138
141
ms = MockSomething ()
139
142
self .assertIsInstance (ms , MockSomething )
140
143
self .assertIsInstance (ms , Something )
@@ -2008,13 +2011,81 @@ def test_basics(self):
2008
2011
u = Union [int , float ]
2009
2012
self .assertNotEqual (u , Union )
2010
2013
2011
- def test_subclass_error (self ):
2014
+ def test_union_isinstance (self ):
2015
+ self .assertTrue (isinstance (42 , Union [int , str ]))
2016
+ self .assertTrue (isinstance ('abc' , Union [int , str ]))
2017
+ self .assertFalse (isinstance (3.14 , Union [int , str ]))
2018
+ self .assertTrue (isinstance (42 , Union [int , list [int ]]))
2019
+ self .assertTrue (isinstance (42 , Union [int , Any ]))
2020
+
2021
+ def test_union_isinstance_type_error (self ):
2022
+ with self .assertRaises (TypeError ):
2023
+ isinstance (42 , Union [str , list [int ]])
2024
+ with self .assertRaises (TypeError ):
2025
+ isinstance (42 , Union [list [int ], int ])
2026
+ with self .assertRaises (TypeError ):
2027
+ isinstance (42 , Union [list [int ], str ])
2028
+ with self .assertRaises (TypeError ):
2029
+ isinstance (42 , Union [str , Any ])
2030
+ with self .assertRaises (TypeError ):
2031
+ isinstance (42 , Union [Any , int ])
2032
+ with self .assertRaises (TypeError ):
2033
+ isinstance (42 , Union [Any , str ])
2034
+
2035
+ def test_optional_isinstance (self ):
2036
+ self .assertTrue (isinstance (42 , Optional [int ]))
2037
+ self .assertTrue (isinstance (None , Optional [int ]))
2038
+ self .assertFalse (isinstance ('abc' , Optional [int ]))
2039
+
2040
+ def test_optional_isinstance_type_error (self ):
2041
+ with self .assertRaises (TypeError ):
2042
+ isinstance (42 , Optional [list [int ]])
2043
+ with self .assertRaises (TypeError ):
2044
+ isinstance (None , Optional [list [int ]])
2045
+ with self .assertRaises (TypeError ):
2046
+ isinstance (42 , Optional [Any ])
2047
+ with self .assertRaises (TypeError ):
2048
+ isinstance (None , Optional [Any ])
2049
+
2050
+ def test_union_issubclass (self ):
2051
+ self .assertTrue (issubclass (int , Union [int , str ]))
2052
+ self .assertTrue (issubclass (str , Union [int , str ]))
2053
+ self .assertFalse (issubclass (float , Union [int , str ]))
2054
+ self .assertTrue (issubclass (int , Union [int , list [int ]]))
2055
+ self .assertTrue (issubclass (int , Union [int , Any ]))
2056
+ self .assertFalse (issubclass (int , Union [str , Any ]))
2057
+ self .assertTrue (issubclass (int , Union [Any , int ]))
2058
+ self .assertFalse (issubclass (int , Union [Any , str ]))
2059
+
2060
+ def test_union_issubclass_type_error (self ):
2012
2061
with self .assertRaises (TypeError ):
2013
2062
issubclass (int , Union )
2014
2063
with self .assertRaises (TypeError ):
2015
2064
issubclass (Union , int )
2016
2065
with self .assertRaises (TypeError ):
2017
2066
issubclass (Union [int , str ], int )
2067
+ with self .assertRaises (TypeError ):
2068
+ issubclass (int , Union [str , list [int ]])
2069
+ with self .assertRaises (TypeError ):
2070
+ issubclass (int , Union [list [int ], int ])
2071
+ with self .assertRaises (TypeError ):
2072
+ issubclass (int , Union [list [int ], str ])
2073
+
2074
+ def test_optional_issubclass (self ):
2075
+ self .assertTrue (issubclass (int , Optional [int ]))
2076
+ self .assertTrue (issubclass (type (None ), Optional [int ]))
2077
+ self .assertFalse (issubclass (str , Optional [int ]))
2078
+ self .assertTrue (issubclass (Any , Optional [Any ]))
2079
+ self .assertTrue (issubclass (type (None ), Optional [Any ]))
2080
+ self .assertFalse (issubclass (int , Optional [Any ]))
2081
+
2082
+ def test_optional_issubclass_type_error (self ):
2083
+ with self .assertRaises (TypeError ):
2084
+ issubclass (list [int ], Optional [list [int ]])
2085
+ with self .assertRaises (TypeError ):
2086
+ issubclass (type (None ), Optional [list [int ]])
2087
+ with self .assertRaises (TypeError ):
2088
+ issubclass (int , Optional [list [int ]])
2018
2089
2019
2090
def test_union_any (self ):
2020
2091
u = Union [Any ]
0 commit comments