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
Alternative string-based approach to format string passthrough #889
Conversation
Uses a regex-based solution instead of str.format
I liked this approach better originally, too, and still do. Less interference and less gotchas. |
I need to look at this CI error on windows in detail, but it occurs to me that backslash (e.g. |
I'd be okay with |
Managed to fix the backslash issue. Having looked at it, |
If you fixed the backslash, I would think that's fine. |
Cool. I'm only really doing the escaping thing for completeness, I'd be surprised if it's ever used! I added a note about its use to the docs, too, all the way at the bottom of the Options page. So this is ready for another review. |
# in repl as escape sequences | ||
result = re.sub( | ||
pattern=find_pattern, | ||
repl=lambda _: str(value), |
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.
repl=lambda _: str(value), | |
repl=value.replace('\\', r'\\'), |
FYI, this is the suggestion in the Python docs for escaping here. Don't have a strong preference, just wanted to point it out. See the final suggestion in https://docs.python.org/3/library/re.html#re.escape.
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.
Thanks, I missed that. Will stick to the lambda for convenience
An alternative approach to, and closes #884 that uses regex instead of format_map.
While working on #884 I felt that there were still quite a lot of gotchas in that implementation i.e.:
These would require the user to provide additional escaping a-la
{{
and}}
.This is an experiment to see if a non-format_map solution might work better.
I'm not sure which one I prefer! The main differences are that, in this PR,
#{wheel}
to passthrough a literal{wheel}
, rather than{{wheel}}
{{unknown}}
is passed through as{unknown}
, but here the escape sequences are only removed on known params. e.g.\{unknown}
passes through as\{unknown}
, untouched. I think this is less disruptive, as most users won't have to think about the escaping unless they hit a naming conflict directly.{{
escape sequences in the wild, due to the change in escaping rules, this would break that. However, I can only find one such instanceSo I suppose I'm leaning towards this approach 1. What do you think @henryiii?
Footnotes
Despite what I said in Do not use Python's str.format() for expanding placeholders in commands #840, sorry! Sometimes it takes writing the code to know. ↩