Skip to content

Linting fails if module contains module of the same name #4444

@iFreilicht

Description

@iFreilicht

Steps to reproduce

Given multiple files:

.
`-- a/
    |-- a.py
    `-- b.py

Which are all empty, running pylint a fails:

$ pylint a
************* Module a
a/__init__.py:1:0: F0010: error while code parsing: Unable to load file a/__init__.py:
[Errno 2] No such file or directory: 'a/__init__.py' (parse-error)
$

However, if I rename a.py, pylint a succeeds:

$ mv a/a.py a/c.py
$ pylint a
$

Alternatively, I can also touch a/__init__.py, but that shouldn't be necessary anymore.

Current behavior

Running pylint a if a/a.py is present fails while searching for an __init__.py file.

Expected behavior

Running pylint a if a/a.py is present should succeed.

pylint --version output

Result of pylint --version output:

pylint 3.0.0a3
astroid 2.5.6
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0]

Additional info

This also has some side-effects in module resolution. For example, if I create another file r.py:

.
|-- a
|   |-- a.py
|   `-- b.py
`-- r.py

With the content:

from a import b

Running pylint -E r will run fine, but pylint -E r a will fail. Not just for module a, but for module r as well.

************* Module r
r.py:1:0: E0611: No name 'b' in module 'a' (no-name-in-module)
************* Module a
a/__init__.py:1:0: F0010: error while code parsing: Unable to load file a/__init__.py:
[Errno 2] No such file or directory: 'a/__init__.py' (parse-error)

Again, if I rename a.py to c.py, pylint -E r a will work perfectly.

Activity

hippo91

hippo91 commented on May 14, 2021

@hippo91
Contributor

@iFreilicht thanks for your report.

jacobtylerwalls

jacobtylerwalls commented on Jul 3, 2022

@jacobtylerwalls
Member

#4909 was a duplicate.

added this to the 2.15.0 milestone on Jul 3, 2022
jacobtylerwalls

jacobtylerwalls commented on Aug 13, 2022

@jacobtylerwalls
Member

#7114 was not correct and will be reverted in #7304.

Regression test for test_lint.py for anyone attempting a fix:

def test_identically_named_nested_module(initialized_linter: PyLinter) -> None:
    with tempdir():
        create_files(["identical/identical.py"])
        with open("identical/identical.py", "w", encoding="utf-8") as f:
            f.write("import imp")
        initialized_linter.check(["identical"])
    assert initialized_linter.stats.by_msg["deprecated-module"] == 1
removed this from the 2.15.0 milestone on Aug 13, 2022
removed their assignment
on Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🪲False Positive 🦟A message is emitted but nothing is wrong with the codeImport systemNeeds PRThis issue is accepted, sufficiently specified and now needs an implementationnamespace-package

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @Pierre-Sassoulas@iFreilicht@hippo91@jacobtylerwalls

      Issue actions

        Linting fails if module contains module of the same name · Issue #4444 · pylint-dev/pylint