8000 Support expression in f-strings spec by palaviv · Pull Request #1625 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 5 commits into from
Dec 28, 2019

Conversation

palaviv
Copy link
Contributor
@palaviv palaviv commented Dec 13, 2019

This allows:

spec = "0>+#10x"
f"{16:{spec}}"

conversion: None,
spec: Some(Box::new(Constant {
value: "spec".to_string(),
})),
Copy link
Member
@coolreader18 coolreader18 Dec 13, 2019

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 }

Copy link
Contributor Author

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'

Copy link
Member

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 ints as specs or similar?

Copy link
Contributor Author

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?

Copy link
Member

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}}")

Copy link
Contributor Author

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.

@palaviv palaviv mentioned this pull request Dec 28, 2019
@coolreader18 coolreader18 merged commit ddb61e2 into RustPython:master Dec 28, 2019
@doyshinda
Copy link
Contributor

Hey, I have a PR for supporting :f string format codes in #1651 and I noticed that after rebasing with these changes, one of my new tests is now failing with f-string style formatting:

assert f'{5.1234: f}' == ' 5.123400'

When using the f style formatting, the " " prefix is missing:

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'
>>>>>

@coolreader18
Copy link
Member
coolreader18 commented Dec 29, 2019

It looks like that came from line 86 in parser/src/fstring.rs:

spec = Some(Box::new(Constant {
    value: spec_expression.trim().to_string(),
}))

If you remove the .trim() in your branch, that should fix it.

@doyshinda
Copy link
Contributor

Confirmed, removing trim() fixed it, thanks!

doyshinda added a commit to doyshinda/RustPython that referenced this pull request Dec 29, 2019
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.
doyshinda added a commit to doyshinda/RustPython that referenced this pull request Dec 30, 2019
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.
doyshinda added a commit to doyshinda/RustPython that referenced this pull request Jan 1, 2020
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0