Skip to content

__path__ mangling in a non namespace package #2474

@chaen

Description

@chaen

Hi,

I have a use case which I am unsure whether it is covered by pylint or not.
I've been through quite a number of issues regarding namespace packages using pkgutil.extend_path, and I could verify that this works fine. Now, what I would like to do is to expose modules in a subdirectory as being part of an upper module. This works fine with python, but pylint gives me the no-name-in-module error.

Here is how to reproduce:

[chaen@notANamespace]$ ls -R
.:
__init__.py  module  useModule.py

./module:
impl  __init__.py

./module/impl:
__init__.py  myClass.py

The useModule.py just imports a class from module and instantiate it. The actual class is in the subdirectory impl, but I do not want this to be visible to the useModule script.

[chaen@notANamespace]$ cat useModule.py 
from module.myClass import MyClass
MyClass()
[chaen@notANamespace]$ cat module/__init__.py 
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__ + '.impl')
[chaen@notANamespace]$ cat module/impl/myClass.py 
class MyClass(object):
  def __init__(self):
    print "Hello PyLint"

The __init__.py in the root and module/impl directories are empty

If I execute it in python, I get what I expect:

[chaen@notANamespace]$ python useModule.py 
Hello PyLint

Pylint however will complain

[chaen@notANamespace]$ pylint useModule.py 
No config file found, using default configuration
************* Module notANamespace.useModule
C:  1, 0: Module name "useModule" doesn't conform to snake_case naming style (invalid-name)
C:  1, 0: Missing module docstring (missing-docstring)
E:  1, 0: No name 'myClass' in module 'notANamespace.module' (no-name-in-module)
E:  1, 0: Unable to import 'module.myClass' (import-error)

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

So I am not sure what makes the namespace packages work and not that. Maybe this isn't the way to 'hide' a subdirectory in python anyway ?

In any case, this are the versions I am using:

[chaen@notANamespace]$ python --version
Python 2.7.15
[chaen@notANamespace]$ pylint --version
No config file found, using default configuration
pylint 1.9.3, 
astroid 1.6.5
Python 2.7.15 (default, May 16 2018, 17:50:09) 
[GCC 8.1.1 20180502 (Red Hat 8.1.1-1)]

Thanks for your help

Activity

PCManticore

PCManticore commented on Dec 28, 2018

@PCManticore
Contributor

Hi @chaen Thanks for reporting an issue. I think pylint is confused by the use of

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__ + '.impl')

While we do have some support for namespace packages, unfortunately we cannot correctly infer what the second argument to extend_path is going to be, which is why you might see this error. I think this could be clearer with using explicit import in __init__ rather than this __path__ mangling.

chaen

chaen commented on Jan 16, 2019

@chaen
Author

Yes, it certainly would be clearer, but also less maintainable if I have to update the list of import in the __init__.py file everytime I add something in my submodule. Unless you have another idea in mind ? :)

added
AstroidRelated to astroid
High effort 🏋Difficult solution or problem to solve
Needs investigation 🔬A bug or crash where it's not immediately obvious what is happenning
and removed on Jun 29, 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

    AstroidRelated to astroidEnhancement ✨Improvement to a componentHigh effort 🏋Difficult solution or problem to solveImport systemNeeds investigation 🔬A bug or crash where it's not immediately obvious what is happenning

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @PCManticore@chaen@Pierre-Sassoulas

        Issue actions

          __path__ mangling in a non namespace package · Issue #2474 · pylint-dev/pylint