Skip to content
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

tidy-imports --transform incorrectly makes semantic changes (Prop#299879) #175

Open
sac111gp opened this issue Feb 3, 2022 · 2 comments
Assignees
Milestone

Comments

@sac111gp
Copy link
Collaborator

sac111gp commented Feb 3, 2022

$ cat tmp.py
x = "foo.bar"
x.foo.bar
$ tidy-imports --transform foo.bar=foo.baz tmp.py
...
-x = "foo.bar"
-x.foo.bar
+x = "foo.baz"
+x.foo.baz

If we have a non-docstring string, we should not change its value. Similarly, we shouldn't change x.foo.bar to x.foo.baz; there's no guarantee they mean the same thing.

@ericdatakelly ericdatakelly added this to the March 2022 milestone Feb 14, 2022
@ericdatakelly ericdatakelly modified the milestones: March 2022, April 2022 Feb 28, 2022
@magsol magsol modified the milestones: March 2022, April 2022 Mar 23, 2022
@Carreau
Copy link
Collaborator

Carreau commented May 11, 2022

Changing inside strings and comments seem to be by design:

    For the rest of the code body, transform_imports() does a crude textual
    string replacement.  This is imperfect but handles most cases.  There may
    be some false positives, but this is difficult to avoid.  Generally we do
    want to do replacements even within in strings and comments.

My guess is this is ment for getattr and alike.

With the current architecture of transforms, that just do string replace on blocks, the easy option is to add a flag to only touch top level imports, and not the rest of the code.

Having something with more fine grained options (touching only docstrings), will need a more complex rewrite of the transformations pipeline to work on ast nodes. It will probably take ~50h seeing the complexity.

Let me know if you want me to just add a flag to touch top level module imports or try to investigate more how to do proper transforms.

@sac111gp
Copy link
Collaborator Author

@quarl would be a better person to comment on #175 (comment)

@Carreau Carreau self-assigned this Nov 9, 2022
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

No branches or pull requests

4 participants