-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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-102721: Improve coverage of _collections_abc._CallableGenericAlias
#102722
Conversation
Co-authored-by: Alex Waygood <[email protected]>
# A special case in PEP 612 where if X = Callable[P, int], | ||
# then X[int, str] == X[[int, str]]. | ||
if (len(self.__parameters__) == 1 | ||
and _is_param_expr(self.__parameters__[0]) | ||
and item and not _is_param_expr(item[0])): | ||
item = (item,) |
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.
This makes me slightly nervous, but I can't find any changes in behaviour from removing this code.
@Fidget-Spinner and @serhiy-storchaka, can either of you think of anything bad that could happen if we remove this code? The whole test suite passes with it removed.
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.
Yes, me too. Let's wait for others to comment.
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.
It makes me nervous too. In case you're looking for an example that triggers this path, here's one:
from typing import ParamSpec
P = ParamSpec("P")
from collections.abc import Callable
C = Callable[[P], int]
print(C[str])
However I agree that if I just comment this out, the output is the same. It's possible that @serhiy-storchaka fixed the issue in the C code (the super()
method) -- the code here is from Aug 4th, 2021, while he touched _Py_subs_parameters
on May 8th, 2022.
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.
Yes, I found examples that triggered this code path, but for all of the examples I found, I couldn't find any differences in behaviour with this code deleted (repr, __args__
, __parameters__
, equality)
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.
Someone could trace through the C code that gets called and see if it does the right thing.
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.
Looks like _unpack_args
C function which was added in May now handles this case: https://github.com/python/cpython/blame/2dc94634b50f0e5e207787e5ac1d56c68b22c3ae/Objects/genericaliasobject.c#L361
It unpacks nested tuples to a flat format, so - this code is not needed anymore.
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.
I don't suppose the pure-Python code could be useful for PyPy, could it? (Don't see how it could, just throwing that out there)
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.
Sorry, the only other iterperter I worked on is RustPython
.
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.
Looks good to me other than my nervousness about https://github.com/python/cpython/pull/102722/files#r1137196475
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.
Don’t worry about PyPy.
Thanks @sobolevn! I'd like to backport the coverage improvements, but not the code changes. |
Do you want me to do that? Or do you want to do that yourself? I am fine with both :) |
Would be great if you could take care of it, thanks! |
Scheduled for tomorrow then :) |
* main: (34 commits) pythongh-102701: Fix overflow in dictobject.c (pythonGH-102750) pythonGH-78530: add support for generators in `asyncio.wait` (python#102761) Increase stack reserve size for Windows debug builds to avoid test crashes (pythonGH-102764) pythongh-102755: Add PyErr_DisplayException(exc) (python#102756) Fix outdated note about 'int' rounding or truncating (python#102736) pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (python#102760) pythongh-99726: Improves correctness of stat results for Windows, and uses faster API when available (pythonGH-102149) pythongh-102192: remove redundant exception fields from ssl module socket (python#102466) pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (python#102743) pythongh-102737: Un-ignore ceval.c in the CI globals check (pythongh-102745) pythonGH-102748: remove legacy support for generator based coroutines from `asyncio.iscoroutine` (python#102749) pythongh-102721: Improve coverage of `_collections_abc._CallableGenericAlias` (python#102722) pythonGH-102653: Make recipe docstring show the correct distribution (python#102742) Add comments to `{typing,_collections_abc}._type_repr` about each other (python#102752) pythongh-102594: PyErr_SetObject adds note to exception raised on normalization error (python#102675) pythongh-94440: Fix issue of ProcessPoolExecutor shutdown hanging (python#94468) pythonGH-100112: avoid using iterable coroutines in asyncio internally (python#100128) pythongh-102690: Use Edge as fallback in webbrowser instead of IE (python#102691) pythongh-102660: Fix Refleaks in import.c (python#102744) pythongh-102738: remove from cases generator the code related to register instructions (python#102739) ...
…icAlias` (python#102722) Co-authored-by: Alex Waygood <[email protected]>
…icAlias` (python#102722) Co-authored-by: Alex Waygood <[email protected]>
Two main changes:
pickle
tests with different callables0
is not allowed, so I think testing this behaviour won't hurt. The test is quite simpleAll tests work for both
typing.Callable
and_collections_abc.Callable
.I also went ahead and remove this suspicious
if
from #102681 (comment)Moreover, all tests pass without it.
I cannot find any examples where it is needed. Please, prove me wrong.
CC @AlexWaygood
_collections_abc._CallableGenericAlias
#102721