Support expression in f-strings spec#1625
Conversation
| conversion: None, | ||
| spec: Some(Box::new(Constant { | ||
| value: "spec".to_string(), | ||
| })), |
There was a problem hiding this comment.
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.
I know this is not perfect yet but we can improve on that later. I added a few more fail cases tests.
|
|
||
| spec = "0>+#10x" | ||
| assert f"{16:{spec}}{foo}" == '00000+0x10bar' | ||
|
|
There was a problem hiding this comment.
Could you add some TypeError tests here for formatting with ints as specs or similar?
There was a problem hiding this comment.
Could you be more specific?
There was a problem hiding this comment.
e.g. assert_raises(TypeError, lambda: f"{123:{45}}")
There was a problem hiding this comment.
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: