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

How to make peace with needed nested import statements for Ruff E402? #9091

Closed
alanwilter opened this issue Dec 11, 2023 · 5 comments · Fixed by #9094
Closed

How to make peace with needed nested import statements for Ruff E402? #9091

alanwilter opened this issue Dec 11, 2023 · 5 comments · Fixed by #9094
Assignees
Labels
rule Implementing or modifying a lint rule

Comments

@alanwilter
Copy link

I use this lines a lot in several projects.

import matplotlib
import numpy as np

matplotlib.use("Agg")

from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

There's no other way that I know to set the matplotlib backend. But by doing so this will trigger E402.

isort has some ways of handling that (see here).

Of course I can fill lots of # noqa: E402 but this is so ugly.

Ideally, Ruff/isort should be smart enough to know these cases.

Also, perhaps, page E402 (and all others when reasonable) should show how to silence this warning if the user knows what's doing.

@konstin
Copy link
Member

konstin commented Dec 11, 2023

Could you share more details why matplotlib.use fails if it's after the other imports? https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.use reads like it should be possible.

isort has some ways of handling that (see here).

Of course I can fill lots of # noqa: E402 but this is so ugly.

isort action comments look similar to the noqa comments to me, could you give an example of how they are different?

@alanwilter
Copy link
Author

alanwilter commented Dec 11, 2023

If I load from matplotlib import pyplot as plt before matplotlib.use("Agg") it will be in interactive mode no matter what I do after. (ok, I can use matplotlib.pyplot.ioff()), but it will be slower to start and, if running remotely, it will start X11 service on my mac.

Another solution would be:

import matplotlib
import numpy as np

from matplotlib.backends.backend_pdf import PdfPages
from matplotlib import pyplot as plt # need to be after the line above, but Ruff isort doesn't let

@charliermarsh
Copy link
Member

I've run into this matplotlib pattern before. I think it's actually reasonable for us to allow matplotlib.use calls to intersperse imports... In practice, I suspect it will strictly reduce false positives, with no false negatives.

@charliermarsh
Copy link
Member

I'll PR it and see what the ecosystem checks say.

@charliermarsh charliermarsh self-assigned this Dec 11, 2023
@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Dec 11, 2023
charliermarsh added a commit that referenced this issue Dec 11, 2023
This PR allows `matplotlib.use` calls to intersperse imports without
triggering `E402`. This is a pragmatic choice as it's common to require
`matplotlib.use` calls prior to importing from within `matplotlib`
itself.

Closes #9091.
@alanwilter
Copy link
Author

You guys are amazing. Father Xmas won't forget you ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants