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

line-too-long (E501) - false positive on import statements from long module names where it's impossible to fix #15236

Open
DetachHead opened this issue Jan 3, 2025 · 2 comments
Labels
rule Implementing or modifying a lint rule

Comments

@DetachHead
Copy link

if a line contains an import statement from a very long module path, it triggers E501, even though there's no way to fix it as far as i know:

from asdfasasd.asdfasdf.asdfasdfsa.dfasdfasdfasdf.asdfasdfasdf.asdfasdfasdf.asdfasdfasdf.asdfsasdfasdf import ( # error: E501
    foo,
)

https://play.ruff.rs/93d32aef-cefb-41f2-8205-e05bbea8ba28?secondary=Format

ruff should be able to determine that this situation is unavoidable and therefore should not report the error. it already does this in some cases for example comments/docstrings with long URLs

@MichaReiser
Copy link
Member

You can use line continuation if you really wanted to avoid the E501 violation:

from asdfasasd.asdfasdf.asdfasdfsa.dfasdfasdfasdf\
    .asdfasdfasdf.asdfasdfasdf.asdfasdfasdf\
    .asdfsasdfasdf import foo

But that's not something we want to encourage (especially because the formatter removes the line continuations again).

That's why I think it's correct to say that you can't change anything on the import side. However, you can change the module structure to reduce the nesting, which will make it easier to import the module.

I did some quick search and long parenthesized imports don't seem that common (source) or my search is too strict. Suppressions for single-line imports is more common (source).

There are many more cases where Python doesn't provide a way for splitting an expression unlike other languages.

class AVeryLongClassNameAndTheresNothingYouCanDoAboutIt: # E501
   pass

a = (1000000000_00) # noqa: E501

but the import case is probably the most common.

Ultimately this is somewhat related to #8383 and raises the question if we should make E501 token aware. I think we could do so now, because we have cheap access to the token stream.

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Jan 3, 2025
@InSyncWithFoo
Copy link
Contributor

@MichaReiser Your query is indeed too strict. A slightly looser one returns ~800 results.

from\s+[a-zA-Z    \.]+\s+import\s+\(\s+#\s+noqa:\s+E501\n[a-zA-Z    ,\s]+\)

from\s+[a-zA-Z0-9_\.]+\s+import\s*\(\s*#\s*noqa:\s*E501\n[a-zA-Z0-9_,\s]+\)
#             ^^^^               ^    ^   ^       ^             ^^^^

Same for the other query.

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

No branches or pull requests

3 participants