-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-105499: Merge typing.Union and types.UnionType #105511
Open
JelleZijlstra
wants to merge
50
commits into
python:main
Choose a base branch
from
JelleZijlstra:unifyunion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+533
−304
Open
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
8c8b1dd
Unify UnionType and Union
JelleZijlstra aba63eb
test_typing succeeds
JelleZijlstra f2f23a0
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra e75282f
blurb
JelleZijlstra 540b04b
Fix test_types
JelleZijlstra ae8fa62
Documentation
JelleZijlstra 7569c48
stray f
JelleZijlstra 8bcb930
Fix tests
JelleZijlstra 291953e
No more typing._make_union
JelleZijlstra f7ca8d4
Add tp_new
JelleZijlstra 0f1ab18
fix a refleak
JelleZijlstra eb47a0b
Fix test
JelleZijlstra eaa4e79
Update Lib/test/test_typing.py
JelleZijlstra 4a0235f
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra c71e8c3
Add back _UnionGenericAlias
JelleZijlstra 0475415
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra bb50899
Remove unnecessary NewRef
JelleZijlstra c564672
Make typing.Union the canonical name
JelleZijlstra 9f58421
docs
JelleZijlstra 8884a94
fix test_pydoc
JelleZijlstra bcc2e6a
Update Objects/unionobject.c
JelleZijlstra c4b217b
Update Doc/library/typing.rst
JelleZijlstra 5eb2a0c
Update Doc/library/stdtypes.rst
JelleZijlstra 64ac293
Update Doc/library/stdtypes.rst
JelleZijlstra 0ba8551
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra aacf2b0
Add __mro_entries__
JelleZijlstra f46c0c6
Improve docs, expose it in _typing
JelleZijlstra 8c04441
Update Doc/library/functools.rst
JelleZijlstra 04df4d0
Merge branch 'main' into unifyunion
JelleZijlstra c363de7
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra 98b634a
Post-merge cleanup
JelleZijlstra f2f961b
No need for tp_new
JelleZijlstra 932f8e5
Merge branch 'main' into unifyunion
JelleZijlstra c3edd87
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra 9ae102d
Add back doctest
JelleZijlstra e0924c6
Merge branch 'main' into unifyunion
JelleZijlstra 7086879
Merge branch 'main' into unifyunion
JelleZijlstra b0e057f
Merge branch 'main' into unifyunion
JelleZijlstra 55d0c97
Merge branch 'main' into unifyunion
JelleZijlstra 8a80fe0
Fix two issues
JelleZijlstra f910e88
in progress: hashable unions
JelleZijlstra cae36c7
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra 65da3f1
fixup
JelleZijlstra f500f5f
Make union support unhashable objects
JelleZijlstra 5f9e599
simplify, extend docs
JelleZijlstra 9927b38
fix more tests
JelleZijlstra eea6eca
another
JelleZijlstra 0785951
change hash
JelleZijlstra 1a39ded
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra eddbfde
Merge branch 'main' into unifyunion
JelleZijlstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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 | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.