From 1afbd63adcdf55b00cd6a5c21091723696312abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 25 Aug 2022 16:19:21 +0200 Subject: [PATCH 1/9] Patch ``sys.path`` on a per-file basis --- doc/whatsnew/fragments/7339.other | 3 ++ pylint/lint/expand_modules.py | 26 +++-------- pylint/lint/pylinter.py | 44 ++++++++++--------- pylint/lint/utils.py | 20 ++++++++- .../redefined_variable_type.txt | 2 +- tests/lint/unittest_lint.py | 30 +++++++++++++ tests/test_self.py | 10 ++--- 7 files changed, 86 insertions(+), 49 deletions(-) create mode 100644 doc/whatsnew/fragments/7339.other diff --git a/doc/whatsnew/fragments/7339.other b/doc/whatsnew/fragments/7339.other new file mode 100644 index 0000000000..0d075e0a5f --- /dev/null +++ b/doc/whatsnew/fragments/7339.other @@ -0,0 +1,3 @@ +Pylint now tries to patch ``sys.path`` on a per file basis instead of per run. + +Closes #7339 diff --git a/pylint/lint/expand_modules.py b/pylint/lint/expand_modules.py index 27d4bd8121..ddcf2fe00c 100644 --- a/pylint/lint/expand_modules.py +++ b/pylint/lint/expand_modules.py @@ -11,6 +11,7 @@ from astroid import modutils +from pylint.lint.utils import fix_import_path, get_python_path from pylint.typing import ErrorDescriptionDict, ModuleDescriptionDict @@ -23,24 +24,6 @@ def _is_package_cb(inner_path: str, parts: list[str]) -> bool: ) -def get_python_path(filepath: str) -> str: - """TODO This get the python path with the (bad) assumption that there is always - an __init__.py. - - This is not true since python 3.3 and is causing problem. - """ - dirname = os.path.realpath(os.path.expanduser(filepath)) - if not os.path.isdir(dirname): - dirname = os.path.dirname(dirname) - while True: - if not os.path.exists(os.path.join(dirname, "__init__.py")): - return dirname - old_dirname = dirname - dirname = os.path.dirname(dirname) - if old_dirname == dirname: - return os.getcwd() - - def _is_in_ignore_list_re(element: str, ignore_list_re: list[Pattern[str]]) -> bool: """Determines if the element is matched in a regex ignore-list.""" return any(file_pattern.match(element) for file_pattern in ignore_list_re) @@ -144,9 +127,10 @@ def expand_modules( ) or _is_in_ignore_list_re(subfilepath, ignore_list_paths_re): continue - modpath = _modpath_from_file( - subfilepath, is_namespace, path=additional_search_path - ) + with fix_import_path((something,)): + modpath = _modpath_from_file( + subfilepath, is_namespace, path=additional_search_path + ) submodname = ".".join(modpath) result.append( { diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 472b3b0302..5447452987 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -658,6 +658,8 @@ def check(self, files_or_modules: Sequence[str] | str) -> None: return # 3) Get all FileItems + # TODO: 2.16: See if this sys.path patching can be removed as we already + # do this in _expand_modules as well with fix_import_path(files_or_modules): if self.config.from_stdin: fileitems = iter( @@ -670,12 +672,11 @@ def check(self, files_or_modules: Sequence[str] | str) -> None: # The contextmanager also opens all checkers and sets up the PyLinter class with self._astroid_module_checker() as check_astroid_module: - with fix_import_path(files_or_modules): - # 4) Get the AST for each FileItem - ast_per_fileitem = self._get_asts(fileitems, data) + # 4) Get the AST for each FileItem + ast_per_fileitem = self._get_asts(fileitems, data) - # 5) Lint each ast - self._lint_files(ast_per_fileitem, check_astroid_module) + # 5) Lint each ast + self._lint_files(ast_per_fileitem, check_astroid_module) def _get_asts( self, fileitems: Iterator[FileItem], data: str | None @@ -758,24 +759,25 @@ def _lint_file( - ast: AST of the module :raises AstroidError: for any failures stemming from astroid """ - self.set_current_module(file.name, file.filepath) - self._ignore_file = False - self.file_state = FileState(file.modpath, self.msgs_store, module) - # fix the current file (if the source file was not available or - # if it's actually a c extension) - self.current_file = module.file + with fix_import_path((file.filepath,)): + self.set_current_module(file.name, file.filepath) + self._ignore_file = False + self.file_state = FileState(file.modpath, self.msgs_store, module) + # fix the current file (if the source file was not available or + # if it's actually a c extension) + self.current_file = module.file - try: - check_astroid_module(module) - except Exception as e: - raise astroid.AstroidError from e + try: + check_astroid_module(module) + except Exception as e: + raise astroid.AstroidError from e - # warn about spurious inline messages handling - spurious_messages = self.file_state.iter_spurious_suppression_messages( - self.msgs_store - ) - for msgid, line, args in spurious_messages: - self.add_message(msgid, line, None, args) + # warn about spurious inline messages handling + spurious_messages = self.file_state.iter_spurious_suppression_messages( + self.msgs_store + ) + for msgid, line, args in spurious_messages: + self.add_message(msgid, line, None, args) def _check_file( self, diff --git a/pylint/lint/utils.py b/pylint/lint/utils.py index ff2812e7e6..473a726d32 100644 --- a/pylint/lint/utils.py +++ b/pylint/lint/utils.py @@ -5,6 +5,7 @@ from __future__ import annotations import contextlib +import os import sys import traceback from collections.abc import Iterator, Sequence @@ -12,7 +13,24 @@ from pathlib import Path from pylint.config import PYLINT_HOME -from pylint.lint.expand_modules import get_python_path + + +def get_python_path(filepath: str) -> str: + """TODO This get the python path with the (bad) assumption that there is always + an __init__.py. + + This is not true since python 3.3 and is causing problem. + """ + dirname = os.path.realpath(os.path.expanduser(filepath)) + if not os.path.isdir(dirname): + dirname = os.path.dirname(dirname) + while True: + if not os.path.exists(os.path.join(dirname, "__init__.py")): + return dirname + old_dirname = dirname + dirname = os.path.dirname(dirname) + if old_dirname == dirname: + return os.getcwd() def prepare_crash_report(ex: Exception, filepath: str, crash_file_path: str) -> Path: diff --git a/tests/functional/ext/redefined_variable_type/redefined_variable_type.txt b/tests/functional/ext/redefined_variable_type/redefined_variable_type.txt index 389571454f..3b6e8f4643 100644 --- a/tests/functional/ext/redefined_variable_type/redefined_variable_type.txt +++ b/tests/functional/ext/redefined_variable_type/redefined_variable_type.txt @@ -3,7 +3,7 @@ redefined-variable-type:21:8:21:40:MyClass.__init__:Redefinition of a_str type f redefined-variable-type:33:12:33:23:MyClass.some_method.func:Redefinition of var type from int to str:UNDEFINED redefined-variable-type:37:8:37:21:MyClass.some_method:Redefinition of myint type from int to bool:UNDEFINED redefined-variable-type:39:0:39:18::Redefinition of _OK type from bool to str:UNDEFINED -redefined-variable-type:49:4:49:19:other_function:Redefinition of instance type from redefined_variable_type.MyClass to bool:UNDEFINED +redefined-variable-type:49:4:49:19:other_function:Redefinition of instance type from functional.ext.redefined_variable_type.redefined_variable_type.MyClass to bool:UNDEFINED redefined-variable-type:51:0:51:29::Redefinition of SOME_FLOAT type from float to int:UNDEFINED redefined-variable-type:71:8:71:16:func2:Redefinition of var3 type from str to int:UNDEFINED redefined-variable-type:75:4:75:11:func2:Redefinition of var type from bool to int:UNDEFINED diff --git a/tests/lint/unittest_lint.py b/tests/lint/unittest_lint.py index d2fa888fcc..e16443fc0e 100644 --- a/tests/lint/unittest_lint.py +++ b/tests/lint/unittest_lint.py @@ -12,6 +12,7 @@ import re import sys import tempfile +import textwrap from collections.abc import Iterable, Iterator from contextlib import contextmanager from importlib import reload @@ -986,3 +987,32 @@ def test_lint_namespace_package_under_dir_on_path(initialized_linter: PyLinter) with fix_import_path([tmpdir]): linter.check(["namespace_on_path"]) assert linter.file_state.base_name == "namespace_on_path" + + +def test_sys_path_patching_for_namespace_package(initialized_linter: PyLinter) -> None: + """Test that a normal package in the arguments doesn't pollute sys.path. + + Originally reported in https://github.com/PyCQA/pylint/issues/7339. + If pylint is ran over a file within a namespace package which + shares the name with a normal package that 1) is stored in another directory + and 2) is being linted at the same time previously there would be a clash of names + due to the addition of the normal package to sys.path. + This was fixed by only patching sys.path on a per-file basis. + """ + linter = initialized_linter + with tempdir() as tmpdir: + create_files(["ns1/m1.py", "ns1/m2.py", "ns2/ns1/__init__.py"]) + with open(os.path.join(tmpdir, "ns1/m1.py"), "w", encoding="utf-8") as f: + f.write( + textwrap.dedent( + """ + '''Test file''' + import ns1.m2 + print(ns1.m2) + """ + ) + ) + os.chdir(tmpdir) + with fix_import_path([tmpdir]): + linter.check(["ns1/m1.py", "ns2/ns1/__init__.py"]) + assert not linter.reporter.messages diff --git a/tests/test_self.py b/tests/test_self.py index 53c9fb11bc..16bf5e0318 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -267,7 +267,7 @@ def test_wrong_import_position_when_others_disabled(self) -> None: module2 = join(HERE, "regrtest_data", "wrong_import_position.py") expected_output = textwrap.dedent( f""" - ************* Module wrong_import_position + ************* Module regrtest_data.wrong_import_position {module2}:11:0: C0413: Import "import os" should be placed at the top of the module (wrong-import-position) """ ) @@ -325,7 +325,7 @@ def test_json_report_when_file_has_syntax_error(self) -> None: "line": 1, "type": "error", "symbol": "syntax-error", - "module": "syntax_error", + "module": "regrtest_data.syntax_error", } message = output[0] for key, value in expected.items(): @@ -368,7 +368,7 @@ def test_json_report_does_not_escape_quotes(self) -> None: assert isinstance(output[0], dict) expected = { "symbol": "unused-variable", - "module": "unused_variable", + "module": "regrtest_data.unused_variable", "column": 4, "message": "Unused variable 'variable'", "message-id": "W0612", @@ -389,7 +389,7 @@ def test_error_mode_shows_no_score(self) -> None: module = join(HERE, "regrtest_data", "application_crash.py") expected_output = textwrap.dedent( f""" - ************* Module application_crash + ************* Module regrtest_data.application_crash {module}:1:6: E0602: Undefined variable 'something_undefined' (undefined-variable) """ ) @@ -446,7 +446,7 @@ def test_pylintrc_comments_in_values(self) -> None: config_path = join(HERE, "regrtest_data", "comments_pylintrc") expected = textwrap.dedent( f""" - ************* Module test_pylintrc_comments + ************* Module regrtest_data.test_pylintrc_comments {path}:2:0: W0311: Bad indentation. Found 1 spaces, expected 4 (bad-indentation) {path}:1:0: C0114: Missing module docstring (missing-module-docstring) {path}:1:0: C0116: Missing function or method docstring (missing-function-docstring) From 986b0644eee469929623b21c6796a19e6c68d844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 25 Aug 2022 16:51:25 +0200 Subject: [PATCH 2/9] Also patches in tests --- .../redefined_variable_type/redefined_variable_type.txt | 2 +- tests/test_functional.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/functional/ext/redefined_variable_type/redefined_variable_type.txt b/tests/functional/ext/redefined_variable_type/redefined_variable_type.txt index 3b6e8f4643..389571454f 100644 --- a/tests/functional/ext/redefined_variable_type/redefined_variable_type.txt +++ b/tests/functional/ext/redefined_variable_type/redefined_variable_type.txt @@ -3,7 +3,7 @@ redefined-variable-type:21:8:21:40:MyClass.__init__:Redefinition of a_str type f redefined-variable-type:33:12:33:23:MyClass.some_method.func:Redefinition of var type from int to str:UNDEFINED redefined-variable-type:37:8:37:21:MyClass.some_method:Redefinition of myint type from int to bool:UNDEFINED redefined-variable-type:39:0:39:18::Redefinition of _OK type from bool to str:UNDEFINED -redefined-variable-type:49:4:49:19:other_function:Redefinition of instance type from functional.ext.redefined_variable_type.redefined_variable_type.MyClass to bool:UNDEFINED +redefined-variable-type:49:4:49:19:other_function:Redefinition of instance type from redefined_variable_type.MyClass to bool:UNDEFINED redefined-variable-type:51:0:51:29::Redefinition of SOME_FLOAT type from float to int:UNDEFINED redefined-variable-type:71:8:71:16:func2:Redefinition of var3 type from str to int:UNDEFINED redefined-variable-type:75:4:75:11:func2:Redefinition of var type from bool to int:UNDEFINED diff --git a/tests/test_functional.py b/tests/test_functional.py index 74b541bcf7..f204f2db83 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -14,6 +14,7 @@ from _pytest.recwarn import WarningsRecorder from pylint import testutils +from pylint.lint.utils import fix_import_path from pylint.testutils import UPDATE_FILE, UPDATE_OPTION from pylint.testutils.functional import ( FunctionalTestFile, @@ -50,7 +51,11 @@ def test_functional( else: lint_test = testutils.LintModuleTest(test_file, pytestconfig) lint_test.setUp() - lint_test.runTest() + # Pytest changes 'sys.path' depending on the files your running over. + # To fix this, we simply patch sys.path like we do in a normal run, but + # as if we're running against the tes file. + with fix_import_path((test_file.source,)): + lint_test.runTest() if recwarn.list: if ( test_file.base in TEST_WITH_EXPECTED_DEPRECATION @@ -68,6 +73,7 @@ def test_functional( UPDATE_FILE.touch() sys.argv.remove(UPDATE_OPTION) try: + pytest.main(sys.argv) finally: if UPDATE_FILE.exists(): From 5b0826a53a7065fd0d1bb501c953769e6c05901f Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Fri, 26 Aug 2022 09:10:08 +0200 Subject: [PATCH 3/9] Update tests/test_functional.py --- tests/test_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_functional.py b/tests/test_functional.py index f204f2db83..54dc233c24 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -53,7 +53,7 @@ def test_functional( lint_test.setUp() # Pytest changes 'sys.path' depending on the files your running over. # To fix this, we simply patch sys.path like we do in a normal run, but - # as if we're running against the tes file. + # as if we're running against the test file. with fix_import_path((test_file.source,)): lint_test.runTest() if recwarn.list: From 349017b653ed12e934ac42f30f23011e41c4f4c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 29 Aug 2022 13:40:22 +0200 Subject: [PATCH 4/9] Apply suggestions from code review Co-authored-by: Jacob Walls --- tests/lint/unittest_lint.py | 2 +- tests/test_functional.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/lint/unittest_lint.py b/tests/lint/unittest_lint.py index e16443fc0e..f81a4470db 100644 --- a/tests/lint/unittest_lint.py +++ b/tests/lint/unittest_lint.py @@ -993,7 +993,7 @@ def test_sys_path_patching_for_namespace_package(initialized_linter: PyLinter) - """Test that a normal package in the arguments doesn't pollute sys.path. Originally reported in https://github.com/PyCQA/pylint/issues/7339. - If pylint is ran over a file within a namespace package which + If pylint is run over a file within a namespace package which shares the name with a normal package that 1) is stored in another directory and 2) is being linted at the same time previously there would be a clash of names due to the addition of the normal package to sys.path. diff --git a/tests/test_functional.py b/tests/test_functional.py index 54dc233c24..709c0f9579 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -51,7 +51,7 @@ def test_functional( else: lint_test = testutils.LintModuleTest(test_file, pytestconfig) lint_test.setUp() - # Pytest changes 'sys.path' depending on the files your running over. + # Pytest changes 'sys.path' depending on the files you're running over. # To fix this, we simply patch sys.path like we do in a normal run, but # as if we're running against the test file. with fix_import_path((test_file.source,)): @@ -73,7 +73,6 @@ def test_functional( UPDATE_FILE.touch() sys.argv.remove(UPDATE_OPTION) try: - pytest.main(sys.argv) finally: if UPDATE_FILE.exists(): From ecff6d64d3518159d1af7f129c0b47283671958b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 29 Aug 2022 13:41:43 +0200 Subject: [PATCH 5/9] WIP: Try out a lru_cache --- pylint/lint/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pylint/lint/utils.py b/pylint/lint/utils.py index 473a726d32..66522acf98 100644 --- a/pylint/lint/utils.py +++ b/pylint/lint/utils.py @@ -10,11 +10,13 @@ import traceback from collections.abc import Iterator, Sequence from datetime import datetime +from functools import lru_cache from pathlib import Path from pylint.config import PYLINT_HOME +@lru_cache(maxsize=None) def get_python_path(filepath: str) -> str: """TODO This get the python path with the (bad) assumption that there is always an __init__.py. From 008b3485993544b81e6c1ce1f0952cdbafa4b4d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 29 Aug 2022 14:25:41 +0200 Subject: [PATCH 6/9] Test impact --- pylint/lint/pylinter.py | 42 ++++++++++++++++++++--------------------- pylint/lint/utils.py | 2 -- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 5447452987..94c6500adb 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -672,11 +672,12 @@ def check(self, files_or_modules: Sequence[str] | str) -> None: # The contextmanager also opens all checkers and sets up the PyLinter class with self._astroid_module_checker() as check_astroid_module: - # 4) Get the AST for each FileItem - ast_per_fileitem = self._get_asts(fileitems, data) + with fix_import_path(files_or_modules): + # 4) Get the AST for each FileItem + ast_per_fileitem = self._get_asts(fileitems, data) - # 5) Lint each ast - self._lint_files(ast_per_fileitem, check_astroid_module) + # 5) Lint each ast + self._lint_files(ast_per_fileitem, check_astroid_module) def _get_asts( self, fileitems: Iterator[FileItem], data: str | None @@ -759,25 +760,24 @@ def _lint_file( - ast: AST of the module :raises AstroidError: for any failures stemming from astroid """ - with fix_import_path((file.filepath,)): - self.set_current_module(file.name, file.filepath) - self._ignore_file = False - self.file_state = FileState(file.modpath, self.msgs_store, module) - # fix the current file (if the source file was not available or - # if it's actually a c extension) - self.current_file = module.file + self.set_current_module(file.name, file.filepath) + self._ignore_file = False + self.file_state = FileState(file.modpath, self.msgs_store, module) + # fix the current file (if the source file was not available or + # if it's actually a c extension) + self.current_file = module.file - try: - check_astroid_module(module) - except Exception as e: - raise astroid.AstroidError from e + try: + check_astroid_module(module) + except Exception as e: + raise astroid.AstroidError from e - # warn about spurious inline messages handling - spurious_messages = self.file_state.iter_spurious_suppression_messages( - self.msgs_store - ) - for msgid, line, args in spurious_messages: - self.add_message(msgid, line, None, args) + # warn about spurious inline messages handling + spurious_messages = self.file_state.iter_spurious_suppression_messages( + self.msgs_store + ) + for msgid, line, args in spurious_messages: + self.add_message(msgid, line, None, args) def _check_file( self, diff --git a/pylint/lint/utils.py b/pylint/lint/utils.py index 66522acf98..473a726d32 100644 --- a/pylint/lint/utils.py +++ b/pylint/lint/utils.py @@ -10,13 +10,11 @@ import traceback from collections.abc import Iterator, Sequence from datetime import datetime -from functools import lru_cache from pathlib import Path from pylint.config import PYLINT_HOME -@lru_cache(maxsize=None) def get_python_path(filepath: str) -> str: """TODO This get the python path with the (bad) assumption that there is always an __init__.py. From 73ac886b2de7bd912400c28678d1eb1c9309e754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 29 Aug 2022 14:58:43 +0200 Subject: [PATCH 7/9] Try something else --- pylint/lint/expand_modules.py | 10 +++++----- pylint/lint/pylinter.py | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pylint/lint/expand_modules.py b/pylint/lint/expand_modules.py index ddcf2fe00c..226cf8257c 100644 --- a/pylint/lint/expand_modules.py +++ b/pylint/lint/expand_modules.py @@ -17,7 +17,8 @@ def _modpath_from_file(filename: str, is_namespace: bool, path: list[str]) -> list[str]: def _is_package_cb(inner_path: str, parts: list[str]) -> bool: - return modutils.check_modpath_has_init(inner_path, parts) or is_namespace + with fix_import_path((inner_path,)): + return modutils.check_modpath_has_init(inner_path, parts) or is_namespace return modutils.modpath_from_file_with_callback( filename, path=path, is_package_cb=_is_package_cb @@ -127,10 +128,9 @@ def expand_modules( ) or _is_in_ignore_list_re(subfilepath, ignore_list_paths_re): continue - with fix_import_path((something,)): - modpath = _modpath_from_file( - subfilepath, is_namespace, path=additional_search_path - ) + modpath = _modpath_from_file( + subfilepath, is_namespace, path=additional_search_path + ) submodname = ".".join(modpath) result.append( { diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 94c6500adb..91a76ed3e9 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -672,12 +672,11 @@ def check(self, files_or_modules: Sequence[str] | str) -> None: # The contextmanager also opens all checkers and sets up the PyLinter class with self._astroid_module_checker() as check_astroid_module: - with fix_import_path(files_or_modules): - # 4) Get the AST for each FileItem - ast_per_fileitem = self._get_asts(fileitems, data) + # 4) Get the AST for each FileItem + ast_per_fileitem = self._get_asts(fileitems, data) - # 5) Lint each ast - self._lint_files(ast_per_fileitem, check_astroid_module) + # 5) Lint each ast + self._lint_files(ast_per_fileitem, check_astroid_module) def _get_asts( self, fileitems: Iterator[FileItem], data: str | None @@ -1028,9 +1027,10 @@ def check_astroid_module( """ before_check_statements = walker.nbstatements - retval = self._check_astroid_module( - ast_node, walker, rawcheckers, tokencheckers - ) + with fix_import_path((ast_node.file or "",)): + retval = self._check_astroid_module( + ast_node, walker, rawcheckers, tokencheckers + ) # TODO: 3.0: Remove unnecessary assertion assert self.current_name From 03505c4593d9439ea4ff5479b47dbbaf329a79ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 29 Aug 2022 15:43:22 +0200 Subject: [PATCH 8/9] Test impact v2 --- pylint/lint/pylinter.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 91a76ed3e9..5f6bba4444 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -675,8 +675,9 @@ def check(self, files_or_modules: Sequence[str] | str) -> None: # 4) Get the AST for each FileItem ast_per_fileitem = self._get_asts(fileitems, data) - # 5) Lint each ast - self._lint_files(ast_per_fileitem, check_astroid_module) + with fix_import_path(files_or_modules): + # 5) Lint each ast + self._lint_files(ast_per_fileitem, check_astroid_module) def _get_asts( self, fileitems: Iterator[FileItem], data: str | None @@ -1027,10 +1028,9 @@ def check_astroid_module( """ before_check_statements = walker.nbstatements - with fix_import_path((ast_node.file or "",)): - retval = self._check_astroid_module( - ast_node, walker, rawcheckers, tokencheckers - ) + retval = self._check_astroid_module( + ast_node, walker, rawcheckers, tokencheckers + ) # TODO: 3.0: Remove unnecessary assertion assert self.current_name From 496c83ef059036cb5d948574c8cabbe1d92a6305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 29 Aug 2022 16:25:01 +0200 Subject: [PATCH 9/9] Test impact v3 --- pylint/lint/expand_modules.py | 6 +++--- pylint/lint/pylinter.py | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pylint/lint/expand_modules.py b/pylint/lint/expand_modules.py index 226cf8257c..46376643e7 100644 --- a/pylint/lint/expand_modules.py +++ b/pylint/lint/expand_modules.py @@ -11,14 +11,14 @@ from astroid import modutils -from pylint.lint.utils import fix_import_path, get_python_path +from pylint.lint.utils import get_python_path from pylint.typing import ErrorDescriptionDict, ModuleDescriptionDict def _modpath_from_file(filename: str, is_namespace: bool, path: list[str]) -> list[str]: def _is_package_cb(inner_path: str, parts: list[str]) -> bool: - with fix_import_path((inner_path,)): - return modutils.check_modpath_has_init(inner_path, parts) or is_namespace + # with fix_import_path((inner_path,)): + return modutils.check_modpath_has_init(inner_path, parts) or is_namespace return modutils.modpath_from_file_with_callback( filename, path=path, is_package_cb=_is_package_cb diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 5f6bba4444..91a76ed3e9 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -675,9 +675,8 @@ def check(self, files_or_modules: Sequence[str] | str) -> None: # 4) Get the AST for each FileItem ast_per_fileitem = self._get_asts(fileitems, data) - with fix_import_path(files_or_modules): - # 5) Lint each ast - self._lint_files(ast_per_fileitem, check_astroid_module) + # 5) Lint each ast + self._lint_files(ast_per_fileitem, check_astroid_module) def _get_asts( self, fileitems: Iterator[FileItem], data: str | None @@ -1028,9 +1027,10 @@ def check_astroid_module( """ before_check_statements = walker.nbstatements - retval = self._check_astroid_module( - ast_node, walker, rawcheckers, tokencheckers - ) + with fix_import_path((ast_node.file or "",)): + retval = self._check_astroid_module( + ast_node, walker, rawcheckers, tokencheckers + ) # TODO: 3.0: Remove unnecessary assertion assert self.current_name