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

Remove raw asserts in test_typing.py #104951

Merged
merged 3 commits into from
May 26, 2023
Merged
Changes from 1 commit
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
44 changes: 22 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 @@ -7104,8 +7104,8 @@ 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.assertEqual(Point2Dor3D.__optional_keys__, frozenset(['z']))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this PR look good. But it looks like we have a lot of tests in this test class that do things like self.assertEqual(some_frozenset, frozenset(['x', 'y'])). That looks like it's checking that some_frozenset is a frozenset, but it's actually not, because frozensets and sets with the same members compare equal. So I wonder if we should be doing this:

Suggested change
self.assertEqual(Point2Dor3D.__required_keys__, frozenset(['x', 'y']))
self.assertEqual(Point2Dor3D.__optional_keys__, frozenset(['z']))
self.assertEqual(Point2Dor3D.__required_keys__, {'x', 'y'})
self.assertIsInstance(Point2Dor3D.__required_keys__, frozenset)
self.assertEqual(Point2Dor3D.__optional_keys__, {'z'})
self.assertIsInstance(Point2Dor3D.__optional_keys__, frozenset)

But maybe we don't need to assert the type of __required_keys__ every time we want to check the contents of the set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, we don't need to change that in this PR; the changes here are definitely good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well do that in this PR too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a few new asserts checking that it's really a frozenset


def test_keys_inheritance(self):
class BaseAnimal(TypedDict):
Expand All @@ -7118,26 +7118,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 +7367,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 +7386,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 +7417,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