-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
UP031: ruff 0.4.2 generated invalid fix it code #11166
Comments
Guessing this is due to the recent change to UP031? Cc. @plredmond |
Why can't tensors be passed-in there, but can be formatted with |
@charliermarsh I think it's related to classes overriding |
@charliermarsh Ah, apparently in the first code, it's implicitly converted before: "Here, you're using the old-style string formatting (%-formatting), where shift, a PyTorch tensor, is implicitly converted to a numeric type (float or integer) when it's formatted. This conversion happens before the formatting is applied, so the process doesn't raise any issues." import torch
x = torch.tensor([1.0])
print("Value: %.5g" % x) # Works because x is cast as a float before __format__ is called
print(f"Value: {x:.5g}") # fails because Tensor.__format__ is called |
I think explicitly casting to float in all float specific f-string fixits could solve this issue? |
I think that makes sense. E.g., when there's a |
There are probably other specifies that do this casting f (or maybe some to int for d) etc. |
@charliermarsh perhaps we could add this kind of explicit-conversion to the UP031 rule after we can infer the types of the expressions? alternatively, since we parse the format string we could use that information to select an explicit-conversion function? |
became
which throws an error. I know it's an unsafe fix, but unsafe fixes should not generate invalid code. Specifically:
from this PR: pytorch/pytorch#125031
The text was updated successfully, but these errors were encountered: