From 10f095de65229367696ca25165447ea251d98fdc Mon Sep 17 00:00:00 2001 From: Martin Wicke <577277+martinwicke@users.noreply.github.com> Date: Mon, 24 Apr 2023 12:10:06 -0700 Subject: [PATCH 1/5] Add test cases for self-documenting variables This is preparation for supporting https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging. --- testdata/ast/fstring.in | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/testdata/ast/fstring.in b/testdata/ast/fstring.in index 684eb21..19fcb78 100644 --- a/testdata/ast/fstring.in +++ b/testdata/ast/fstring.in @@ -50,3 +50,7 @@ f""" j ) """ + +f'k=' + +f'k= i' From 752fc9ca0f8f94f5424617c6c81542b9bef7b0f0 Mon Sep 17 00:00:00 2001 From: Martin Wicke <577277+martinwicke@users.noreply.github.com> Date: Mon, 24 Apr 2023 12:10:34 -0700 Subject: [PATCH 2/5] Fix typo --- testdata/ast/fstring.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testdata/ast/fstring.in b/testdata/ast/fstring.in index 19fcb78..e4d4c7b 100644 --- a/testdata/ast/fstring.in +++ b/testdata/ast/fstring.in @@ -53,4 +53,4 @@ f""" f'k=' -f'k= i' +f'k= l' From 384c7711ec1615da037e07eb1622b404f68ec3bb Mon Sep 17 00:00:00 2001 From: Martin Wicke <577277+martinwicke@users.noreply.github.com> Date: Mon, 24 Apr 2023 13:00:33 -0700 Subject: [PATCH 3/5] Self-documenting variables in f-strings Eat a = after a variable in an f-string. This is already encoded in the structure of the JoinedStr so we shouldn't have to remember it. --- pasta/base/annotate.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pasta/base/annotate.py b/pasta/base/annotate.py index c3b6429..08d5917 100644 --- a/pasta/base/annotate.py +++ b/pasta/base/annotate.py @@ -1490,11 +1490,15 @@ def check_slice_includes_step(self, node): def visit_FormattedValue(self, node): self.visit(node.value) if node.conversion != -1: - self.attr( - node, - 'conversion', [self.ws, '!', chr(node.conversion)], - deps=('conversion',), - default='!%c' % node.conversion) + # If we have a self-documenting token, eat the '='. + if self.tokens.peek_non_whitespace().src == '=': + self.token('=') + else: + self.attr( + node, + 'conversion', [self.ws, '!', chr(node.conversion)], + deps=('conversion',), + default='!%c' % node.conversion) if node.format_spec: self.attr( node, 'format_spec_prefix', [self.ws, ':', self.ws], default=':') From 721900685dcd2e1dc69e0a7f95e5a441ffe561a2 Mon Sep 17 00:00:00 2001 From: Martin Wicke <577277+martinwicke@users.noreply.github.com> Date: Wed, 17 May 2023 10:54:37 -0700 Subject: [PATCH 4/5] Use optional_token, allow ! with = Also add link to docs. --- pasta/base/annotate.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pasta/base/annotate.py b/pasta/base/annotate.py index 08d5917..718efbf 100644 --- a/pasta/base/annotate.py +++ b/pasta/base/annotate.py @@ -1490,15 +1490,13 @@ def check_slice_includes_step(self, node): def visit_FormattedValue(self, node): self.visit(node.value) if node.conversion != -1: - # If we have a self-documenting token, eat the '='. - if self.tokens.peek_non_whitespace().src == '=': - self.token('=') - else: - self.attr( - node, - 'conversion', [self.ws, '!', chr(node.conversion)], - deps=('conversion',), - default='!%c' % node.conversion) + # If we have a self-documenting token, eat the '=' (see https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging) + self.optional_token(node, 'self_documenting', '=') + self.attr( + node, + 'conversion', [self.ws, '!', chr(node.conversion)], + deps=('conversion',), + default='!%c' % node.conversion) if node.format_spec: self.attr( node, 'format_spec_prefix', [self.ws, ':', self.ws], default=':') From 103cda1fb90d9f16ef66460a8b2dc698302908c2 Mon Sep 17 00:00:00 2001 From: Martin Wicke <577277+martinwicke@users.noreply.github.com> Date: Wed, 17 May 2023 10:56:36 -0700 Subject: [PATCH 5/5] Fix {x=} test cases and add more --- testdata/ast/fstring.in | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/testdata/ast/fstring.in b/testdata/ast/fstring.in index e4d4c7b..5cc6fa5 100644 --- a/testdata/ast/fstring.in +++ b/testdata/ast/fstring.in @@ -51,6 +51,10 @@ f""" ) """ -f'k=' +f'{k=}' -f'k= l' +f'{l=!s}' + +f'{m =:.3f}' + +f'{n=} l'