Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-109118: Make comprehensions work within annotation scopes, but without inlining#118160

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 7 commits intopython:mainfromJelleZijlstra:lambdaanno
Apr 28, 2024

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstraJelleZijlstra commentedApr 22, 2024
edited by bedevere-appbot
Loading

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

@carljm
Copy link
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 ofInner, there is already a reference toT (inmake_base(T)). The goal of this PR is to inline the comprehension[T for _ in (1,)] into that same scope, but somehow have the inlinedT symbol behave differently from the existingT symbol in the same scope. Since all symbol information is tracked by name, that would require renaming the inlinedT, somehow tracking that the renamed symbol is "really" namedT, 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

@JelleZijlstra
Copy link
MemberAuthor

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

carljm reacted with thumbs up emoji

@JelleZijlstraJelleZijlstra changed the titlegh-109118: Attempt to make comprehensions work within annotation scopesgh-109118: Make comprehensions work within annotation scopes, but without inliningApr 26, 2024
@JelleZijlstraJelleZijlstra marked this pull request as ready for reviewApril 26, 2024 04:20
Copy link
Member

@carljmcarljm left a comment

Choose a reason for hiding this comment

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

Thank you!

Co-authored-by: Carl Meyer <carl@oddbird.net>
@JelleZijlstraJelleZijlstra merged commit2326d6c intopython:mainApr 28, 2024
36 checks passed
@JelleZijlstraJelleZijlstra deleted the lambdaanno branchApril 28, 2024 13:21
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@carljmcarljmcarljm approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@JelleZijlstra@carljm

[8]ページ先頭

©2009-2025 Movatter.jp