Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit namespace packages (PEP420) cause import-error #4057

Closed
csymeonides-mf opened this issue Feb 1, 2021 · 6 comments
Closed

Implicit namespace packages (PEP420) cause import-error #4057

csymeonides-mf opened this issue Feb 1, 2021 · 6 comments
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code

Comments

@csymeonides-mf
Copy link

Is this fixed in the preview release? No

Steps to reproduce

I'm experiencing the same issue as #842.

Trying to use PEP420 Implicit Namespace Packages (i.e. without an __init__.py in the top-level package directory). My tree looks like this:

src/
    my_namespace/
        my_package/
            __init__.py
            foo.py
            bar.py

and foo.py contains:

from my_namespace.my_package.bar import Bar

Current behavior

Running pylint src works fine, but pylint src/my_namespace/my_package/foo.py returns an import-error which is a false positive.

Expected behavior

No import-error

pylint --version output

pylint 2.6.0
astroid 2.4.2
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)]
@Pierre-Sassoulas Pierre-Sassoulas added Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code labels Feb 6, 2021
@ghost
Copy link

ghost commented Feb 9, 2021

I second this one. I set up a simple repo to reproduce the issue:
https://github.com/ssfabiopicchi/precommit-test

pylint 2.6.0
astroid 2.4.2
Python 3.8.6 | packaged by conda-forge | (default, Oct  7 2020, 19:08:05) 
[GCC 7.5.0]

Somehow it works for me in another repo, but I haven't investigated it further yet.

@ghost
Copy link

ghost commented Feb 9, 2021

At first I thought the issue was related to pre-commit because it was only happening when running with pre-commit and not when running it directly

@bdegreve
Copy link

Is the package properly installed? Taken at face value, the top namespace would have to be src, not my_namespace. pylint may not know in needs to start searching in src. When installing it with following setup.py, I don't have any troubles:

from setuptools import find_namespace_packages, setup
setup(
    name="my-package",
    version="1.0.0",
    packages=["my_namespace.my_package"],
    package_dir={
        '': 'src',
    },
)

Then install with:

pip install --editable .

Then:

pylint .\src\my_namespace\my_package\foo.py
************* Module my_package.foo
src\my_namespace\my_package\foo.py:1:0: C0304: Final newline missing (missing-final-newline)
src\my_namespace\my_package\foo.py:1:0: C0104: Disallowed name "foo" (disallowed-name)
src\my_namespace\my_package\foo.py:1:0: C0114: Missing module docstring (missing-module-docstring)
src\my_namespace\my_package\foo.py:1:0: W0611: Unused Bar imported from my_namespace.my_package.bar (unused-import)

-----------------------------------------------------------------------
Your code has been rated at -30.00/10 (previous run: -80.00/10, +50.00)

@kayabaNerve
Copy link

kayabaNerve commented Mar 30, 2021

I have a similar issue, except it's my namespace package as a whole which fails on new Pylint versions (any >2.4.4).

My rcfile has an edited init hook of init-hook="import sys; import os; sys.path.append(os.path.join(os.getcwd(), 'e2e'));" to handle discovery as a package. Pylint 2.4.4 handles this without issue. I just use the top folder of the namespace package as the lint folder (so I live in /, and the Python section of the project is /e2e, so I run pylint --rcfile rc e2e which internally imports via e2e.).

I couldn't get modern versions to work, and I followed this issue due to the timing of it (it appeared recently with the same subject and was still open). I believe this issue is pointing at a behavior change between Pylint versions, which has been frequent with namespace packages. The way to move forward may be correcting the namespace, something I was unable to do as AFAICT, my namespace was perfect. This issue's does seem incorrect when reading through your comment though. If the actual namespace is incorrect, I'd be curious to know how previous versions responded.

I may be best off creating a new issue though.

@hippo91
Copy link
Contributor

hippo91 commented Apr 9, 2021

@csymeonides-mf thanks for your report. From @bdegreve investigations (thanks by the way :+1) it seems that this is not a pylint bug.
I'm i wrong?
@kayabaNerve i think it will be clearer to open an new issue but maybe is it related to #1361 ?

@csymeonides-mf
Copy link
Author

@bdegreve Apologies I forgot to reply to your comment. You're right, I hadn't installed the package, and that's because we use pip-tools and there were some issues with installing local packages. So I agree with @hippo91, this isn't really a pylint bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

No branches or pull requests

5 participants