-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Support expression in f-strings spec #1625
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
Changes from all commits
c3703f0
8e84a85
b394ad4
6698fde
6a4c3da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from testutils import assert_raises | ||
foo = 'bar' | ||
|
||
assert f"{''}" == '' | ||
|
@@ -16,6 +17,13 @@ | |
assert f'{16:0>+#10x}' == '00000+0x10' | ||
assert f"{{{(lambda x: f'hello, {x}')('world}')}" == '{hello, world}' | ||
|
||
spec = "0>+#10x" | ||
assert f"{16:{spec}}{foo}" == '00000+0x10bar' | ||
|
||
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. Could you add some TypeError tests here for formatting with 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. Could you be more specific? 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. e.g. 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.
This is actually a valid format string. |
||
# TODO: | ||
# spec = "bla" | ||
# assert_raises(ValueError, lambda: f"{16:{spec}}") | ||
|
||
# Normally `!` cannot appear outside of delimiters in the expression but | ||
# cpython makes an exception for `!=`, so we should too. | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
Could you add some more tests for some weirder cases? e.g.
f"{val: {'#'} }"
interprets the spec as' {\'#\'} '
; it only interpolates the value if there are no characters between:
and{
and none between the two closing}
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.
I know this is not perfect yet but we can improve on that later. I added a few more fail cases tests.