Skip to content
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

Merged
merged 2 commits into from
Mar 16, 2023

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Mar 15, 2023

Two main changes:

  1. More pickle tests with different callables
  2. Passing things like 0 is not allowed, so I think testing this behaviour won't hurt. The test is quite simple

All 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

Lib/test/test_typing.py Outdated Show resolved Hide resolved
Lib/test/test_typing.py Outdated Show resolved Hide resolved
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Comment on lines -487 to -492
# 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,)
Copy link
Member

@AlexWaygood AlexWaygood Mar 15, 2023

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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)

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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)

Copy link
Member Author

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.

Copy link
Member

@AlexWaygood AlexWaygood left a 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

Copy link
Member

@gvanrossum gvanrossum left a 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.

@AlexWaygood AlexWaygood merged commit fbe82fd into python:main Mar 16, 2023
@AlexWaygood
Copy link
Member

Thanks @sobolevn! I'd like to backport the coverage improvements, but not the code changes.

@sobolevn
Copy link
Member Author

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 :)

@AlexWaygood
Copy link
Member

Would be great if you could take care of it, thanks!

@sobolevn
Copy link
Member Author

Scheduled for tomorrow then :)

carljm added a commit to carljm/cpython that referenced this pull request Mar 17, 2023
* 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)
  ...
miss-islington pushed a commit that referenced this pull request Mar 17, 2023
…ricAlias` (GH-102788)

This is a backport of #102722 without the `typing.py` changes.

Automerge-Triggered-By: GH:AlexWaygood
miss-islington pushed a commit that referenced this pull request Mar 17, 2023
…ricAlias` (GH-102790)

This is a manual backport of #102722 but without `typing.py` changes and without `TypeVarTuple` case, because it was added in 3.11

Automerge-Triggered-By: GH:AlexWaygood
Fidget-Spinner pushed a commit to Fidget-Spinner/cpython that referenced this pull request Mar 27, 2023
…icAlias` (python#102722)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
…icAlias` (python#102722)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants