GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-43224: Forbid TypeVar substitution with Unpack by serhiy-storchaka · Pull Request #32031 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Mar 21, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

https://bugs.python.org/issue43224

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
bpo-43224: Forbid TypeVar substitution with Unpack
8cfe3eb
@serhiy-storchaka serhiy-storchaka added type-feature A feature request or enhancement skip news labels Mar 21, 2022
@serhiy-storchaka
serhiy-storchaka requested review from Fidget-Spinner, JelleZijlstra and gvanrossum as code owners March 21, 2022 18:08
@bedevere-bot bedevere-bot added the awaiting core review label Mar 21, 2022
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Mar 21, 2022
gvanrossum
gvanrossum reviewed Mar 21, 2022
View reviewed changes
Comment thread Lib/test/test_typing.py
for A in G, Tuple:
B = A[T, Unpack[Ts], str, T2]
with self.assertRaises(TypeError):
B[int, Unpack[Ts]]

@gvanrossum gvanrossum Mar 21, 2022

Copy link
Copy Markdown
Member

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.

Remind me where PEP 646 forbids this? Assuming Unpack[Ts] === *Ts, it seems this is forbidding the following:

class G(Generic[*Ts]): ...
B = G[T, *Ts, str, T2]
X = B[int, *Ts]  # <-- TypeError here

What is wrong with X that isn't wrong with B?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Mar 22, 2022

Copy link
Copy Markdown
Member Author

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'll rewrite it as B[int, *Ts2], so we could unambiguously refer to variables.

AFAIK PEP 646 does not cover such case. It does not specify the result.

But what can be the result of substitution B[int, *Ts2]? T is substituted with int, T2 should be substituted with the last item of *Ts2, and *T should be substituted with all but the last items of *Ts2. But we cannot express this, because Ts2 is a variable. If we substitute *Ts2 with an empty sequence of types (X[()]), there would not be a value for T2.

I think that it is better to make it an error:

def f(a: tuple[*Ts2]) -> B[int, *Ts2]: ...

You should write

def f(a: tuple[*Ts2, T3]) -> B[int, *Ts2, T3]: ...

to make it having some sense.

There is a workaround of this problem. It may even make the code more clear. You can see, that a should be a tuple containing at least 1 item. T2 in B will be substituted with the type of the last item, and *Ts in B will be substituted with the rest of types.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@JelleZijlstra JelleZijlstra Apr 11, 2022

Copy link
Copy Markdown
Member

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.

@mrahtz what do you think of this case?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mrahtz mrahtz Apr 11, 2022

Copy link
Copy Markdown
Contributor

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.

Thanks for the prod, Jelle. I agree with Serhiy - looks like this is one of the cases we should forbid at runtime.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
JelleZijlstra
JelleZijlstra approved these changes Apr 11, 2022
View reviewed changes

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

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 in principle, pending resolution of Guido's question.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Apr 11, 2022
@mrahtz mrahtz mannequin mentioned this pull request Apr 11, 2022
Add support for PEP 646 #87390
Closed
@JelleZijlstra
JelleZijlstra merged commit 15537c5 into python:main Apr 13, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Apr 13, 2022
@serhiy-storchaka
serhiy-storchaka deleted the typing-typevar-subst-unpack branch July 1, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gvanrossum gvanrossum gvanrossum left review comments
@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner
+1 more reviewer
@mrahtz mrahtz mrahtz left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@serhiy-storchaka @JelleZijlstra @gvanrossum @mrahtz @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.