Skip to content

Commit

Permalink
Preliminary changes 2
Browse files Browse the repository at this point in the history
- Responses to review comments
- Add test for calling _astroid_module_checker on different levels
- Move Path.resolve() out of _get_namespace_for_file recursive calls
  • Loading branch information
Aleksey Petryankin committed Apr 20, 2024
1 parent 04d878c commit 9b3576f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pylint/config/config_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def _config_initialization(
linter._parse_error_mode()

# Link the base Namespace object on the current directory
if Path(".").resolve() not in linter._directory_namespaces:
if len(linter._directory_namespaces) == 0:
linter._directory_namespaces[Path(".").resolve()] = (linter.config, {})

# parsed_args_list should now only be a list of inputs to lint.
Expand Down
7 changes: 1 addition & 6 deletions pylint/config/find_default_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _cfg_has_config(path: Path | str) -> bool:
return any(section.startswith("pylint.") for section in parser.sections())


def _yield_default_files(basedir: Path | str = ".") -> Iterator[Path]:
def _yield_default_files(basedir: Path = Path(".")) -> Iterator[Path]:
"""Iterate over the default config file names and see if they exist."""
basedir = Path(basedir)
for config_name in CONFIG_NAMES:
Expand Down Expand Up @@ -144,8 +144,3 @@ def find_default_config_files() -> Iterator[Path]:
yield Path("/etc/pylintrc").resolve()
except OSError:
pass


def find_subdirectory_config_files(basedir: Path | str) -> Iterator[Path]:
"""Find config file in arbitrary subdirectory."""
yield from _yield_default_files(basedir)
3 changes: 1 addition & 2 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ def set_current_module(self, modname: str, filepath: str | None = None) -> None:
# If there is an actual filepath we might need to update the config attribute
if filepath:
config_path, namespace = self._get_namespace_for_file(
Path(filepath), self._directory_namespaces
Path(filepath).resolve(), self._directory_namespaces
)
if namespace:
self.config = namespace
Expand All @@ -940,7 +940,6 @@ def set_current_module(self, modname: str, filepath: str | None = None) -> None:
def _get_namespace_for_file(
self, filepath: Path, namespaces: DirectoryNamespaceDict
) -> tuple[Path | None, argparse.Namespace | None]:
filepath = filepath.resolve()
for directory in namespaces:
if _is_relative_to(filepath, directory):
_, namespace = self._get_namespace_for_file(
Expand Down
20 changes: 20 additions & 0 deletions tests/lint/test_pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from unittest.mock import patch

from pytest import CaptureFixture
from test_regr import REGR_DATA

from pylint.lint.pylinter import PyLinter
from pylint.typing import FileItem
from pylint.utils import FileState


Expand Down Expand Up @@ -48,3 +50,21 @@ 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_global_vs_local_astroid_module() -> None:
fileitems = iter(
[
FileItem("filename", __file__, "modname"),
FileItem("name2", str(Path(REGR_DATA, "decimal_inference.py")), "mod2"),
]
)
linter1 = PyLinter()
ast_mapping = linter1._get_asts(fileitems, None)
with linter1._astroid_module_checker() as check_astroid_module:
linter1._lint_files(ast_mapping, check_astroid_module)
stats1 = linter1.stats
linter2 = PyLinter()
linter2._lint_files(ast_mapping, None)
stats2 = linter2.stats
assert str(stats1) == str(stats2)

0 comments on commit 9b3576f

Please sign in to comment.