Skip to content

Commit

Permalink
Remove raw asserts in test_typing.py (#104951)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored May 26, 2023
1 parent 5ab4bc0 commit 2cb4456
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6474,7 +6474,7 @@ def __len__(self):
return 0

self.assertEqual(len(MMC()), 0)
assert callable(MMC.update)
self.assertTrue(callable(MMC.update))
self.assertIsInstance(MMC(), typing.Mapping)

class MMB(typing.MutableMapping[KT, VT]):
Expand Down Expand Up @@ -6669,8 +6669,8 @@ def foo(a: A) -> Optional[BaseException]:
else:
return a()

assert isinstance(foo(KeyboardInterrupt), KeyboardInterrupt)
assert foo(None) is None
self.assertIsInstance(foo(KeyboardInterrupt), KeyboardInterrupt)
self.assertIsNone(foo(None))


class TestModules(TestCase):
Expand Down Expand Up @@ -7017,6 +7017,10 @@ def test_basics_functional_syntax(self):
self.assertEqual(Emp.__bases__, (dict,))
self.assertEqual(Emp.__annotations__, {'name': str, 'id': int})
self.assertEqual(Emp.__total__, True)
self.assertEqual(Emp.__required_keys__, {'name', 'id'})
self.assertIsInstance(Emp.__required_keys__, frozenset)
self.assertEqual(Emp.__optional_keys__, set())
self.assertIsInstance(Emp.__optional_keys__, frozenset)

def test_typeddict_create_errors(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -7092,7 +7096,9 @@ def test_total(self):
self.assertEqual(D(x=1), {'x': 1})
self.assertEqual(D.__total__, False)
self.assertEqual(D.__required_keys__, frozenset())
self.assertIsInstance(D.__required_keys__, frozenset)
self.assertEqual(D.__optional_keys__, {'x'})
self.assertIsInstance(D.__optional_keys__, frozenset)

self.assertEqual(Options(), {})
self.assertEqual(Options(log_level=2), {'log_level': 2})
Expand All @@ -7104,8 +7110,10 @@ def test_optional_keys(self):
class Point2Dor3D(Point2D, total=False):
z: int

assert Point2Dor3D.__required_keys__ == frozenset(['x', 'y'])
assert Point2Dor3D.__optional_keys__ == frozenset(['z'])
self.assertEqual(Point2Dor3D.__required_keys__, frozenset(['x', 'y']))
self.assertIsInstance(Point2Dor3D.__required_keys__, frozenset)
self.assertEqual(Point2Dor3D.__optional_keys__, frozenset(['z']))
self.assertIsInstance(Point2Dor3D.__optional_keys__, frozenset)

def test_keys_inheritance(self):
class BaseAnimal(TypedDict):
Expand All @@ -7118,26 +7126,26 @@ class Animal(BaseAnimal, total=False):
class Cat(Animal):
fur_color: str

assert BaseAnimal.__required_keys__ == frozenset(['name'])
assert BaseAnimal.__optional_keys__ == frozenset([])
assert BaseAnimal.__annotations__ == {'name': str}
self.assertEqual(BaseAnimal.__required_keys__, frozenset(['name']))
self.assertEqual(BaseAnimal.__optional_keys__, frozenset([]))
self.assertEqual(BaseAnimal.__annotations__, {'name': str})

assert Animal.__required_keys__ == frozenset(['name'])
assert Animal.__optional_keys__ == frozenset(['tail', 'voice'])
assert Animal.__annotations__ == {
self.assertEqual(Animal.__required_keys__, frozenset(['name']))
self.assertEqual(Animal.__optional_keys__, frozenset(['tail', 'voice']))
self.assertEqual(Animal.__annotations__, {
'name': str,
'tail': bool,
'voice': str,
}
})

assert Cat.__required_keys__ == frozenset(['name', 'fur_color'])
assert Cat.__optional_keys__ == frozenset(['tail', 'voice'])
assert Cat.__annotations__ == {
self.assertEqual(Cat.__required_keys__, frozenset(['name', 'fur_color']))
self.assertEqual(Cat.__optional_keys__, frozenset(['tail', 'voice']))
self.assertEqual(Cat.__annotations__, {
'fur_color': str,
'name': str,
'tail': bool,
'voice': str,
}
})

def test_required_notrequired_keys(self):
self.assertEqual(NontotalMovie.__required_keys__,
Expand Down Expand Up @@ -7367,11 +7375,11 @@ class C(B[int]):
self.assertEqual(C.__total__, True)
self.assertEqual(C.__optional_keys__, frozenset(['b']))
self.assertEqual(C.__required_keys__, frozenset(['a', 'c']))
assert C.__annotations__ == {
self.assertEqual(C.__annotations__, {
'a': T,
'b': KT,
'c': int,
}
})
with self.assertRaises(TypeError):
C[str]

Expand All @@ -7386,11 +7394,11 @@ class Point3D(Point2DGeneric[T], Generic[T, KT]):
self.assertEqual(Point3D.__total__, True)
self.assertEqual(Point3D.__optional_keys__, frozenset())
self.assertEqual(Point3D.__required_keys__, frozenset(['a', 'b', 'c']))
assert Point3D.__annotations__ == {
self.assertEqual(Point3D.__annotations__, {
'a': T,
'b': T,
'c': KT,
}
})
self.assertEqual(Point3D[int, str].__origin__, Point3D)

with self.assertRaises(TypeError):
Expand All @@ -7417,11 +7425,11 @@ class WithImplicitAny(B):
self.assertEqual(WithImplicitAny.__total__, True)
self.assertEqual(WithImplicitAny.__optional_keys__, frozenset(['b']))
self.assertEqual(WithImplicitAny.__required_keys__, frozenset(['a', 'c']))
assert WithImplicitAny.__annotations__ == {
self.assertEqual(WithImplicitAny.__annotations__, {
'a': T,
'b': KT,
'c': int,
}
})
with self.assertRaises(TypeError):
WithImplicitAny[str]

Expand Down

0 comments on commit 2cb4456

Please sign in to comment.