Skip to content

__path__ mangling in a non namespace package #2474

Open
@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

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

    Issue actions