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

Add ignored modules to Astroid module deny list #9504

Merged
merged 4 commits into from May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .pyenchant_pylint_custom_dict.txt
Expand Up @@ -300,6 +300,7 @@ spammy
sqlalchemy
src
starargs
stateful
staticmethod
stderr
stdin
Expand Down
2 changes: 1 addition & 1 deletion doc/user_guide/configuration/all-options.rst
Expand Up @@ -120,7 +120,7 @@ Standard Checkers

--ignored-modules
"""""""""""""""""
*List of module names for which member attributes should not be checked (useful for modules/projects where namespaces are manipulated during runtime and thus existing member attributes cannot be deduced by static analysis). It supports qualified module names, as well as Unix pattern matching.*
*List of module names for which member attributes should not be checked and will not be imported (useful for modules/projects where namespaces are manipulated during runtime and thus existing member attributes cannot be deduced by static analysis). It supports qualified module names, as well as Unix pattern matching.*

**Default:** ``()``

Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/9442.other
@@ -0,0 +1,5 @@
Ignored modules are now not checked at all, instead of being checked and then
ignored. This should speed up the analysis of large codebases which have
ignored modules.

Closes #9442 (`#9442 <https://github.com/pylint-dev/pylint/issues/9442>`_)
9 changes: 5 additions & 4 deletions examples/pylintrc
Expand Up @@ -59,10 +59,11 @@ ignore-paths=
# Emacs file locks
ignore-patterns=^\.#

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis). It
# supports qualified module names, as well as Unix pattern matching.
# List of module names for which member attributes should not be checked and
# will not be imported (useful for modules/projects where namespaces are
# manipulated during runtime and thus existing member attributes cannot be
# deduced by static analysis). It supports qualified module names, as well
# as Unix pattern matching.
ignored-modules=

# Python code to execute, usually for sys.path manipulation such as
Expand Down
9 changes: 5 additions & 4 deletions examples/pyproject.toml
Expand Up @@ -49,10 +49,11 @@ ignore = ["CVS"]
# file locks
ignore-patterns = ["^\\.#"]

# List of module names for which member attributes should not be checked (useful
# for modules/projects where namespaces are manipulated during runtime and thus
# existing member attributes cannot be deduced by static analysis). It supports
# qualified module names, as well as Unix pattern matching.
# List of module names for which member attributes should not be checked and
# will not be imported (useful for modules/projects where namespaces are
# manipulated during runtime and thus existing member attributes cannot be
# deduced by static analysis). It supports qualified module names, as well
# as Unix pattern matching.
# ignored-modules =

# Python code to execute, usually for sys.path manipulation such as
Expand Down
3 changes: 2 additions & 1 deletion pylint/lint/base_options.py
Expand Up @@ -384,7 +384,8 @@ def _make_linter_options(linter: PyLinter) -> Options:
"type": "csv",
"metavar": "<module names>",
"help": "List of module names for which member attributes "
"should not be checked (useful for modules/projects "
"should not be checked and will not be imported "
"(useful for modules/projects "
"where namespaces are manipulated during runtime and "
"thus existing member attributes cannot be "
"deduced by static analysis). It supports qualified "
Expand Down
1 change: 1 addition & 0 deletions pylint/lint/pylinter.py
Expand Up @@ -1073,6 +1073,7 @@ def open(self) -> None:
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
MANAGER.module_denylist.update(self.config.ignored_modules)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
Expand Down
9 changes: 5 additions & 4 deletions pylintrc
Expand Up @@ -342,10 +342,11 @@ property-classes=abc.abstractproperty
# members is set to 'yes'
mixin-class-rgx=.*MixIn

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis). It
# supports qualified module names, as well as Unix pattern matching.
# List of module names for which member attributes should not be checked and
# will not be imported (useful for modules/projects where namespaces are
# manipulated during runtime and thus existing member attributes cannot be
# deduced by static analysis). It supports qualified module names, as well
# as Unix pattern matching.
ignored-modules=

# List of class names for which member attributes should not be checked (useful
Expand Down
13 changes: 12 additions & 1 deletion tests/lint/test_pylinter.py
Expand Up @@ -10,7 +10,7 @@

from pytest import CaptureFixture

from pylint.lint.pylinter import PyLinter
from pylint.lint.pylinter import MANAGER, PyLinter
from pylint.utils import FileState


Expand Down Expand Up @@ -48,3 +48,14 @@ def test_crash_during_linting(
assert len(files) == 1
assert "pylint-crash-20" in str(files[0])
assert any(m.symbol == "astroid-error" for m in linter.reporter.messages)


def test_open_pylinter_denied_modules(linter: PyLinter) -> None:
"""Test PyLinter open() adds ignored modules to Astroid manager deny list."""
MANAGER.module_denylist = {"mod1"}
try:
linter.config.ignored_modules = ["mod2", "mod3"]
linter.open()
assert MANAGER.module_denylist == {"mod1", "mod2", "mod3"}
finally:
MANAGER.module_denylist = set()
14 changes: 14 additions & 0 deletions tests/test_functional.py
Expand Up @@ -7,13 +7,16 @@
from __future__ import annotations

import sys
from collections.abc import Iterator
from pathlib import Path
from typing import TYPE_CHECKING

import pytest
from _pytest.config import Config

from pylint import testutils
from pylint.constants import PY312_PLUS
from pylint.lint.pylinter import MANAGER
from pylint.testutils import UPDATE_FILE, UPDATE_OPTION
from pylint.testutils.functional import (
FunctionalTestFile,
Expand All @@ -22,6 +25,9 @@
)
from pylint.utils import HAS_ISORT_5

if TYPE_CHECKING:
from pylint.lint import PyLinter

FUNCTIONAL_DIR = Path(__file__).parent.resolve() / "functional"


Expand All @@ -40,6 +46,14 @@
]


@pytest.fixture
def revert_stateful_config_changes(linter: PyLinter) -> Iterator[PyLinter]:
yield linter
# Revert any stateful configuration changes.
MANAGER.brain["module_denylist"] = set()


@pytest.mark.usefixtures("revert_stateful_config_changes")
@pytest.mark.parametrize("test_file", TESTS, ids=TESTS_NAMES)
def test_functional(test_file: FunctionalTestFile, pytestconfig: Config) -> None:
__tracebackhide__ = True # pylint: disable=unused-variable
Expand Down