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
20157 8000 38
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
refactor C code, add deprecation warning for 3.9
  • Loading branch information
Fidget-Spinner committed Dec 6, 2020
commit a7896206ea64ee9ec781aa4a97c13cae3a71d6e9
18 changes: 16 additions & 2 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,28 @@ class _CallableGenericAlias(GenericAlias):
__slots__ = ()

def __new__(cls, origin, args):
Copy link
Member

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 both TypeErrors below) and return what was returned in 3.9.0.

try:
return cls.__create_ga(origin, args)
except TypeError as exc:
if sys.version_info[:2] == (3, 9):
import warnings
warnings.warn(f'{str(exc)} '
f'(This will raise TypeError in Python 3.10)',
DeprecationWarning)
return GenericAlias(origin, args)
else:
raise exc

@classmethod
def __create_ga(cls, origin, args):
if not isinstance(args, tuple) or len(args) != 2:
raise TypeError(
"Callable must be used as Callable[[arg, ...], result]")
"Callable must be used as Callable[[arg, ...], result].")
t_args, t_result = args
if not isinstance(t_args, (list, EllipsisType)):
raise TypeError(
f"Callable[args, result]: args must be a list or Ellipsis. "
f"Got {_type_repr(t_args)}")
f"Got {_type_repr(t_args)} instead.")
if t_args is Ellipsis:
ga_args = args
else:
Expand Down
55 changes: 29 additions & 26 deletions Objects/genericaliasobject.c
10000
Original file line number Diff line number Diff line change
Expand Up @@ -564,43 +564,30 @@ static PyGetSetDef ga_properties[] = {
{0}
};

static inline gaobject *
create_ga(PyTypeObject *type, PyObject *origin, PyObject *args) {
/* A helper function to create GenericAlias' args tuple and set its attributes.
* Returns 1 on success, 0 on failure.
*/
static inline int
setup_ga(gaobject *alias, PyObject *origin, PyObject *args) {
if (alias == NULL) {
return 0;
}
if (!PyTuple_Check(args)) {
args = PyTuple_Pack(1, args);
if (args == NULL) {
return NULL;
return 0;
}
}
else {
Py_INCREF(args);
}

gaobject *alias;
int gc_should_track = 0;
if (type != NULL) {
assert(type->tp_alloc != NULL);
alias = (gaobject *)type->tp_alloc(type, 0);
}
else {
alias = PyObject_GC_New(gaobject, &Py_GenericAliasType);
gc_should_track = 1;
}

if (alias == NULL) {
Py_DECREF(args);
return NULL;
}

Py_INCREF(origin);
alias->origin = origin;
alias->args = args;
alias->parameters = NULL;
alias->weakreflist = NULL;
if (gc_should_track) {
_PyObject_GC_TRACK(alias);
}
return alias;
return 1;
}

static PyObject *
Expand All @@ -615,8 +602,15 @@ ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
PyObject *origin = PyTuple_GET_ITEM(args, 0);
PyObject *arguments = PyTuple_GET_ITEM(args, 1);
PyObject *self = (PyObject *)create_ga(type, origin, arguments);
return self;
gaobject *self = (gaobject *)type->tp_alloc(type, 0);
if (self == NULL) {
return NULL;
}
if (!setup_ga(self, origin, arguments)) {
type->tp_free(self);
return NULL;
}
return (PyObject *)self;
}

static PyNumberMethods ga_as_number = {
Expand Down Expand Up @@ -656,5 +650,14 @@ PyTypeObject Py_GenericAliasType = {
PyObject *
Py_GenericAlias(PyObject *origin, PyObject *args)
{
return (PyObject *)create_ga(NULL, origin, args);
gaobject *alias = PyObject_GC_New(gaobject, &Py_GenericAliasType);
if (alias == NULL) {
return NULL;
}
if (!setup_ga(alias, origin, args)) {
PyObject_GC_Del(alias);
return NULL;
}
_PyObject_GC_TRACK(alias);
return (PyObject *)alias;
}
0