Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.12] Remove raw asserts in test_typing.py (GH-104951) #104978

Merged
merged 1 commit into from
May 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_basics_keywords_syntax(self):
with self.assertWarns(DeprecationWarning):
Expand Down Expand Up @@ -7119,7 +7123,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 @@ -7131,8 +7137,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 @@ -7145,26 +7153,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 @@ -7394,11 +7402,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 @@ -7413,11 +7421,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 @@ -7444,11 +7452,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