-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-42195: Ensure consistency of Callable's __args__ in collections.abc and typing #23060
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
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
2c4a297
Allow subclassing of GenericAlias, fix collections.abc.Callable's Gen…
Fidget-Spinner 588d421
fix typing tests, add hash and eq methods
Fidget-Spinner 050fa13
Fix pickling
Fidget-Spinner f60ea8a
whitespace
Fidget-Spinner 2f3c6dc
Add test specifically for bpo
Fidget-Spinner 2c4508e
update error message
Fidget-Spinner 19d2973
Appease test_site
Fidget-Spinner 3116c8e
Represent Callable __args__ via [tuple[args], result]
Fidget-Spinner f2b593a
add back tests for weakref, styling nits, add news
Fidget-Spinner 93d51e4
remove redundant tuple checks leftover from old code
Fidget-Spinner 327e1a5
Use _PosArgs instead of tuple
Fidget-Spinner e971ccb
Fix typo and news
Fidget-Spinner abd8b98
Refactor C code to use less duplication
Fidget-Spinner 3ddca06
Address most of Guido's reviews (tests failing on purpose)
Fidget-Spinner 1ab59c5
try to revert back to good old flat tuple __args__ days
Fidget-Spinner ee2d2e1
getting even closer
Fidget-Spinner 2015738
finally done
Fidget-Spinner 6704ffd
Update news
Fidget-Spinner 598d29b
Address review partially
Fidget-Spinner c43ebcf
Address review fully, update news and tests, remove try-except block
Fidget-Spinner 37ae3a9
Borrowed references don't need decref
Fidget-Spinner adbfcad
improve _PyArg_NoKwnames error handling, add union and subclass tests
Fidget-Spinner d1dd627
Don't change getargs, use _PyArg_NoKeywords instead
Fidget-Spinner 2c21045
Merge remote-tracking branch 'upstream/master' into abc-callable-ga
Fidget-Spinner 9f71667
remove stray whitespace
Fidget-Spinner a789620
refactor C code, add deprecation warning for 3.9
Fidget-Spinner 1890b37
remove redundant check in C code, and try except in __new__
Fidget-Spinner 4e928c6
remove check
Fidget-Spinner 6b11d33
Loosen type checks for Callable args, cast to PyObject in genericalia…
Fidget-Spinner 4215c3b
update news to mention about removing validation in argtypes
Fidget-Spinner 585bf19
remove commented out code
Fidget-Spinner 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from _collections_abc import * | ||
from _collections_abc import __all__ | ||
from _collections_abc import _CallableGenericAlias |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -446,14 +446,6 @@ def test_cannot_instantiate(self): | |
type(c)() | ||
|
||
def test_callable_wrong_forms(self): | ||
with self.assertRaises(TypeError): | ||
Callable[[...], int] | ||
with self.assertRaises(TypeError): | ||
Callable[(), int] | ||
with self.assertRaises(TypeError): | ||
Callable[[()], int] | ||
with self.assertRaises(TypeError): | ||
Callable[[int, 1], 2] | ||
with self.assertRaises(TypeError): | ||
Callable[int] | ||
|
||
|
@@ -1807,10 +1799,9 @@ def barfoo2(x: CT): ... | |
def test_extended_generic_rules_subclassing(self): | ||
class T1(Tuple[T, KT]): ... | ||
class T2(Tuple[T, ...]): ... | ||
class C1(Callable[[T], T]): ... | ||
class C2(Callable[..., int]): | ||
def __call__(self): | ||
return None | ||
class C1(typing.Container[T]): | ||
def __contains__(self, item): | ||
return False | ||
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. So in the 3.9 backport this file should be untouched. |
||
|
||
self.assertEqual(T1.__parameters__, (T, KT)) | ||
self.assertEqual(T1[int, str].__args__, (int, str)) | ||
|
@@ -1824,10 +1815,9 @@ def __call__(self): | |
## T2[int, str] | ||
|
||
self.assertEqual(repr(C1[int]).split('.')[-1], 'C1[int]') | ||
self.assertEqual(C2.__parameters__, ()) | ||
self.assertIsInstance(C2(), collections.abc.Callable) | ||
self.assertIsSubclass(C2, collections.abc.Callable) | ||
self.assertIsSubclass(C1, collections.abc.Callable) | ||
self.assertEqual(C1.__parameters__, (T,)) | ||
self.assertIsInstance(C1(), collections.abc.Container) | ||
self.assertIsSubclass(C1, collections.abc.Container) | ||
self.assertIsInstance(T1(), tuple) | ||
self.assertIsSubclass(T2, tuple) | ||
with self.assertRaises(TypeError): | ||
|
@@ -1861,10 +1851,6 @@ def test_type_erasure_special(self): | |
class MyTup(Tuple[T, T]): ... | ||
self.assertIs(MyTup[int]().__class__, MyTup) | ||
self.assertEqual(MyTup[int]().__orig_class__, MyTup[int]) | ||
class MyCall(Callable[..., T]): | ||
def __call__(self): return None | ||
self.assertIs(MyCall[T]().__class__, MyCall) | ||
self.assertEqual(MyCall[T]().__orig_class__, MyCall[T]) | ||
class MyDict(typing.Dict[T, T]): ... | ||
self.assertIs(MyDict[int]().__class__, MyDict) | ||
self.assertEqual(MyDict[int]().__orig_class__, MyDict[int]) | ||
|
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
Misc/NEWS.d/next/Core and Builtins/2020-11-20-00-57-47.bpo-42195.HeqcpS.rst
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
The ``__args__`` of the parameterized generics for :data:`typing.Callable` | ||
and :class:`collections.abc.Callable` are now consistent. The ``__args__`` | ||
for :class:`collections.abc.Callable` are now flattened while | ||
:data:`typing.Callable`'s have not changed. To allow this change, | ||
:class:`types.GenericAlias` can now be subclassed and | ||
``collections.abc.Callable``'s ``__class_getitem__`` will now return a subclass | ||
of ``types.GenericAlias``. Tests for typing were also updated to not subclass | ||
things like ``Callable[..., T]`` as that is not a valid base class. Finally, | ||
both ``Callable``s no longer validate their ``argtypes``, in | ||
``Callable[[argtypes], resulttype]`` to prepare for :pep:`612`. Patch by Ken Jin. | ||
|
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.
Okay, for 3.10 I think this is the right solution.
For 3.9, I think we need to issue a
DeprecationWarning
(using the warnings module) with the same message (for bothTypeError
s below) and return what was returned in 3.9.0.