Skip to content

Commit a2e3841

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

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
@@ -5,23 +5,27 @@
55
import os
66
import tempfile
77
import unittest
8+
89
from pylint import lint
910
from pylint.testutils import GenericTestReporter
1011

12+
1113
class ModuleShadowingTest(unittest.TestCase):
1214

1315
def setUp(self):
1416
self.tempdir = tempfile.TemporaryDirectory()
1517
self.pkg_dir = os.path.join(self.tempdir.name, "my_module")
1618
os.makedirs(self.pkg_dir)
17-
18-
with open(os.path.join(self.pkg_dir, "__init__.py"), "w", encoding="utf-8") as f:
19+
20+
with open(
21+
os.path.join(self.pkg_dir, "__init__.py"), "w", encoding="utf-8"
22+
) as f:
1923
f.write("")
20-
24+
2125
self.utils_file = os.path.join(self.pkg_dir, "utils.py")
2226
with open(self.utils_file, "w", encoding="utf-8") as f:
2327
f.write("def format():\n pass\n\ndef other_method():\n pass\n")
24-
28+
2529
self.test_file = os.path.join(self.tempdir.name, "main.py")
2630

2731
def tearDown(self):
@@ -30,18 +34,18 @@ def tearDown(self):
3034
def _run_pylint(self, code):
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):
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)