Skip to content

Commit 4799e5b

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

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
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/functional/i/import_module_shadowing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ def setUp(self):
1212
self.tempdir = tempfile.TemporaryDirectory()
1313
self.pkg_dir = os.path.join(self.tempdir.name, "my_module")
1414
os.makedirs(self.pkg_dir)
15-
15+
1616
with open(os.path.join(self.pkg_dir, "__init__.py"), "w", encoding="utf-8") as f:
1717
f.write("")
18-
18+
1919
self.utils_file = os.path.join(self.pkg_dir, "utils.py")
2020
with open(self.utils_file, "w", encoding="utf-8") as f:
2121
f.write("def format():\n pass\n\ndef other_method():\n pass\n")
22-
22+
2323
self.test_file = os.path.join(self.tempdir.name, "main.py")
2424

2525
def tearDown(self):
@@ -28,7 +28,7 @@ def tearDown(self):
2828
def _run_pylint(self, code):
2929
with open(self.test_file, "w", encoding="utf-8") as f:
3030
f.write(code)
31-
31+
3232
reporter = GenericTestReporter()
3333
lint.Run(
3434
[

0 commit comments

Comments
 (0)