Skip to content

Commit 1f90b2f

Browse files
[3.12] test_types: Replace raw assert statements (GH-105500) (#105507)
(cherry picked from commit a8eb737) Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 3c5f0ea commit 1f90b2f

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

Lib/test/test_types.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ def test_type_function(self):
226226
def test_int__format__(self):
227227
def test(i, format_spec, result):
228228
# just make sure we have the unified type for integers
229-
assert type(i) == int
230-
assert type(format_spec) == str
229+
self.assertIs(type(i), int)
230+
self.assertIs(type(format_spec), str)
231231
self.assertEqual(i.__format__(format_spec), result)
232232

233233
test(123456789, 'd', '123456789')
@@ -782,8 +782,8 @@ def __subclasscheck__(cls, sub):
782782

783783
def test_or_type_operator_with_TypeVar(self):
784784
TV = typing.TypeVar('T')
785-
assert TV | str == typing.Union[TV, str]
786-
assert str | TV == typing.Union[str, TV]
785+
self.assertEqual(TV | str, typing.Union[TV, str])
786+
self.assertEqual(str | TV, typing.Union[str, TV])
787787
self.assertIs((int | TV)[int], int)
788788
self.assertIs((TV | int)[int], int)
789789

@@ -887,43 +887,45 @@ def test_or_type_operator_with_forward(self):
887887
ForwardBefore = 'Forward' | T
888888
def forward_after(x: ForwardAfter[int]) -> None: ...
889889
def forward_before(x: ForwardBefore[int]) -> None: ...
890-
assert typing.get_args(typing.get_type_hints(forward_after)['x']) == (int, Forward)
891-
assert typing.get_args(typing.get_type_hints(forward_before)['x']) == (int, Forward)
890+
self.assertEqual(typing.get_args(typing.get_type_hints(forward_after)['x']),
891+
(int, Forward))
892+
self.assertEqual(typing.get_args(typing.get_type_hints(forward_before)['x']),
893+
(int, Forward))
892894

893895
def test_or_type_operator_with_Protocol(self):
894896
class Proto(typing.Protocol):
895897
def meth(self) -> int:
896898
...
897-
assert Proto | str == typing.Union[Proto, str]
899+
self.assertEqual(Proto | str, typing.Union[Proto, str])
898900

899901
def test_or_type_operator_with_Alias(self):
900-
assert list | str == typing.Union[list, str]
901-
assert typing.List | str == typing.Union[typing.List, str]
902+
self.assertEqual(list | str, typing.Union[list, str])
903+
self.assertEqual(typing.List | str, typing.Union[typing.List, str])
902904

903905
def test_or_type_operator_with_NamedTuple(self):
904-
NT=namedtuple('A', ['B', 'C', 'D'])
905-
assert NT | str == typing.Union[NT,str]
906+
NT = namedtuple('A', ['B', 'C', 'D'])
907+
self.assertEqual(NT | str, typing.Union[NT, str])
906908

907909
def test_or_type_operator_with_TypedDict(self):
908910
class Point2D(typing.TypedDict):
909911
x: int
910912
y: int
911913
label: str
912-
assert Point2D | str == typing.Union[Point2D, str]
914+
self.assertEqual(Point2D | str, typing.Union[Point2D, str])
913915

914916
def test_or_type_operator_with_NewType(self):
915917
UserId = typing.NewType('UserId', int)
916-
assert UserId | str == typing.Union[UserId, str]
918+
self.assertEqual(UserId | str, typing.Union[UserId, str])
917919

918920
def test_or_type_operator_with_IO(self):
919-
assert typing.IO | str == typing.Union[typing.IO, str]
921+
self.assertEqual(typing.IO | str, typing.Union[typing.IO, str])
920922

921923
def test_or_type_operator_with_SpecialForm(self):
922-
assert typing.Any | str == typing.Union[typing.Any, str]
923-
assert typing.NoReturn | str == typing.Union[typing.NoReturn, str]
924-
assert typing.Optional[int] | str == typing.Union[typing.Optional[int], str]
925-
assert typing.Optional[int] | str == typing.Union[int, str, None]
926-
assert typing.Union[int, bool] | str == typing.Union[int, bool, str]
924+
self.assertEqual(typing.Any | str, typing.Union[typing.Any, str])
925+
self.assertEqual(typing.NoReturn | str, typing.Union[typing.NoReturn, str])
926+
self.assertEqual(typing.Optional[int] | str, typing.Union[typing.Optional[int], str])
927+
self.assertEqual(typing.Optional[int] | str, typing.Union[int, str, None])
928+
self.assertEqual(typing.Union[int, bool] | str, typing.Union[int, bool, str])
927929

928930
def test_or_type_operator_with_Literal(self):
929931
Literal = typing.Literal
@@ -955,12 +957,12 @@ class Ints(enum.IntEnum):
955957
(Literal[1], Literal[Ints.B]))
956958

957959
def test_or_type_repr(self):
958-
assert repr(int | str) == "int | str"
959-
assert repr((int | str) | list) == "int | str | list"
960-
assert repr(int | (str | list)) == "int | str | list"
961-
assert repr(int | None) == "int | None"
962-
assert repr(int | type(None)) == "int | None"
963-
assert repr(int | typing.GenericAlias(list, int)) == "int | list[int]"
960+
self.assertEqual(repr(int | str), "int | str")
961+
self.assertEqual(repr((int | str) | list), "int | str | list")
962+
self.assertEqual(repr(int | (str | list)), "int | str | list")
963+
self.assertEqual(repr(int | None), "int | None")
964+
self.assertEqual(repr(int | type(None)), "int | None")
965+
self.assertEqual(repr(int | typing.GenericAlias(list, int)), "int | list[int]")
964966

965967
def test_or_type_operator_with_genericalias(self):
966968
a = list[int]

0 commit comments

Comments
 (0)