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-119180: Rename parameter to __annotate__ functions#124461

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

Closed
Closed
Show file tree
Hide file tree
Changes from1 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
NextNext commit
gh-119180: Rename parameter to __annotate__ functions
Larry Hastings pointed out that using an illegal parameter name makesit impossible to use inspect.signature() on annotate functions.Cross-refpython/peps#3993.
  • Loading branch information
@JelleZijlstra
JelleZijlstra committedSep 24, 2024
commitf76867841e7bf6e53371bd1aaaf309a227c60367
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

1 change: 0 additions & 1 deletionInclude/internal/pycore_global_strings.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,7 +43,6 @@ struct _Py_global_strings {
STRUCT_FOR_STR(defaults, ".defaults")
STRUCT_FOR_STR(dot_locals, ".<locals>")
STRUCT_FOR_STR(empty, "")
STRUCT_FOR_STR(format, ".format")
STRUCT_FOR_STR(generic_base, ".generic_base")
STRUCT_FOR_STR(json_decoder, "json.decoder")
STRUCT_FOR_STR(kwdefaults, ".kwdefaults")
Expand Down
1 change: 0 additions & 1 deletionInclude/internal/pycore_runtime_init_generated.h
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

4 changes: 0 additions & 4 deletionsInclude/internal/pycore_unicodeobject_generated.h
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

12 changes: 12 additions & 0 deletionsLib/test/test_type_annotations.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
import annotationlib
import inspect
import textwrap
import types
import unittest
Expand DownExpand Up@@ -266,6 +267,17 @@ def check_annotations(self, f):
f.__annotations__ = {"z": 43}
self.assertIs(f.__annotate__, None)

def test_annotate_function_signature(self):
def f(x: int): pass
anno = f.__annotate__
self.assertIsInstance(anno, types.FunctionType)
self.assertEqual(f.__name__, "__annotate__")

expected_sig = inspect.Signature(
[inspect.Parameter("__format__", inspect.Parameter.POSITIONAL_ONLY)]
Copy link
Member

Choose a reason for hiding this comment

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

__format__ is already used in a different context, as a method name. Can this be confusing?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I feel it's unlikely to cause much confusion, since the name will very rarely show up to users (only if they introspect annotate functions, which is very unlikely to happen), and in a context that doesn't have anything to do with the__format__ method.

Copy link
Member

Choose a reason for hiding this comment

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

Given that we are kind of picking a name out of thin air that we expect not to matter, it seems like we might as well avoid the potential for someone thinking this is related to__format__? Would there be an issue with just using_format? (I don't feel strongly about this at all.)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

We should use a dundered name because dundered name are reserved to the implementation. Users could use a class named_format in their annotations.

The current PR has this behavior:

>>> def f(x: __format__): pass... >>> f.__annotations__{'x': 1}

I think with a dunder name we can handwave that away with "don't do that", but a user could reasonably use the name_format.

Still we could use a different name like__fmt__ or__annotate__ (I think Larry suggested the latter, but that name feels more confusing than__format__).

Copy link
Member

Choose a reason for hiding this comment

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

Ah yeah, makes sense why it needs to be a dunder name. Given users should never have to type it, or likely see it, and the main thing we prefer to avoid is collisions with a user parameter, should we actually prefer something longer, like__annotation_format__?

AlexWaygood reacted with thumbs up emoji
)
self.assertEqual(inspect.signature(anno), expected_sig)


class DeferredEvaluationTests(unittest.TestCase):
def test_function(self):
Expand Down
3 changes: 1 addition & 2 deletionsPython/codegen.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -655,8 +655,7 @@ codegen_setup_annotations_scope(compiler *c, location loc,
codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS,
key, loc.lineno, NULL, &umd));

// if .format != 1: raise NotImplementedError
_Py_DECLARE_STR(format, ".format");
// if __format__ != 1: raise NotImplementedError
ADDOP_I(c, loc, LOAD_FAST, 0);
ADDOP_LOAD_CONST(c, loc, _PyLong_GetOne());
ADDOP_I(c, loc, COMPARE_OP, (Py_NE << 5) | compare_masks[Py_NE]);
Expand Down
5 changes: 2 additions & 3 deletionsPython/symtable.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1427,12 +1427,11 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
int result = symtable_enter_existing_block(st, ste);
Py_DECREF(ste);
if (block == AnnotationBlock || block == TypeVariableBlock || block == TypeAliasBlock) {
_Py_DECLARE_STR(format, ".format");
// We need to insert code that reads this "parameter" to the function.
if (!symtable_add_def(st, &_Py_STR(format), DEF_PARAM, loc)) {
if (!symtable_add_def(st, &_Py_ID(__format__), DEF_PARAM, loc)) {
return 0;
}
if (!symtable_add_def(st, &_Py_STR(format), USE, loc)) {
if (!symtable_add_def(st, &_Py_ID(__format__), USE, loc)) {
return 0;
}
}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp