Skip to content

gh-137814: Fix __qualname__ of __annotate__#137842

Open
JelleZijlstra wants to merge 7 commits intopython:mainfrom
JelleZijlstra:annotate-qualname
Open

gh-137814: Fix __qualname__ of __annotate__#137842
JelleZijlstra wants to merge 7 commits intopython:mainfrom
JelleZijlstra:annotate-qualname

Conversation

@JelleZijlstra
Copy link
Copy Markdown
Member

@JelleZijlstra JelleZijlstra commented Aug 16, 2025

PyObject *ste_id; /* int: key in ste_table->st_blocks */
PyObject *ste_symbols; /* dict: variable names to flags */
PyObject *ste_name; /* string: name of current block */
PyObject *ste_function_name; /* string or NULL: for annotation blocks: name of the corresponding functions */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can’t use ste_name for this?

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.

It's possible but I don't know if it's better. I just pushed a version that does this.

return Py_NewRef(&_Py_ID(__annotate__));
}
return Py_NewRef(ste->ste_name);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was assuming that the ste_name of an annotations ste would be set to __annotations__ so you wouldn’t need to do this. Is that not possible?

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.

Your previous comment suggested using the ste_name to store the name of the corresponding function instead. We can't do both.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we set the ste_name to funcname.__annotations__?

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.

Then we'd end up with that in the __name__ of the function. I guess we could do that, but having a period in the __name__ seems odd.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, I'm not sure which option is best. I'll approve with whichever you choose.

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.

There are at least two options, the initial version of this PR (adding ste_function_name) and the one I pushed earlier this morning after your comment (overloading ste_name). The advantage of the ste_function_name version is that it's simpler: less code and ste_name always means the same thing. The advantage of the version that overloads ste_name is that we save a little on memory usage: one fewer pointer in the symbol table struct.

Personally I prefer the ste_function_name version since it's simpler even if it uses slightly more memory during compilation, but if you prefer the other version, I'm OK with that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm happy with the original version. Thank you for indulging my musings.

@JelleZijlstra
Copy link
Copy Markdown
Member Author

I mentioned on the issue that this needs a magic number bump. Maybe that makes it not worth backporting into 3.14?

JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this pull request Apr 7, 2026
… the interpreter

I'd still like to do python#137842 on 3.15+, but that requires changing bytecode and we can't
really afford to do that in 3.14. So to fix this in 3.14, let's patch things up in the
ceval loop instead.

This is safe because the compiler only sets __annotate__ to just-created dedicated
annotate functions.
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.

3 participants