@@ -121,7 +121,7 @@ class Sub(Any): pass
121
121
122
122
def test_errors (self ):
123
123
with self .assertRaises (TypeError ):
124
- issubclass (42 , Any )
124
+ isinstance (42 , Any )
125
125
with self .assertRaises (TypeError ):
126
126
Any [int ] # Any is not a generic type.
127
127
@@ -136,6 +136,9 @@ class Something: pass
136
136
137
137
class MockSomething (Something , Mock ): pass
138
138
self .assertTrue (issubclass (MockSomething , Any ))
139
+ self .assertTrue (issubclass (MockSomething , MockSomething ))
140
+ self .assertTrue (issubclass (MockSomething , Something ))
141
+ self .assertTrue (issubclass (MockSomething , Mock ))
139
142
ms = MockSomething ()
140
143
self .assertIsInstance (ms , MockSomething )
141
144
self .assertIsInstance (ms , Something )
@@ -1810,13 +1813,81 @@ def test_basics(self):
1810
1813
u = Union [int , float ]
1811
1814
self .assertNotEqual (u , Union )
1812
1815
1813
- def test_subclass_error (self ):
1816
+ def test_union_isinstance (self ):
1817
+ self .assertTrue (isinstance (42 , Union [int , str ]))
1818
+ self .assertTrue (isinstance ('abc' , Union [int , str ]))
1819
+ self .assertFalse (isinstance (3.14 , Union [int , str ]))
1820
+ self .assertTrue (isinstance (42 , Union [int , list [int ]]))
1821
+ self .assertTrue (isinstance (42 , Union [int , Any ]))
1822
+
1823
+ def test_union_isinstance_type_error (self ):
1824
+ with self .assertRaises (TypeError ):
1825
+ isinstance (42 , Union [str , list [int ]])
1826
+ with self .assertRaises (TypeError ):
1827
+ isinstance (42 , Union [list [int ], int ])
1828
+ with self .assertRaises (TypeError ):
1829
+ isinstance (42 , Union [list [int ], str ])
1830
+ with self .assertRaises (TypeError ):
1831
+ isinstance (42 , Union [str , Any ])
1832
+ with self .assertRaises (TypeError ):
1833
+ isinstance (42 , Union [Any , int ])
1834
+ with self .assertRaises (TypeError ):
1835
+ isinstance (42 , Union [Any , str ])
1836
+
1837
+ def test_optional_isinstance (self ):
1838
+ self .assertTrue (isinstance (42 , Optional [int ]))
1839
+ self .assertTrue (isinstance (None , Optional [int ]))
1840
+ self .assertFalse (isinstance ('abc' , Optional [int ]))
1841
+
1842
+ def test_optional_isinstance_type_error (self ):
1843
+ with self .assertRaises (TypeError ):
1844
+ isinstance (42 , Optional [list [int ]])
1845
+ with self .assertRaises (TypeError ):
1846
+ isinstance (None , Optional [list [int ]])
1847
+ with self .assertRaises (TypeError ):
1848
+ isinstance (42 , Optional [Any ])
1849
+ with self .assertRaises (TypeError ):
1850
+ isinstance (None , Optional [Any ])
1851
+
1852
+ def test_union_issubclass (self ):
1853
+ self .assertTrue (issubclass (int , Union [int , str ]))
1854
+ self .assertTrue (issubclass (str , Union [int , str ]))
1855
+ self .assertFalse (issubclass (float , Union [int , str ]))
1856
+ self .assertTrue (issubclass (int , Union [int , list [int ]]))
1857
+ self .assertTrue (issubclass (int , Union [int , Any ]))
1858
+ self .assertFalse (issubclass (int , Union [str , Any ]))
1859
+ self .assertTrue (issubclass (int , Union [Any , int ]))
1860
+ self .assertFalse (issubclass (int , Union [Any , str ]))
1861
+
1862
+ def test_union_issubclass_type_error (self ):
1814
1863
with self .assertRaises (TypeError ):
1815
1864
issubclass (int , Union )
1816
1865
with self .assertRaises (TypeError ):
1817
1866
issubclass (Union , int )
1818
1867
with self .assertRaises (TypeError ):
1819
1868
issubclass (Union [int , str ], int )
1869
+ with self .assertRaises (TypeError ):
1870
+ issubclass (int , Union [str , list [int ]])
1871
+ with self .assertRaises (TypeError ):
1872
+ issubclass (int , Union [list [int ], int ])
1873
+ with self .assertRaises (TypeError ):
1874
+ issubclass (int , Union [list [int ], str ])
1875
+
1876
+ def test_optional_issubclass (self ):
1877
+ self .assertTrue (issubclass (int , Optional [int ]))
1878
+ self .assertTrue (issubclass (type (None ), Optional [int ]))
1879
+ self .assertFalse (issubclass (str , Optional [int ]))
1880
+ self .assertTrue (issubclass (Any , Optional [Any ]))
1881
+ self .assertTrue (issubclass (type (None ), Optional [Any ]))
1882
+ self .assertFalse (issubclass (int , Optional [Any ]))
1883
+
1884
+ def test_optional_issubclass_type_error (self ):
1885
+ with self .assertRaises (TypeError ):
1886
+ issubclass (list [int ], Optional [list [int ]])
1887
+ with self .assertRaises (TypeError ):
1888
+ issubclass (type (None ), Optional [list [int ]])
1889
+ with self .assertRaises (TypeError ):
1890
+ issubclass (int , Optional [list [int ]])
1820
1891
1821
1892
def test_union_any (self ):
1822
1893
u = Union [Any ]
0 commit comments