8000 Improve test cases for type substitution of a list of types after initial ParamSpec substitution · Issue #103054 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Improve test cases for type substitution of a list of types after initial ParamSpec substitution #103054

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

Closed
sobolevn opened this issue Mar 27, 2023 · 1 comment
Assignees
Labels
tests Tests in the Lib/test dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member
sobolevn commented Mar 27, 2023

After merging #102808 I realized that I've missed several important cases.

Right now substitution algorithm has these lines:

cpython/Lib/typing.py

Lines 1477 to 1488 in 2cdc518

if self.__origin__ == collections.abc.Callable and isinstance(new_arg, tuple):
# Consider the following `Callable`.
# C = Callable[[int], str]
# Here, `C.__args__` should be (int, str) - NOT ([int], str).
# That means that if we had something like...
# P = ParamSpec('P')
# T = TypeVar('T')
# C = Callable[P, T]
# D = C[[int, str], float]
# ...we need to be careful; `new_args` should end up as
# `(int, str, float)` rather than `([int, str], float)`.
new_args.extend(new_arg)

It is tested. But, it is never tested for the nested arguments:

cpython/Lib/typing.py

Lines 1500 to 1511 in 2cdc518

elif isinstance(old_arg, tuple):
# Corner case:
# P = ParamSpec('P')
# T = TypeVar('T')
# class Base(Generic[P]): ...
# Can be substituted like this:
# X = Base[[int, T]]
# In this case, `old_arg` will be a tuple:
new_args.append(
tuple(self._make_substitution(old_arg, new_arg_by_param)),
)
else:

I think that Callable is complex and important enought to be covered with as many cases as possible. Furthermore, Callable can be nested deeply in real types and we need to be sure that this use-case works as intended.

I will send a PR with more tests :)

Related #88965

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error tests Tests in the Lib/test dir topic-typing labels Mar 27, 2023
@sobolevn sobolevn self-assigned this Mar 27, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Mar 27, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Mar 27, 2023
JelleZijlstra pushed a commit that referenced this issue Mar 29, 2023
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Eclips4 <80244920+Eclips4@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 29, 2023
…ythonGH-103055)

(cherry picked from commit 60bdc16)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Eclips4 <80244920+Eclips4@users.noreply.github.com>
miss-islington added a commit that referenced this issue Mar 29, 2023
)

(cherry picked from commit 60bdc16)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Eclips4 <80244920+Eclips4@users.noreply.github.com>
@AlexWaygood
Copy link
Member

Thanks Nikita!

warsaw pushed a commit to warsaw/cpython that referenced this issue Apr 11, 2023
…ython#103055)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Eclips4 <80244920+Eclips4@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants
0