-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-126072: Set docstring attribute for module and class #126231
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Misc/NEWS.d/next/Core_and_Builtins/2024-10-31-21-49-00.gh-issue-126072.o9k8Ns.rst
Outdated
Show resolved
Hide resolved
1dc125e
to
8622d78
Compare
Just kindly to ask any further updates needed? I'm not sure whether it's proper to re-request review in CPython PR workflow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excuse formatting, reviewing on my phone.
Python/codegen.c
Outdated
Py_ssize_t first_instr = 0; | ||
PyObject *docstring = _PyAST_GetDocString(body); | ||
assert(OPTIMIZATION_LEVEL(c) < 2 || docstring == NULL); | ||
if (docstring) { | ||
assert(ste->ste_has_docstring); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it still true that GetDocstring returns non-null only when ste_has_docstring is true?
Perhaps it should be
if(ste_has_docstring) assert(GetDocstring)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently I think they are equivalent, i.e., ste_has_docstring == 1
iff. there is a docstring during symbol table generation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And when you run python -OO
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently when running with -OO
, the first docstring is removed and the subsequent docstring is converted into JoinedStr
in the AST stage. For _PyAST_GetDocString
, it will not identify the converted JoinedStr
as docstring, so ste_has_docstring
will not be set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test for docstring optimization under -OO
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently when running with
-OO
, the first docstring is removed and the subsequent docstring is converted intoJoinedStr
in the AST stage. For_PyAST_GetDocString
, it will not identify the convertedJoinedStr
as docstring, soste_has_docstring
will not be set.
Right, but we want to remove that hack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we do it in this PR?
d9c97a6
to
7db42a1
Compare
@iritkatriel Hi, I've encountered an issue when trying to remove f-string conversion in def func():
"not " + "a docstring"
|
I thought the idea is that it uses |
Sorry I'm a little confused now. 😂 If we remove the f-string conversion in the Lines 1846 to 1848 in 83ba8c2
The reason is If there is something I misunderstood, please correct me, thanks! |
I guess the problem is that we perform AST optimization before we build the symtable, so we have to keep doing the f-string conversion to avoid breaking the symtable. Maybe we can instead run the symtable before ast optimization, so we can get rid of the f-string trick in the AST optimizer. |
Yeah that's the problem. That's say, we cannot convey the information from AST construction to symtable generation phase.
Theoratically it's feasible, but I'm afraid that there are side-effects and the scope is beyond this PR. |
My proposal (in #126072 (comment)) was to move the docstring removal to the symtable, so it's all in one place. |
But I think the problem is still not addressed, we've lost some information after the AST optimization (the concatenated string has been optimized as constant string). Even though the docstring removal is put together with symtable, it still fails to distinguish in the above case. |
75bfe7b
to
0f06e57
Compare
2194f4f
to
390caaa
Compare
Revert to the version without swapping AST optimization and symtable generation. The docstring removal in |
No, this would mean we optimise less when we should be optimising more. I think we should leave symtable after ast_opt and try my suggestion to just move the docstring handling from ast_opt to symtable. |
As mentioned above, what blocks us doing so is if we leave the docstring handling to symtable, for the following code: def with_const_expression():
"aa" * 4 The optimized AST will be: Module(
body=[
FunctionDef(
name='with_const_expression',
args=arguments(),
body=[
Expr(
value=Constant(value='aaaaaaaa'))])]) Then in the symtable stage, it cannot distinguish whether the constant string |
Ok, I see. I think we should just forget about that for this PR. Can you revert all the changes related to removing the hack, and make this PR just about about module and class change? |
This is the ongoging work of #126101, where we introduced a new attribute
ste_has_docstring
in the symbol table entry. This PR aims to set this attribute for modules and classes whenever there are docstrings for them. Meanwhile, it also preventsNone
being added toco_consts
for lambda, annotation and type alias.