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

Allow for exceptions for E402 #472

Closed
hynek opened this issue Jan 21, 2016 · 15 comments
Closed

Allow for exceptions for E402 #472

hynek opened this issue Jan 21, 2016 · 15 comments

Comments

@hynek
Copy link

hynek commented Jan 21, 2016

Hi,
in tests I have quite frequently something like:

import pytest

pytest.importorskip("twisted")

from twisted.internet.defer import Deferred, succeed, fail

from internal_package import xyz

which now reports E402s.

Any chance you add an exception for that? The annoying part is that I’d have to add noqas to every single import statement that comes after the importorskip() which would also hamper with unused symbols detection…

By itself I find E402 useful so I don’t want to suppress it either…

@jayvdb
Copy link
Member

jayvdb commented Jan 21, 2016

One improvement would be for pytest.importorskip("twisted") # noqa to mean all the following imports do not need a noqa.

@IanLee1521
Copy link
Member

It seems like a better way might be to have your test code checked with something like pep8 --ignore=E402 tests/

@hynek
Copy link
Author

hynek commented Jan 22, 2016

As I wrote I like E402 so I would prefer to not silence it altogether...

Since it seems to support the try/except idiom, I thought this could be added too.

But I could live with putting noqa on the non-import line too.

@alasdairnicol
Copy link
Contributor

It's a similar issue when writing standalone scripts that use Django

import django
from django.conf import settings
from myapp import myapp_defaults

settings.configure(default_settings=myapp_defaults, DEBUG=True)
django.setup()

# Now this script or any imported module can use any part of Django it needs.
from my_app import models
from my_other_app import models

I'd find it useful to be able to add #noqa to the settings.configure and django.setup() lines, as @jayvdb suggested.

@IanLee1521
Copy link
Member

Closing, see call for pull requests in #480.

@ashb
Copy link

ashb commented Feb 19, 2021

If anyone comes across this issue and would like a fix that doesn't involve noqa on each line, nor to globally disable it, I wrote https://pypi.org/project/flake8-pytest-importorskip/ to handle it "automatically".

@asottile
Copy link
Member

If anyone comes across this issue and would like a fix that doesn't involve noqa on each line, nor to globally disable it, I wrote https://pypi.org/project/flake8-pytest-importorskip/ to handle it "automatically".

I would not recommend that plugin -- it uses private implementation detail that will break in a future version of flake8: https://github.com/ashb/flake8-pytest-importorskip/blob/6d2e6fb03ce5f938ae555b6f5e930637af3454fe/flake8_pytest_importorskip/__init__.py#L9

@ashb
Copy link

ashb commented Feb 19, 2021

Yes -- it's a hack. It works for now though.

It does this in a slightly hacky way, so it may break in future versions of flake8 or pycodestyle.

@asottile
Copy link
Member

Yes -- it's a hack. It works for now though.

It does this in a slightly hacky way, so it may break in future versions of flake8 or pycodestyle.

ah, well as the flake8 maintainer -- I plan to change that bit of code so it likely will not work in the future :)

@ashb
Copy link

ashb commented Feb 20, 2021

@asottile If you are changing that, would it be possible to have a plugin change the logical_line that "later plugins" see?

@asottile
Copy link
Member

I really don't think that's a good idea -- lying about the source code to other plugins. plus there's not really a concept of plugin ordering (that your plugin works at all right now is dumb ordering luck)

@ashb
Copy link

ashb commented Feb 20, 2021

I thought ordering was alphabetic - guess just got lucky.

@ashb
Copy link

ashb commented Feb 20, 2021

As for Lying to other plugins: http://pylint.pycqa.org/en/latest/how_tos/transform_plugins.html 😀

@asottile
Copy link
Member

pylint's system is more for augmenting existing information (giving hints to pylint's engine about information it can't glean statically) -- I don't see how you'd implement your bait-and-switch with a non-import line to an import given that without severely breaking other things

@ashb
Copy link

ashb commented Feb 20, 2021

Yeah, I wasn't being serious anyway. Another perhaps less hacky approach would be to let warnings get filtered by other plugins before being issued.

I also have another idea that might work that doesn't need me to look at private state -- I might be able to monkey patch the other plugin function, to wrap it.

https://pypi.org/project/flake8-pytest-importorskip/ v1.1.0 has just been released, and this plugin no longer accesses any private state of flake8.

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

6 participants