Skip to content

Commit c14dc93

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

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed
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/test_import_module_shadowing.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
import os
44
import tempfile
55
import unittest
6+
67
from pylint import lint
78
from pylint.testutils import GenericTestReporter
89

10+
911
class ModuleShadowingTest(unittest.TestCase):
1012

1113
def setUp(self):
1214
self.tempdir = tempfile.TemporaryDirectory()
1315
self.pkg_dir = os.path.join(self.tempdir.name, "my_module")
1416
os.makedirs(self.pkg_dir)
15-
16-
with open(os.path.join(self.pkg_dir, "__init__.py"), "w", encoding="utf-8") as f:
17+
18+
with open(
19+
os.path.join(self.pkg_dir, "__init__.py"), "w", encoding="utf-8"
20+
) as f:
1721
f.write("")
18-
22+
1923
self.utils_file = os.path.join(self.pkg_dir, "utils.py")
2024
with open(self.utils_file, "w", encoding="utf-8") as f:
2125
f.write("def format():\n pass\n\ndef other_method():\n pass\n")
22-
26+
2327
self.test_file = os.path.join(self.tempdir.name, "main.py")
2428

2529
def tearDown(self):
@@ -28,18 +32,18 @@ def tearDown(self):
2832
def _run_pylint(self, code):
2933
with open(self.test_file, "w", encoding="utf-8") as f:
3034
f.write(code)
31-
35+
3236
reporter = GenericTestReporter()
3337
lint.Run(
3438
[
3539
"--disable=all",
3640
"--enable=no-name-in-module",
3741
"--persistent=no",
3842
"--rcfile=",
39-
self.test_file
43+
self.test_file,
4044
],
4145
reporter=reporter,
42-
exit=False
46+
exit=False,
4347
)
4448
return reporter.messages
4549

@@ -86,5 +90,6 @@ def test_shadowed_import_without_call(self):
8690
errors = [msg for msg in messages if msg.msg_id == "E0611"]
8791
self.assertEqual(len(errors), 0)
8892

93+
8994
if __name__ == "__main__":
9095
unittest.main()

0 commit comments

Comments
 (0)