Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-105499: Merge typing.Union and types.UnionType#105511
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
8c8b1ddaba63ebf2f23a0e75282f540b04bae8fa627569c488bcb930291953ef7ca8d40f1ab18eb47a0beaa4e794a0235fc71e8c30475415bb50899c5646729f584218884a94bcc2e6ac4b217b5eb2a0c64ac2930ba8551aacf2b0f46c0c68c0444104df4d0c363de798b634af2f961b932f8e5c3edd879ae102de0924c67086879b0e057f55d0c978a80fe0f910e88cae36c765da3f1f500f5f5f9e5999927b38eea6eca07859511a39dededdbfde3ec9271cab69f0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -709,10 +709,6 @@ def test_or_types_operator(self): | ||
| y = int | bool | ||
| with self.assertRaises(TypeError): | ||
| x < y | ||
| # Check that we don't crash if typing.Union does not have a tuple in __args__ | ||
| y = typing.Union[str, int] | ||
| y.__args__ = [str, int] | ||
| self.assertEqual(x, y) | ||
| def test_hash(self): | ||
| self.assertEqual(hash(int | str), hash(str | int)) | ||
| @@ -727,17 +723,40 @@ class B(metaclass=UnhashableMeta): ... | ||
| self.assertEqual((A | B).__args__, (A, B)) | ||
| union1 = A | B | ||
| with self.assertRaises(TypeError): | ||
| with self.assertRaisesRegex(TypeError, "unhashable type: 'UnhashableMeta'"): | ||
| hash(union1) | ||
| union2 = int | B | ||
| with self.assertRaises(TypeError): | ||
| with self.assertRaisesRegex(TypeError, "unhashable type: 'UnhashableMeta'"): | ||
| hash(union2) | ||
| union3 = A | int | ||
| with self.assertRaises(TypeError): | ||
| with self.assertRaisesRegex(TypeError, "unhashable type: 'UnhashableMeta'"): | ||
| hash(union3) | ||
| def test_unhashable_becomes_hashable(self): | ||
| is_hashable = False | ||
| class UnhashableMeta(type): | ||
| def __hash__(self): | ||
| if is_hashable: | ||
| return 1 | ||
| else: | ||
| raise TypeError("not hashable") | ||
| class A(metaclass=UnhashableMeta): ... | ||
| class B(metaclass=UnhashableMeta): ... | ||
| union = A | B | ||
| self.assertEqual(union.__args__, (A, B)) | ||
| with self.assertRaisesRegex(TypeError, "not hashable"): | ||
| hash(union) | ||
| is_hashable = True | ||
| with self.assertRaisesRegex(TypeError, "union contains 2 unhashable elements"): | ||
| hash(union) | ||
| def test_instancecheck_and_subclasscheck(self): | ||
| for x in (int | str, typing.Union[int, str]): | ||
| with self.subTest(x=x): | ||
| @@ -921,7 +940,7 @@ def forward_before(x: ForwardBefore[int]) -> None: ... | ||
| self.assertEqual(typing.get_args(typing.get_type_hints(forward_after)['x']), | ||
| (int, Forward)) | ||
| self.assertEqual(typing.get_args(typing.get_type_hints(forward_before)['x']), | ||
| (int, Forward)) | ||
| (Forward, int)) | ||
| def test_or_type_operator_with_Protocol(self): | ||
| class Proto(typing.Protocol): | ||
| @@ -1015,9 +1034,14 @@ def __eq__(self, other): | ||
| return 1 / 0 | ||
| bt = BadType('bt', (),{}) | ||
| bt2 = BadType('bt2', (),{}) | ||
| # Comparison should fail and errors should propagate out for bad types. | ||
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the new code there are fewer code paths that trigger the equality comparison. | ||
| union1 = int | bt | ||
| union2 = int | bt2 | ||
| with self.assertRaises(ZeroDivisionError): | ||
| union1 == union2 | ||
| with self.assertRaises(ZeroDivisionError): | ||
| list[int] | list[bt] | ||
| bt | bt2 | ||
| union_ga = (list[str] | int, collections.abc.Callable[..., str] | int, | ||
| d | int) | ||
| @@ -1060,6 +1084,14 @@ def test_or_type_operator_reference_cycle(self): | ||
| self.assertLessEqual(sys.gettotalrefcount() - before, leeway, | ||
| msg='Check for union reference leak.') | ||
| def test_instantiation(self): | ||
| with self.assertRaises(TypeError): | ||
| types.UnionType() | ||
| self.assertIs(int, types.UnionType[int]) | ||
| self.assertIs(int, types.UnionType[int, int]) | ||
| self.assertEqual(int | str, types.UnionType[int, str]) | ||
| self.assertEqual(int | typing.ForwardRef("str"), types.UnionType[int, "str"]) | ||
| class MappingProxyTests(unittest.TestCase): | ||
| mappingproxy = types.MappingProxyType | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.__args__is no longer writable.