Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-137814: Fix __qualname__ of __annotate__
  • Loading branch information
JelleZijlstra committed Aug 16, 2025
commit f29b8b91563904e51c4898ad78207279f93a4715
1 change: 1 addition & 0 deletions Include/internal/pycore_symtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ typedef struct _symtable_entry {
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.

PyObject *ste_varnames; /* list of function parameters */
PyObject *ste_children; /* list of child blocks */
PyObject *ste_directives;/* locations of global and nonlocal statements */
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,17 @@ def test_complex_comprehension_inlining_exec(self):
genexp = annos["unique_name_2"][0]
lamb = list(genexp)[0]
self.assertEqual(lamb(), 42)

def test_annotate_qualname(self):
code = """
def f() -> None: pass
class Outer:
x: int
def method(self, x: int):
pass
"""
ns = run_code(code)
method = ns["Outer"].method
self.assertEqual(method.__annotate__.__qualname__, "Outer.method.__annotate__")
self.assertEqual(ns["f"].__annotate__.__qualname__, "f.__annotate__")
self.assertEqual(ns["Outer"].__annotate__.__qualname__, "Outer.__annotate__")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the ``__qualname__`` attribute of ``__annotate__`` functions on
functions.
11 changes: 11 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ compiler_set_qualname(compiler *c)
base = Py_NewRef(parent->u_metadata.u_qualname);
}
}
if (u->u_ste->ste_function_name != NULL) {
base = PyUnicode_FromFormat("%U.%U",
parent->u_metadata.u_qualname,
u->u_ste->ste_function_name);
if (base == NULL) {
return ERROR;
}
}
}
else if (u->u_ste->ste_function_name != NULL) {
base = Py_NewRef(u->u_ste->ste_function_name);
}

if (base != NULL) {
Expand Down
3 changes: 3 additions & 0 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,
ste->ste_id = k; /* ste owns reference to k */

ste->ste_name = Py_NewRef(name);
ste->ste_function_name = NULL;

ste->ste_symbols = NULL;
ste->ste_varnames = NULL;
Expand Down Expand Up @@ -185,6 +186,7 @@ ste_dealloc(PyObject *op)
ste->ste_table = NULL;
Py_XDECREF(ste->ste_id);
Py_XDECREF(ste->ste_name);
Py_XDECREF(ste->ste_function_name);
Py_XDECREF(ste->ste_symbols);
Py_XDECREF(ste->ste_varnames);
Py_XDECREF(ste->ste_children);
Expand Down Expand Up @@ -2830,6 +2832,7 @@ symtable_visit_annotations(struct symtable *st, stmt_ty o, arguments_ty a, expr_
(void *)a, LOCATION(o))) {
return 0;
}
Py_XSETREF(st->st_cur->ste_function_name, Py_NewRef(function_ste->ste_name));
if (is_in_class || current_type == ClassBlock) {
st->st_cur->ste_can_see_class_scope = 1;
if (!symtable_add_def(st, &_Py_ID(__classdict__), USE, LOCATION(o))) {
Expand Down
Loading