Repository navigation
String annotations [PEP 563] #4390
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a87ea0
Document `from __future__ import annotations` (PEP 563)
ambv 863d3e9
Plumbing for `from __future__ import annotations` (PEP 563)
ambv bc3ad37
Implement unparsing the AST back to string form
ambv 7f88115
Response to Serhiy's review
08f8600
Strings are no longer treated special
de9e5b0
f-string support
ambv 26c028c
compatibility typo
ambv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Plumbing for
from __future__ import annotations (PEP 563)
The string form is recovered by unparsing the AST.
- Loading branch information
commit 863d3e986fd0beec767383c302b06dd59436938f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1658,13 +1658,27 @@ compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, | |
| return 0; | ||
| } | ||
|
|
||
| static int | ||
| compiler_visit_annexpr(struct compiler *c, expr_ty annotation) | ||
| { | ||
| static PyObject *ann_as_str; | ||
|
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. Why
Contributor
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. A left-over from something else I tried before. Removed. |
||
| ann_as_str = PyAST_UnicodeFromAstExpr(annotation, 1, 1); | ||
| if (!ann_as_str) | ||
| return 0; | ||
| ADDOP_N(c, LOAD_CONST, ann_as_str, consts); | ||
| return 1; | ||
| } | ||
|
|
||
| static int | ||
| compiler_visit_argannotation(struct compiler *c, identifier id, | ||
| expr_ty annotation, PyObject *names) | ||
| { | ||
| if (annotation) { | ||
| PyObject *mangled; | ||
| VISIT(c, expr, annotation); | ||
| if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) | ||
| VISIT(c, annexpr, annotation) | ||
| else | ||
| VISIT(c, expr, annotation); | ||
| mangled = _Py_Mangle(c->u->u_private, id); | ||
| if (!mangled) | ||
| return 0; | ||
|
|
@@ -4646,7 +4660,10 @@ compiler_annassign(struct compiler *c, stmt_ty s) | |
| if (!mangled) { | ||
| return 0; | ||
| } | ||
| VISIT(c, expr, s->v.AnnAssign.annotation); | ||
| if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) | ||
| VISIT(c, annexpr, s->v.AnnAssign.annotation) | ||
| else | ||
| VISIT(c, expr, s->v.AnnAssign.annotation); | ||
| /* ADDOP_N decrefs its argument */ | ||
| ADDOP_N(c, STORE_ANNOTATION, mangled, names); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For uniformity with other names it would be better to name this function like
PyAST_AsUnicode.And I think it would be better to make it private and don't include in a limited API for now.
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.
Actually, since this functions supports only AST corresponding to expressions,
PyAST_AsUnicodeis not a good name. Maybe just add an underscore toPyAST_UnicodeFromAstExpr? Or_PyAST_ExprAsUnicode?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.
OK, I like _PyAST_ExprAsUnicode.