-
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
Conversation
conversion: None, | ||
spec: Some(Box::new(Constant { | ||
value: "spec".to_string(), | ||
})), |
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.
@@ -16,6 +16,9 @@ | |||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some TypeError tests here for formatting with int
s as specs or similar?
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 be more specific?
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.
e.g. assert_raises(TypeError, lambda: f"{123:{45}}")
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.
e.g.
assert_raises(TypeError, lambda: f"{123:{45}}")
This is actually a valid format string.
I added a TODO in the tests for an invalid one. This currently does not fail but it is not related to this change.
0fbaaa4
to
6a4c3da
Compare
Hey, I have a PR for supporting assert f'{5.1234: f}' == ' 5.123400' When using the Welcome to the magnificent Rust Python 0.1.1 interpreter 😱 🖖
>>>>> f'{5.1234: f}'
'5.123400'
>>>>> '{: f}'.format(5.1234)
' 5.123400'
>>>>> '{x: f}'.format_map({'x': 5.1234})
' 5.123400'
>>>>> |
It looks like that came from line 86 in spec = Some(Box::new(Constant {
value: spec_expression.trim().to_string(),
})) If you remove the |
Confirmed, removing |
Also adds automatic conversion of int -> float when the ":f" format code is specified. Fixes a f-string formatting style regression introduced in RustPython#1625.
Also adds automatic conversion of int -> float when the ":f" format code is specified. Fixes a f-string formatting style regression introduced in RustPython#1625.
Also adds automatic conversion of int -> float when the ":f" format code is specified. Fixes a f-string formatting style regression introduced in RustPython#1625.
This allows: