| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -77,6 +77,11 @@ class A(builtins.object) | |
| | __weakref__%s | ||
|
|
||
| class B(builtins.object) | ||
| | Methods defined here: | ||
| | | ||
| | __annotate__(...) | ||
|
Comment thread
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality(It would be great if we could generate a nice __text_signature__ for these generated methods, but that definitely doesn't need to be done in this PR)
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThey're actually Python function, so I wonder why pydoc doesn't pick them up. Possibly because the parameter is called .format.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIndeed: >>> inspect.signature(f.__annotate__)
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
inspect.signature(f.__annotate__)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
File "/Users/jelle/py/cpython/Lib/inspect.py", line 3329, in signature
return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
globals=globals, locals=locals, eval_str=eval_str)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jelle/py/cpython/Lib/inspect.py", line 3055, in from_callable
return _signature_from_callable(obj, sigcls=cls,
follow_wrapper_chains=follow_wrapped,
globals=globals, locals=locals, eval_str=eval_str)
File "/Users/jelle/py/cpython/Lib/inspect.py", line 2558, in _signature_from_callable
return _signature_from_function(sigcls, obj,
skip_bound_arg=skip_bound_arg,
globals=globals, locals=locals, eval_str=eval_str)
File "/Users/jelle/py/cpython/Lib/inspect.py", line 2403, in _signature_from_function
parameters.append(Parameter(name, annotation=annotation,
~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
kind=kind))
^^^^^^^^^^
File "/Users/jelle/py/cpython/Lib/inspect.py", line 2750, in __init__
raise ValueError('{!r} is not a valid parameter name'.format(name))
ValueError: '.format' is not a valid parameter name
Maybe in a followup PR we can figure out a way to make this work. The parameter has to have an illegal name so it doesn't shadow any symbol name that the user might have used.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
I'm not sure I 100% understand this point, and no tests seem to fail if I make this change locally to your PR branch and recompile (and I verified that it means that __annotate__ methods have valid signatures as per inspect.signature): Detailsdiff --git a/Include/internal/pycore_global_strings.h b/Include/internal/pycore_global_strings.h
index 009802c4416..899b19ae941 100644
--- a/Include/internal/pycore_global_strings.h
+++ b/Include/internal/pycore_global_strings.h
@@ -45,7 +45,7 @@ struct _Py_global_strings {
STRUCT_FOR_STR(dot, ".")
STRUCT_FOR_STR(dot_locals, ".<locals>")
STRUCT_FOR_STR(empty, "")
- STRUCT_FOR_STR(format, ".format")
+ STRUCT_FOR_STR(format, "format")
STRUCT_FOR_STR(generic_base, ".generic_base")
STRUCT_FOR_STR(json_decoder, "json.decoder")
STRUCT_FOR_STR(kwdefaults, ".kwdefaults")
diff --git a/Include/internal/pycore_runtime_init_generated.h b/Include/internal/pycore_runtime_init_generated.h
index ff5b6ee8e0f..e8b5173a457 100644
--- a/Include/internal/pycore_runtime_init_generated.h
+++ b/Include/internal/pycore_runtime_init_generated.h
@@ -554,7 +554,7 @@ extern "C" {
INIT_STR(dot, "."), \
INIT_STR(dot_locals, ".<locals>"), \
INIT_STR(empty, ""), \
- INIT_STR(format, ".format"), \
+ INIT_STR(format, "format"), \
INIT_STR(generic_base, ".generic_base"), \
INIT_STR(json_decoder, "json.decoder"), \
INIT_STR(kwdefaults, ".kwdefaults"), \
diff --git a/Python/compile.c b/Python/compile.c
index c3372766d0b..7535067ded0 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1447,7 +1447,7 @@ compiler_setup_annotations_scope(struct compiler *c, location loc,
}
c->u->u_metadata.u_posonlyargcount = 1;
// if .format != 1: raise NotImplementedError
- _Py_DECLARE_STR(format, ".format");
+ _Py_DECLARE_STR(format, "format");
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]);
diff --git a/Python/symtable.c b/Python/symtable.c
index 23fc4a0ec03..0154092a905 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -2511,7 +2511,7 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
}
}
- _Py_DECLARE_STR(format, ".format");
+ _Py_DECLARE_STR(format, "format");
// The generated __annotate__ function takes a single parameter with the
// internal name ".format".
if (!symtable_add_def(st, &_Py_STR(format), DEF_PARAM,
@@ -2570,7 +2570,7 @@ symtable_visit_annotations(struct symtable *st, stmt_ty o, arguments_ty a, expr_
return 0;
}
}
- _Py_DECLARE_STR(format, ".format");
+ _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, LOCATION(o))) {
return 0;This also might be worth a brief mention in PEP-749, since PEP-649 seems to specify that the signature of __annotate__ should be __annotate__(format: int). In any event, this can definitely be tackled in a followup; I don't want to block this PR!
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityTry something like this: class format: pass def f(x: format): pass print(f.__annotations__)
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI see, makes sense! Worth adding a test like that?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualitySure, the problem is that any such test can only tell us about one specific name.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityRight, it doesn't verify any fundamental invariants about __annotate__ — but I still think it's useful to have such a test, so that the CI goes obviously red if someone else comes along in the future and tries to do the "obvious fix" to get inspect.signature() working for these methods. Thanks for adding it!
Sorry, something went wrong.
All reactions
|
||
| | | ||
| | ---------------------------------------------------------------------- | ||
| | Data descriptors defined here: | ||
| | | ||
| | __dict__%s | ||
| Expand All | @@ -87,8 +92,6 @@ class B(builtins.object) | |
| | Data and other attributes defined here: | ||
| | | ||
| | NO_MEANING = 'eggs' | ||
| | | ||
| | __annotations__ = {'NO_MEANING': <class 'str'>} | ||
|
|
||
| class C(builtins.object) | ||
| | Methods defined here: | ||
| Expand Down Expand Up | @@ -176,6 +179,9 @@ class A(builtins.object) | |
| list of weak references to the object | ||
|
|
||
| class B(builtins.object) | ||
| Methods defined here: | ||
| __annotate__(...) | ||
| ---------------------------------------------------------------------- | ||
| Data descriptors defined here: | ||
| __dict__ | ||
| dictionary for instance variables | ||
| Expand All | @@ -184,7 +190,6 @@ class B(builtins.object) | |
| ---------------------------------------------------------------------- | ||
| Data and other attributes defined here: | ||
| NO_MEANING = 'eggs' | ||
| __annotations__ = {'NO_MEANING': <class 'str'>} | ||
|
|
||
|
|
||
| class C(builtins.object) | ||
| Expand Down | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -105,7 +105,7 @@ def test_runsource_shows_syntax_error_for_failed_compilation(self): | |
|
|
||
| def test_no_active_future(self): | ||
| console = InteractiveColoredConsole() | ||
| source = "x: int = 1; print(__annotations__)" | ||
| source = "x: int = 1; print(__annotate__(1))" | ||
|
Comment thread
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWhy does printing __annotations__ not work here? Shouldn't it call __annotate__ under the hood anyway?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityPrinting a global doesn't go through the descriptor protocol, so printing __annotations__ will just throw a NameError; it is added to the namespace only when the __annotate__ descriptor is called.
Sorry, something went wrong.
carljm reacted with thumbs up emoji
All reactions
|
||
| f = io.StringIO() | ||
| with contextlib.redirect_stdout(f): | ||
| result = console.runsource(source) | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.