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 — gh-109118: Make comprehensions work within annotation scopes, but without inlining by JelleZijlstra · Pull Request #118160 · python/cpython

Conversation

@JelleZijlstra

@JelleZijlstra JelleZijlstra commented Apr 22, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

This is what I have so far to allow comprehensions within annotation scopes. The first commit changes the tests to what I think is the right behavior, and removes the SyntaxError on comprehensions in annotation scopes. These tests fail because of comprehension inlining; you can see that analogous tests work correctly with genexps (which are not inlined) but not with listcomps (which are). The second commit is an attempt to fix the tests, inspired by #104528, but it does not work yet.

cc @carljm

  • Issue: Annotation scopes containing nested scopes #109118

Sorry, something went wrong.

Uh oh!

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

All reactions
JelleZijlstra added 2 commits April 22, 2024 13:19
@JelleZijlstra
Desired behavior
ad56c5f
@JelleZijlstra
not correct
5ade8d4
@bedevere-app bedevere-app Bot mentioned this pull request Apr 22, 2024
Annotation scopes containing nested scopes #109118
Closed
@JelleZijlstra JelleZijlstra mentioned this pull request Apr 22, 2024
gh-109118: Allow lambdas in annotation scopes in classes #118019
Merged
JelleZijlstra
JelleZijlstra commented Apr 22, 2024
View reviewed changes
Comment thread Lib/test/test_type_params.py Resolved

Uh oh!

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

@carljm

carljm commented Apr 24, 2024

Copy link
Copy Markdown
Member

I looked at this a bit, and my initial conclusions aren't promising. I think this may not really be possible to support, at least not without extensive hackery.

If we consider this test case:

            class C[T]:
                T = "class"
                class Inner[U](make_base([T for _ in (1,)]), make_base(T)):
                    pass

The problem is that in the annotation scope of Inner, there is already a reference to T (in make_base(T)). The goal of this PR is to inline the comprehension [T for _ in (1,)] into that same scope, but somehow have the inlined T symbol behave differently from the existing T symbol in the same scope. Since all symbol information is tracked by name, that would require renaming the inlined T, somehow tracking that the renamed symbol is "really" named T, and extending that knowledge into the compiler. That sounds quite painful.

I think the more feasible options are either to disable comprehension inlining in annotation scopes, or to accept that the visibility effects of comprehension inlining leak here.

Given that comprehension inlining is usually transparent, and that complex code in annotation scopes is unlikely, I think just disabling comprehension inlining in this case is reasonable.

(If I had a time machine, I think I would inline comprehensions only in function scopes and nowhere else, as I'd initially proposed in the first version of PEP 709.)

JelleZijlstra reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

JelleZijlstra added 4 commits April 25, 2024 21:03
@JelleZijlstra
Revert "not correct"
ba90c97
This reverts commit 5ade8d4.
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into lambdaanno
28e78a6
@JelleZijlstra
Do not inline them
1a9b8ed
@JelleZijlstra
Rename "error_cases"
79e9332
@JelleZijlstra

JelleZijlstra commented Apr 26, 2024

Copy link
Copy Markdown
Member Author

Thanks. I can't think of a tractable solution either, so I pushed the non-inlining version.

carljm reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@JelleZijlstra JelleZijlstra changed the title gh-109118: Attempt to make comprehensions work within annotation scopes gh-109118: Make comprehensions work within annotation scopes, but without inlining Apr 26, 2024
@JelleZijlstra
JelleZijlstra marked this pull request as ready for review April 26, 2024 04:20
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 26, 2024
carljm
carljm approved these changes Apr 26, 2024
View reviewed changes

@carljm carljm 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.

Thank you!

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Doc/whatsnew/3.13.rst Resolved

Uh oh!

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

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Apr 26, 2024
@JelleZijlstra @carljm
Update Doc/whatsnew/3.13.rst
9e6ade7
Co-authored-by: Carl Meyer <[email protected]>
@JelleZijlstra
JelleZijlstra merged commit 2326d6c into python:main Apr 28, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 28, 2024
@JelleZijlstra
JelleZijlstra deleted the lambdaanno branch April 28, 2024 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@carljm carljm carljm approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

2 participants

@JelleZijlstra @carljm
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.