Skip to content

gh-130881: Handle conditionally defined annotations#130935

Merged
JelleZijlstra merged 11 commits intopython:mainfrom
JelleZijlstra:conditional-anno
Mar 26, 2025
Merged

gh-130881: Handle conditionally defined annotations#130935
JelleZijlstra merged 11 commits intopython:mainfrom
JelleZijlstra:conditional-anno

Conversation

@JelleZijlstra
Copy link
Copy Markdown
Member

@JelleZijlstra JelleZijlstra commented Mar 7, 2025

Comment thread Python/codegen.c
Comment thread Python/codegen.c
Comment thread Python/codegen.c Outdated
Comment thread Python/compile.c Outdated
Comment thread Python/compile.c Outdated
Comment thread Python/symtable.c Outdated
Comment thread Python/symtable.c Outdated
@JelleZijlstra
Copy link
Copy Markdown
Member Author

Thanks @picnixz @tomasr8 for the reviews, please take another look!

@picnixz picnixz self-requested a review March 12, 2025 14:42
Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

I won't force you to follow PEP-7 as the file has a mix of everything which makes it very hard to choose a convention. You can also ignore my suggestion in symtable.c if you think it's not necessary and if the surrounding code is using the same writing style.

Comment thread Python/symtable.c
Comment thread Python/symtable.c
Comment thread Python/compile.c Outdated
Comment thread Python/compile.c
@JelleZijlstra JelleZijlstra requested a review from picnixz March 14, 2025 02:29
Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

I think I reviewed the changes, though I'm not entirely sure about the tests themselves so another eye is welcome

Comment thread Python/codegen.c Outdated
Comment thread Python/codegen.c Outdated
Comment thread Python/compile.c Outdated
@tomasr8
Copy link
Copy Markdown
Member

tomasr8 commented Mar 14, 2025

Once this is merged, should the text in PEP 649 be updated to reflect this change?

Code that sets annotations on module or class attributes from inside any kind of flow control statement. It’s currently possible to set module and class attributes with annotations inside an if or try statement, and it works as one would expect. It’s untenable to support this behavior when this PEP is active.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Copy link
Copy Markdown
Member

@tomasr8 tomasr8 left a comment

Choose a reason for hiding this comment

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

Just some comments/questions for the tests, otherwise looks good :)

Comment thread Lib/test/test_type_annotations.py Outdated
Comment thread Lib/test/test_type_annotations.py
Comment thread Lib/test/test_type_annotations.py
Comment thread Lib/test/test_type_annotations.py
Comment thread Lib/test/test_type_annotations.py
@JelleZijlstra
Copy link
Copy Markdown
Member Author

Once this is merged, should the text in PEP 649 be updated to reflect this change?

Code that sets annotations on module or class attributes from inside any kind of flow control statement. It’s currently possible to set module and class attributes with annotations inside an if or try statement, and it works as one would expect. It’s untenable to support this behavior when this PEP is active.

I don't own PEP 649 so I can't update it. Could add it to PEP 749 but I already submitted it to the SC.

Copy link
Copy Markdown
Member

@tomasr8 tomasr8 left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good to me :)

@tomasr8
Copy link
Copy Markdown
Member

tomasr8 commented Mar 16, 2025

Once this is merged, should the text in PEP 649 be updated to reflect this change?

Code that sets annotations on module or class attributes from inside any kind of flow control statement. It’s currently possible to set module and class attributes with annotations inside an if or try statement, and it works as one would expect. It’s untenable to support this behavior when this PEP is active.

I don't own PEP 649 so I can't update it. Could add it to PEP 749 but I already submitted it to the SC.

Larry is the owner. @larryhastings should the PEP be updated?

@larryhastings
Copy link
Copy Markdown
Contributor

Accepted PEPs are historical documents; as a rule I'm averse to changing them after they're accepted. PEP 749 is the perfect place to document this change to what's specified in 649.

Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Two nits I missed but otherwise, looks good to me

Comment thread Python/codegen.c Outdated
Comment thread Python/codegen.c Outdated
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@JelleZijlstra JelleZijlstra enabled auto-merge (squash) March 26, 2025 03:21
@JelleZijlstra JelleZijlstra merged commit 898e6b3 into python:main Mar 26, 2025
42 checks passed
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 1, 2025
JelleZijlstra pushed a commit to python/mypy that referenced this pull request Dec 10, 2025
On Python 3.14, this module attribute seems to exist in certain
situations:

```pycon
>>> if True:
...     a: str
...     
>>> __conditional_annotations__
{0}
```

It's a pretty esoteric attribute without any documentation. It seems to
have been added in python/cpython#130935

Either way, stubtest was complaining about it in scipy-stubs. So all
things considered, I figured it'd be best to just ignore it.
@SnoopJ
Copy link
Copy Markdown
Contributor

SnoopJ commented Apr 20, 2026

EDIT: I was reading PEP 749 too quickly and missed that there is a section on conditionally-defined annotations (because it does not mention the new dunder by name). I guess the PEP is pretty descriptive, but my question about adding doc of the dunder still stands, I think. I had to consult a blame for compile.c to find the PR that introduced the new identifier.

@JelleZijlstra I just stumbled across this functionality and was surprised that I had to dive into the codebase's history to find out what it was all about. Looks like the window for including this info in PEP 749 has passed, but should there perhaps be an issue for adding information about this feature to the Data Model docs?

@JelleZijlstra
Copy link
Copy Markdown
Member Author

The dunder is private. Do we have documentation for other purely internal dunders?

@SnoopJ
Copy link
Copy Markdown
Contributor

SnoopJ commented Apr 20, 2026

Do we have documentation for other purely internal dunders?

There is a section on internal types which might count towards answering that question? Downstreams I think would have fewer reasons for looking at __conditional_annotations__ (since most interested consumers would "just" consult the result of calling __annotate__ I understand the feature right?), and the storage isn't really a 'type'. module.__cached__ is perhaps also in the same vein.

I can see the argument for "nobody should need to know about this" but figured I'd ask on behalf of anyone else who was surprised to discover the dunder's existence and then have trouble dredging up more information about what it's for.

@JelleZijlstra
Copy link
Copy Markdown
Member Author

If you'd like this documented please open a new issue. But note that several other private class attributes, such as __basicsize__, are also undocumented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants