Skip to content

Commit

Permalink
Implemented new check consider-using-from-import (pylint-dev#4491)
Browse files Browse the repository at this point in the history
  • Loading branch information
yushao2 committed May 23, 2021
1 parent b199fa6 commit 5ebbc30
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ modules are added.

Closes #4470

* New checker``consider-using-from-import``. Emitted when a submodule/member of a package is imported and aliased
with the same name.

Closes #2309


What's New in Pylint 2.8.2?
===========================
Expand Down
2 changes: 2 additions & 0 deletions doc/whatsnew/2.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ New checkers
* ``unnecessary-dict-index-lookup``: Emitted when iterating over dictionary items
(key-value pairs) and accessing the value by index lookup.

* ``consider-using-from-import``: Emitted when a submodule/member of a package is imported and aliased with the same name.

Other Changes
=============

Expand Down
36 changes: 25 additions & 11 deletions pylint/checkers/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import os
import sys
from distutils import sysconfig
from typing import Dict, List
from typing import Dict, List, Union

import astroid

Expand Down Expand Up @@ -236,6 +236,14 @@ def _make_graph(filename: str, dep_info: Dict[str, List[str]], sect: VNode, gtyp
"cyclic-import",
"Used when a cyclic import between two or more modules is detected.",
),
"R0402": (
"Use 'from %s import %s' instead",
"consider-using-from-import",
"Emitted when a submodule/member of a package is imported and "
"aliased with the same name. "
"E.g., instead of ``import pandas.DataFrame as DataFrame`` use "
"``from pandas import DataFrame``",
),
"W0401": (
"Wildcard import %s",
"wildcard-import",
Expand Down Expand Up @@ -865,22 +873,28 @@ def _check_preferred_module(self, node, mod_path):
args=(self.preferred_modules[mod_path], mod_path),
)

def _check_import_as_rename(self, node):
def _check_import_as_rename(
self, node: Union[astroid.Import, astroid.ImportFrom]
) -> None:
names = node.names
for name in names:
if not all(name):
return

real_name = name[0]
splitted_packages = real_name.rsplit(".")
real_name = splitted_packages[-1]
imported_name = name[1]
# consider only following cases
# import x as x
# and ignore following
# import x.y.z as z
if real_name == imported_name and len(splitted_packages) == 1:
splitted_packages = name[0].rsplit(".", maxsplit=1)
import_name = splitted_packages[-1]
aliased_name = name[1]
if import_name != aliased_name:
continue

if len(splitted_packages) == 1:
self.add_message("useless-import-alias", node=node)
elif len(splitted_packages) == 2:
self.add_message(
"consider-using-from-import",
node=node,
args=(splitted_packages[0], import_name),
)

def _check_reimport(self, node, basename=None, level=None):
"""check if the import is necessary (i.e. not already done)"""
Expand Down
2 changes: 1 addition & 1 deletion tests/extensions/test_empty_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

import pylint.extensions.empty_comment as empty_comment
from pylint.extensions import empty_comment


@pytest.fixture(scope="module")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# pylint: disable=unused-import, missing-docstring, invalid-name, reimported, import-error, wrong-import-order, no-name-in-module, relative-beyond-top-level
# Functional tests for import aliasing
# 1. useless-import-alias
# 2. consider-using-from-import

from collections import OrderedDict as OrderedDict # [useless-import-alias]
from collections import OrderedDict as o_dict
import os.path as path
import os.path as path # [consider-using-from-import]
import os.path as p
import foo.bar.foobar as foobar
import foo.bar.foobar as foobar # [consider-using-from-import]
import os
import os as OS
from sys import version
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/i/import_aliasing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
useless-import-alias:6:0::Import alias does not rename original package
consider-using-from-import:8:0::Use 'from os import path' instead
consider-using-from-import:10:0::Use 'from foo.bar import foobar' instead
useless-import-alias:14:0::Import alias does not rename original package
useless-import-alias:17:0::Import alias does not rename original package
useless-import-alias:18:0::Import alias does not rename original package
useless-import-alias:20:0::Import alias does not rename original package
useless-import-alias:21:0::Import alias does not rename original package
useless-import-alias:23:0::Import alias does not rename original package
7 changes: 0 additions & 7 deletions tests/functional/u/useless/useless-import-alias.txt

This file was deleted.

0 comments on commit 5ebbc30

Please sign in to comment.