Skip to content

Commit f768678

Browse files
committed
pythongh-119180: Rename parameter to __annotate__ functions
Larry Hastings pointed out that using an illegal parameter name makes it impossible to use inspect.signature() on annotate functions. Cross-ref python/peps#3993.
1 parent 17b3bc9 commit f768678

File tree

7 files changed

+15
-12
lines changed

7 files changed

+15
-12
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ struct _Py_global_strings {
4343
STRUCT_FOR_STR(defaults, ".defaults")
4444
STRUCT_FOR_STR(dot_locals, ".<locals>")
4545
STRUCT_FOR_STR(empty, "")
46-
STRUCT_FOR_STR(format, ".format")
4746
STRUCT_FOR_STR(generic_base, ".generic_base")
4847
STRUCT_FOR_STR(json_decoder, "json.decoder")
4948
STRUCT_FOR_STR(kwdefaults, ".kwdefaults")

Include/internal/pycore_runtime_init_generated.h

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_type_annotations.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import annotationlib
2+
import inspect
23
import textwrap
34
import types
45
import unittest
@@ -266,6 +267,17 @@ def check_annotations(self, f):
266267
f.__annotations__ = {"z": 43}
267268
self.assertIs(f.__annotate__, None)
268269

270+
def test_annotate_function_signature(self):
271+
def f(x: int): pass
272+
anno = f.__annotate__
273+
self.assertIsInstance(anno, types.FunctionType)
274+
self.assertEqual(f.__name__, "__annotate__")
275+
276+
expected_sig = inspect.Signature(
277+
[inspect.Parameter("__format__", inspect.Parameter.POSITIONAL_ONLY)]
278+
)
279+
self.assertEqual(inspect.signature(anno), expected_sig)
280+
269281

270282
class DeferredEvaluationTests(unittest.TestCase):
271283
def test_function(self):

Python/codegen.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,7 @@ codegen_setup_annotations_scope(compiler *c, location loc,
655655
codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS,
656656
key, loc.lineno, NULL, &umd));
657657

658-
// if .format != 1: raise NotImplementedError
659-
_Py_DECLARE_STR(format, ".format");
658+
// if __format__ != 1: raise NotImplementedError
660659
ADDOP_I(c, loc, LOAD_FAST, 0);
661660
ADDOP_LOAD_CONST(c, loc, _PyLong_GetOne());
662661
ADDOP_I(c, loc, COMPARE_OP, (Py_NE << 5) | compare_masks[Py_NE]);

Python/symtable.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,12 +1427,11 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
14271427
int result = symtable_enter_existing_block(st, ste);
14281428
Py_DECREF(ste);
14291429
if (block == AnnotationBlock || block == TypeVariableBlock || block == TypeAliasBlock) {
1430-
_Py_DECLARE_STR(format, ".format");
14311430
// We need to insert code that reads this "parameter" to the function.
1432-
if (!symtable_add_def(st, &_Py_STR(format), DEF_PARAM, loc)) {
1431+
if (!symtable_add_def(st, &_Py_ID(__format__), DEF_PARAM, loc)) {
14331432
return 0;
14341433
}
1435-
if (!symtable_add_def(st, &_Py_STR(format), USE, loc)) {
1434+
if (!symtable_add_def(st, &_Py_ID(__format__), USE, loc)) {
14361435
return 0;
14371436
}
14381437
}

0 commit comments

Comments
 (0)