8000 bpo-42195: Ensure consistency of Callable's __args__ in collections.abc and typing by Fidget-Spinner · Pull Request #23060 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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 31 commits into from
Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 Oct 31, 2020
588d421
fix typing tests, add hash and eq methods
Fidget-Spinner Oct 31, 2020
050fa13
Fix pickling
Fidget-Spinner Oct 31, 2020
f60ea8a
whitespace
Fidget-Spinner Oct 31, 2020
2f3c6dc
Add test specifically for bpo
Fidget-Spinner Oct 31, 2020
2c4508e
update error message
Fidget-Spinner Oct 31, 2020
19d2973
Appease test_site
Fidget-Spinner Oct 31, 2020
3116c8e
Represent Callable __args__ via [tuple[args], result]
Fidget-Spinner Nov 19, 2020
f2b593a
add back tests for weakref, styling nits, add news
Fidget-Spinner Nov 19, 2020
93d51e4
remove redundant tuple checks leftover from old code
Fidget-Spinner Nov 19, 2020
327e1a5
Use _PosArgs instead of tuple
Fidget-Spinner Nov 28, 2020
e971ccb
Fix typo and news
Fidget-Spinner Nov 29, 2020
abd8b98
Refactor C code to use less duplication
Fidget-Spinner Nov 30, 2020
3ddca06
Address most of Guido's reviews (tests failing on purpose)
Fidget-Spinner Dec 1, 2020
1ab59c5
try to revert back to good old flat tuple __args__ days
Fidget-Spinner Dec 2, 2020
ee2d2e1
getting even closer
Fidget-Spinner Dec 2, 2020
2015738
finally done
Fidget-Spinner Dec 2, 2020
6704ffd
Update news
Fidget-Spinner Dec 4, 2020
598d29b
Address review partially
Fidget-Spinner Dec 5, 2020
c43ebcf
Address review fully, update news and tests, remove try-except block
Fidget-Spinner Dec 5, 2020
37ae3a9
Borrowed references don't need decref
Fidget-Spinner Dec 5, 2020
adbfcad
improve _PyArg_NoKwnames error handling, add union and subclass tests
Fidget-Spinner Dec 5, 2020
d1dd627
Don't change getargs, use _PyArg_NoKeywords instead
Fidget-Spinner Dec 5, 2020
2c21045
Merge remote-tracking branch 'upstream/master' into abc-callable-ga
Fidget-Spinner Dec 5, 2020
9f71667
remove stray whitespace
Fidget-Spinner Dec 5, 2020
a789620
refactor C code, add deprecation warning for 3.9
Fidget-Spinner Dec 6, 2020
1890b37
remove redundant check in C code, and try except in __new__
Fidget-Spinner Dec 6, 2020
4e928c6
remove check
Fidget-Spinner Dec 7, 2020
6b11d33
Loosen type checks for Callable args, cast to PyObject in genericalia…
Fidget-Spinner Dec 11, 2020
4215c3b
update news to mention about removing validation in argtypes
Fidget-Spinner Dec 11, 2020
585bf19
remove commented out code
Fidget-Spinner Dec 12, 2020
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
Prev Previous commit
Next Next commit
Address most of Guido's reviews (tests failing on purpose)
  • Loading branch information
Fidget-Spinner committed Dec 1, 2020
commit 3ddca0664b4e53d525ace1947128ed11ff6d452c
47 changes: 21 additions & 26 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ def _f(): pass
"MappingView", "KeysView", "ItemsView", "ValuesView",
"Sequence", "MutableSequence",
"ByteString",
# The following classse are to allow pickling, not actually
# meant to be imported.
"_CallableGenericAlias",
"_PosArgs",
"_PosArgsGenericAlias"
]

# This module has been renamed from collections.abc to _collections_abc to
Expand Down Expand Up @@ -422,17 +417,19 @@ class _PosArgsGenericAlias(GenericAlias):
""" Internal class specifically to represent positional arguments in
``_CallableGenericAlias``.
"""
__slots__ = ()

def __repr__(self):
return f"{__name__}._PosArgsGenericAlias" \
f"[{', '.join(_type_repr(t) for t in self.__args__)}]"

def __eq__(self, other):
o_cls = other.__class__
if not (o_cls.__module__ == "typing" and o_cls.__name__
== "_GenericAlias" or isinstance(other, GenericAlias)):
return NotImplemented
return (self.__origin__ == other.__origin__
and self.__args__ == other.__args__)
if ((o_cls.__module__ == "typing" and o_cls.__name__
== "_GenericAlias") or isinstance(other, GenericAlias)):
return (self.__origin__ == other.__origin__
and self.__args__ == other.__args__)
return NotImplemented

def __hash__(self):
return hash((self.__origin__, self.__args__))
Expand All @@ -445,39 +442,37 @@ class _PosArgs:
def __class_getitem__(cls, item):
return _PosArgsGenericAlias(tuple, item)

# _PosArgs = type("_PosArgs", (tuple, ), {})

class _CallableGenericAlias(GenericAlias):
""" Internal class specifically for consistency between the ``__args__`` of
``collections.abc.Callable``'s and ``typing.Callable``'s ``GenericAlias``.
"""

def __new__(cls, *args, **kwargs):
if not isinstance(args, tuple) or len(args) != 2:
raise TypeError("Callable must be used as Callable[[arg, ...], result]")
origin, _args = args
def __new__(cls, origin, _args, **kwargs):
if not isinstance(_args, tuple) or len(_args) != 2:
raise TypeError("Callable must be used as Callable[[arg, ...], result]")
t_args, t_result = _args
if not isinstance(t_args, (list, EllipsisType)):
raise TypeError("Callable[args, result]: args must be a list "
f"or Ellipsis. Got {_type_repr(t_args)}")
raise TypeError(f"Callable[args, result]: args must be a list or Ellipsis. "
f"Got {_type_repr(t_args)}")

ga_args = (_args if t_args is Ellipsis
else (_PosArgs[tuple(t_args)], t_result))
if t_args is Ellipsis:
ga_args = _args
else:
ga_args = _PosArgs[tuple(t_args)], t_result

return super().__new__(cls, origin, ga_args)


def __repr__(self):
if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
return super().__repr__()
t_args = self.__args__[0]
if t_args.__args__ == ((),):
t_args_repr = '[]'
else:
t_args_repr = f'[{", ".join(_type_repr(a) for a in t_args.__args__)}]'
origin = _type_repr(self.__origin__)

if len(self.__args__) == 2 and t_args is Ellipsis:
return super().__repr__()
t_args_repr = ('[]' if t_args.__args__ == ((),) else
f'[{", ".join(_type_repr(a) for a in t_args.__args__)}]')

return f"{origin}[{t_args_repr}, {_type_repr(self.__args__[-1])}]"

def __reduce__(self):
Expand All @@ -490,7 +485,7 @@ def __reduce__(self):
def _type_repr(obj):
"""Return the repr() of an object, special-casing types (internal helper).

Borrowed from :mod:`typing` without importing since collections.abc
Copied from :mod:`typing` since collections.abc
shouldn't depend on that module.
"""
if isinstance(obj, GenericAlias):
Expand Down
1 change: 1 addition & 0 deletions Lib/collections/abc.py
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, _PosArgs, _PosArgsGenericAlias
3 changes: 2 additions & 1 deletion Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
defaultdict, deque, OrderedDict, Counter, UserDict, UserList
)
from collections.abc import *
from collections.abc import _PosArgs
from concurrent.futures import Future
from concurrent.futures.thread import _WorkItem
from contextlib import AbstractContextManager, AbstractAsyncContextManager
Expand Down Expand Up @@ -308,7 +309,7 @@ def test_abc_callable(self):
self.assertEqual(alias.__args__, (_PosArgs[int, str], float))
self.assertEqual(alias.__parameters__, ())

with self.subTest("Testing nstance checks"):
with self.subTest("Testing instance checks"):
self.assertIsInstance(alias, GenericAlias)

invalid_params = ('Callable[int]', 'Callable[int, str]')
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ def __call__(self):
## with self.assertRaises(TypeError):
## T2[int, str]

self.assertEqual(repr(C1[[int], int]).split('.')[-1], 'C1[[int], int]')
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)
Expand Down Expand Up @@ -1858,8 +1858,8 @@ class MyTup(Tuple[T, T]): ...
self.assertEqual(MyTup[int]().__orig_class__, MyTup[int])
class MyCall(Callable[..., T]):
def __call__(self): return None
self.assertIs(MyCall[[T], T]().__class__, MyCall)
self.assertEqual(MyCall[[T], T]().__orig_class__, MyCall[[T], T])
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])
Expand Down
11 changes: 6 additions & 5 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
'Text',
'TYPE_CHECKING',
'TypeAlias',
'_PosArgs', # Not meant to be imported, just for pickling.
]

# The pseudo-submodules 're' and 'io' are part of the public
Expand Down Expand Up @@ -878,11 +877,13 @@ def __ror__(self, right):
class _CallableGenericAlias(_GenericAlias, _root=True):
def __repr__(self):
assert self._name == 'Callable'
t_args = self.__args__[0]
if len(self.__args__) == 2 and t_args is Ellipsis:
if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
return super().__repr__()
t_args_repr = ('[]' if t_args.__args__ == ((),) else
f'[{", ".join(_type_repr(a) for a in t_args.__args__)}]')
t_args = self.__args__[0]
if t_args.__args__ == ((),):
t_args_repr = '[]'
else:
t_args_repr = f'[{", ".join(_type_repr(a) for a in t_args.__args__)}]'
return (f'typing.Callable'
f'[{t_args_repr}, '
f'{_type_repr(self.__args__[-1])}]')
Expand Down
3 changes: 0 additions & 3 deletions Objects/genericaliasobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,17 +614,14 @@ ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) {
return NULL;
}

PyObject *origin = PyTuple_GET_ITEM(args, 0);
PyObject *arguments = PyTuple_GET_ITEM(args, 1);

PyObject *self = (PyObject *)create_ga(type, origin, arguments);
if (self == NULL) {
Py_DECREF(origin);
Py_DECREF(arguments);
return NULL;
}

return self;
}

Expand Down
0