Skip to content

Commit

Permalink
updated functional test for wrong-import-order to address test covera…
Browse files Browse the repository at this point in the history
…ge failure; added comments in imports.py to outline when each branch of_get_full_import_name applies
  • Loading branch information
azinneck0485 committed Nov 16, 2023
1 parent 4685b3c commit fdba403
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pylint/checkers/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,15 +921,24 @@ def _get_full_import_name(self, importNode: ImportNode) -> str:
# for: import X, this returns X
# for: import X.Y this returns X.Y
# for: from X import Y, this returns X.Y

try:
# this will only succeed for ImportFrom nodes, which in themselves
# contain the information needed to reconstruct the package
return f"{importNode[0].modname}.{importNode[0].names[0][0]}"
except AttributeError:
# in all other cases, the import will either be X or X.Y
node: str = importNode[0].names[0][0]
package: str = importNode[1]

if node.split(".")[0] == package:
# this is sufficient with one import per line, since package = X
# and node = X.Y or X
return node

# when there is a node that contains multiple imports, the "current"
# import being analyzed is specified by package (node is the first
# import on the line and therefore != package in this case)
return package

def _get_imported_module(
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/w/wrong_import_order.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Checks import order rule"""
# pylint: disable=unused-import,ungrouped-imports,import-error,no-name-in-module,relative-beyond-top-level
# pylint: disable=unused-import,ungrouped-imports,import-error,no-name-in-module,relative-beyond-top-level,multiple-imports
from __future__ import absolute_import
try:
from six.moves import configparser
Expand All @@ -22,6 +22,8 @@
from .package2 import Class2
from ..package3 import Class3
from six.moves.urllib.parse import quote # [wrong-import-order]
import pylint.constants # [wrong-import-order]
import re, requests # [wrong-import-order, wrong-import-order]


LOGGER = logging.getLogger(__name__)
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/w/wrong_import_order.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ wrong-import-order:15:0:15:15::"standard import ""datetime"" should be placed be
wrong-import-order:18:0:18:22::"third party import ""totally_missing"" should be placed before local import ""package.Class""":UNDEFINED
wrong-import-order:20:0:20:14::"third party import ""astroid"" should be placed before local imports ""package.Class"", "".package""":UNDEFINED
wrong-import-order:24:0:24:40::"third party import ""six.moves.urllib.parse.quote"" should be placed before local imports ""package.Class"", "".package"", "".package2"", ""package2.Class2"", ""package3.Class3""":UNDEFINED
wrong-import-order:25:0:25:23::"first party import ""pylint.constants"" should be placed before local imports ""package.Class"", "".package"", "".package2"", ""package2.Class2"", ""package3.Class3""":UNDEFINED
wrong-import-order:26:0:26:19::"standard import ""re"" should be placed before third party imports ""six"", ""astroid.are_exclusive"", ""unused_import"", ""totally_missing"", ""astroid"", ""six.moves.urllib.parse.quote"", first party import ""pylint.constants"", and local imports ""package.Class"", "".package"", "".package2"", ""package2.Class2"", ""package3.Class3""":UNDEFINED
wrong-import-order:26:0:26:19::"third party import ""requests"" should be placed before first party import ""pylint.constants"" and local imports ""package.Class"", "".package"", "".package2"", ""package2.Class2"", ""package3.Class3""":UNDEFINED

0 comments on commit fdba403

Please sign in to comment.