Skip to content

Commit d676e52

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 019d4e6 commit d676e52

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

doc/whatsnew/fragments/10193.bugfix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Fix incorrect `no-name-in-module` when calling methods on shadowed imports (e.g. `import pkg.sub as pkg`).
22

3-
Closes #10193
3+
Closes #10193

pylint/checkers/variables.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,13 @@ class C: ...
201201
def _infer_name_module(node: nodes.Import, name: str) -> Generator[InferenceResult]:
202202
context = astroid.context.InferenceContext()
203203
context.lookupname = name
204-
205-
if (len(node.names) == 1 and
206-
node.names[0][1] and
207-
'.' in node.names[0][0] and
208-
node.names[0][0].startswith(node.names[0][1] + '.')):
204+
205+
if (
206+
len(node.names) == 1
207+
and node.names[0][1]
208+
and "." in node.names[0][0]
209+
and node.names[0][0].startswith(node.names[0][1] + ".")
210+
):
209211

210212
module = next(node.infer(context, asname=False), None)
211213
if isinstance(module, nodes.Module):
@@ -3159,12 +3161,14 @@ def _check_module_attrs(
31593161
"""Check that module_names (list of string) are accessible through the
31603162
given module, if the latest access name corresponds to a module, return it.
31613163
"""
3162-
if (isinstance(node, nodes.Import) and module_names):
3164+
if isinstance(node, nodes.Import) and module_names:
31633165
for alias, asname in node.names:
3164-
if (asname and
3165-
'.' in alias and
3166-
alias.startswith(asname + '.') and
3167-
module_names[0] == alias.split('.')[-1]):
3166+
if (
3167+
asname
3168+
and "." in alias
3169+
and alias.startswith(asname + ".")
3170+
and module_names[0] == alias.split(".")[-1]
3171+
):
31683172
module_names.pop(0)
31693173
break
31703174
while module_names:

tests/checkers/unittest_import_module_shadowing.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,45 @@
77
import os
88
import tempfile
99
import unittest
10+
1011
from pylint import lint
1112
from pylint.testutils import GenericTestReporter
1213

14+
1315
class ModuleShadowingTest(unittest.TestCase):
1416

1517
def setUp(self) -> None:
1618
self.tempdir = tempfile.TemporaryDirectory()
1719
self.addCleanup(self.tempdir.cleanup)
1820
self.pkg_dir = os.path.join(self.tempdir.name, "my_module")
1921
os.makedirs(self.pkg_dir)
20-
21-
with open(os.path.join(self.pkg_dir, "__init__.py"), "w", encoding="utf-8") as f:
22+
23+
with open(
24+
os.path.join(self.pkg_dir, "__init__.py"), "w", encoding="utf-8"
25+
) as f:
2226
f.write("")
23-
27+
2428
self.utils_file = os.path.join(self.pkg_dir, "utils.py")
2529
with open(self.utils_file, "w", encoding="utf-8") as f:
2630
f.write("def format():\n pass\n\ndef other_method():\n pass\n")
27-
31+
2832
self.test_file = os.path.join(self.tempdir.name, "main.py")
2933

3034
def _run_pylint(self, code: str) -> list[Message]:
3135
with open(self.test_file, "w", encoding="utf-8") as f:
3236
f.write(code)
33-
37+
3438
reporter = GenericTestReporter()
3539
lint.Run(
3640
[
3741
"--disable=all",
3842
"--enable=no-name-in-module",
3943
"--persistent=no",
4044
"--rcfile=",
41-
self.test_file
45+
self.test_file,
4246
],
4347
reporter=reporter,
44-
exit=False
48+
exit=False,
4549
)
4650
return reporter.messages
4751

@@ -88,5 +92,6 @@ def test_shadowed_import_without_call(self) -> None:
8892
errors = [msg for msg in messages if msg.msg_id == "E0611"]
8993
self.assertEqual(len(errors), 0)
9094

95+
9196
if __name__ == "__main__":
9297
unittest.main()

0 commit comments

Comments
 (0)