-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[stdlib] Allow !r
in String.format
#3267
Comments
Hi :) Had a quick look into this. Unsure of how we can allow both A potential solution could be to introduce a new trait, It would be neat to insert, where appropriate, the following into if i == e[].field[Int]:
if e[].conversion_flag == "r":
res = repr(args[i])
else:
res = str(args[i]) It follows we would need to introduce this new field, In comparison to the
On another note, if we look at the Format String Syntax from the Python docs,
, it's worth mentioning that the current |
…3914) [External] [stdlib] Allow `!r` conversion flag in `String.format` This addresses #3267. - Implemented parsing and handling of `!s` and `!r` conversion flags in format strings. - Updated the `_FormatCurlyEntry` struct to include a `conversion_flag` field. - Modified the `create_entries` method to correctly parse and store conversion flags. - Updated the string formatting logic to apply the appropriate conversion (`str` or `repr`) based on the flag. Additionally, I was conscious of potential future support of `!a` flag and `format_spec` , which is not currently supported. The additions of this PR should be direct to modify for this support. Hence why `supported_conversion_flags` was added, and a TODO was left in the code for the latter. For reference, see [Python's Format String Syntax](https://docs.python.org/3/library/string.html#format-string-syntax): ``` replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}" field_name ::= arg_name ("." attribute_name | "[" element_index "]")* arg_name ::= [identifier | digit ] attribute_name ::= identifier element_index ::= digit | index_string index_string ::= <any source character except "]"> conversion ::= "r" | "s" | "a" format_spec ::= <see Python docs for details> ``` ORIGINAL_AUTHOR=Joshua James Venter <67124214 [email protected]> PUBLIC_PR_LINK=#3279 Co-authored-by: Joshua James Venter <67124214 [email protected]> Closes #3279 MODULAR_ORIG_COMMIT_REV_ID: 9ee92a55ac683be22f7b93012f37c6980f7110b6
Fixed in #3279, thank you for the contribution! |
Allow "{!r}".format(x), which is equivalent to
repr(x)
rather thanstr(x)
. See https://dev.to/behainguyen/python-the-r-string-format-and-the-repr-and-str-methods-29i7 for more info.The text was updated successfully, but these errors were encountered: