FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-43224: Implement substitution of unpacked TypeVarTuple by serhiy-storchaka · Pull Request #31800 · python/cpython · GitHub

/ cpython Public

bpo-43224: Implement substitution of unpacked TypeVarTuple - #31800

Merged
serhiy-storchaka merged 5 commits into
python:mainfrom
serhiy-storchaka:typevartuple-subst
Mar 11, 2022
Merged

bpo-43224: Implement substitution of unpacked TypeVarTuple#31800
serhiy-storchaka merged 5 commits into
python:mainfrom
serhiy-storchaka:typevartuple-subst

Conversation

serhiy-storchaka commented Mar 10, 2022
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

Comment thread Lib/typing.py Outdated
raise NotImplementedError(
"Type substitution for TypeVarTuples is not yet implemented"
)
if len(self.__parameters__) == 1 and isinstance(self.__parameters__[0], TypeVarTuple):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'm not sure it's sufficient to only check for the case where len(self.__parameters__) == 1 - we could have e.g.:

T1 = TypeVar('T1')
T2 = TypeVar('T2')
Ts = TypeVarTuple('Ts')
class A(Generic[T1, T2, Unpack[Ts]]): pass
B = A[int, T2, Unpack[Ts]]
C = B[str, float]
print(C)

There, B.__parameters__ is (T2, Ts), causing a TypeError later on in the code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

You are right. The previous code only worked with existing tests. New code adds tests for generics with multiple type variables.

mrahtz commented Mar 10, 2022

Copy link
Copy Markdown
Contributor

Thanks for looking into this!

mrahtz commented Mar 12, 2022

Copy link
Copy Markdown
Contributor

Woah, I'm not sure merging this was a good idea. We discussed this extensively in #31021 (review) and decided against implementing it this way. There are too many edge cases (e.g. the merged implementation will happily assign an unpacked arbitrary-length tuple such as *tuple[int, ...] to a plain TypeVar, which is not legal - see the extensive tests in b9b1c80#diff-04d29c98076c2d6bb75921ea9becb26a862544d39b71db87b6e354c759b9305dL794), and @JelleZijlstra suspected it might cause issues with Annotated.

I'd strongly prefer this merge to be reversed in favour of #31804 and/or further discussion on what the right approach is.

Copy link
Copy Markdown
Member Author

I think that list[T][int] should return list[int], and tuple[*Ts][int, str] should return tuple[int, str].

Workaround can be used in cases where this does not work.

mrahtz commented Mar 13, 2022

Copy link
Copy Markdown
Contributor

If you're adamant about keeping this implementation, I don't feel happy about it, but I don't want this to become a blocker for us. I'll submit a new PR with extra test cases so we can be more confident we've covered the edge cases.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL