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

ungrouped-imports / wrong-import-order : FP because only two isort's options are taken into account #9977

Open
webknjaz opened this issue Sep 27, 2024 · 0 comments
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@webknjaz
Copy link
Contributor

webknjaz commented Sep 27, 2024

Current problem

There's this PEP 421 implicit namespace we're using. I'll call it an ecosystem_namespace in this example. It has a special project providing a ecosystem_namespace.interfaces which is kinda a platform spec or a framework for all the other projects in this namespace. So it must always be imported before any other packages in the namespace, including the local ones.

When I configure isort as follows

# .isort.cfg
[settings]
combine_as_imports = true
default_section = THIRDPARTY
honor_noqa = true
include_trailing_comma = true
indent = 4
known_first_party = ecosystem_namespace.projectlocal_subnamespace, ecosystem_namespace.another_projectlocal_subnamespace
known_frameworks = cherrypy, django
known_platforms = ecosystem_namespace.interfaces
known_testing = hypothesis, pytest
line_length = 79
lines_after_imports = 2
multi_line_output = 3
no_lines_before = LOCALFOLDER
sections = FUTURE, STDLIB, TESTING, FRAMEWORKS, PLATFORMS, THIRDPARTY, FIRSTPARTY, LOCALFOLDER
src_paths = src/*/*
use_parentheses = true
verbose = true

it helps me get the following order in the import section structure:

# src/ecosystem_namespace/projectlocal_subnamespace/api.py

from __futures__ import annotations

import os

import hypothesis
import pytest

import cheroot

import ecosystem_namespace.interfaces

import octomachinery

from ecosystem_namespace.projectlocal_subnamespace import smth

isort is able to understand that ecosystem_namespace.interfaces is a separate thing that must reside in its own category in a defined place.

However, pylint yields ungrouped-imports and wrong-import-order, despite relying on isort internally.

Additionally, the error messages in the pylint output imply that it treats the namespace (ecosystem_namespace) as a first-party package, which is nowhere near being true.

src/ecosystem_namespace/projectlocal_subnamespace/plugins.py:13:0: C0411: third party import "octomachinery" should be placed before first party import "ecosystem_namespace.interfaces._temporary_private_api.detect_runtime"  (wrong-import-order)
src/ecosystem_namespace/projectlocal_subnamespace/plugins.py:15:0: C0412: Imports from package ecosystem_namespace are not grouped (ungrouped-imports)

Desired solution

I believe, the ungrouped-imports and wrong-import-order checks should always pass when the import order matches the existing isort configuration. And it should be able to understand that namespace packages aren't first-party.

Additional context

Looking into

extra_standard_library=config.known_standard_library,
known_third_party=config.known_third_party,
, it looks like pylint only special-cases first-party and third-party categories but is ignorant about any other sections defined additionally. Perhaps, this needs to change.

@webknjaz webknjaz added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Sep 27, 2024
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Sep 27, 2024
@Pierre-Sassoulas Pierre-Sassoulas changed the title [FR and partially a bug?] isort's custom grouping feature should be taken into account in the ungrouped-imports and wrong-import-order checks ungrouped-imports / wrong-import-order : FP because only two isort's options are taken into account Sep 27, 2024
webknjaz added a commit to webknjaz/ansible--awx-plugins that referenced this issue Sep 29, 2024
They are handled by `isort` already and it is way more flexible than
PyLint, which reports false-positives.

Refs:
* pylint-dev/pylint#9976
* pylint-dev/pylint#9977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

2 participants