-
Notifications
You must be signed in to change notification settings - Fork 36
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
Bad %d conversion? #59
Comments
this is unfortunate that old % formatting has a different behavior, https://stackoverflow.com/questions/5627605/string-formatting-with-0d-format-gives-unknown-format-code-d-for-object-o Example:
This leaves the choices:
|
I think the root problem here is that under the hood, f-strings use str.format() under the hood. For example, this works:
and these fails:
So really, any conversion flynt does of %d to :d has the potential to introduce a new exception. I can think of a 4th option: convert and keep %d but also wrap the value with int():
This keeps % formatting semantics and prevents flynt from breaking working code. You might emit a warning that the int() might not be necessary. Or at the least document the considerations discussed here. I think adding int() is preferable to taking the risk of creating exceptions. There will be strings that are only formatted when something goes wrong and I really hate it when an error print() is broken and I miss out on the hint needed to fix the underlying problem. You can argue that testing will reveal problems but the reality is testing is not universal. |
int(n) has yet different behavior from %d formatting (e.g. it could accept a string '123'). I think a thorough solution will involve a new flag, --aggressive, and for False This will need changes in CLI interface and tests as well, so ETA for this change is couple of months, I don't have much time to work on this now. In the meanwhile will add a section about %d to README # Dangers of conversion. |
fixed in 0.47 |
I started with:
flynt converted this to:
which results in the error:
To be fair the calculation of "n" should have used integer division (//) but the conversion shouldn't break the script.
The text was updated successfully, but these errors were encountered: