Skip to content

Cannot compute top package's path of a native namespace package #19

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

Closed
pawamoy opened this issue Apr 9, 2020 · 2 comments
Closed

Cannot compute top package's path of a native namespace package #19

pawamoy opened this issue Apr 9, 2020 · 2 comments
Labels
bug Something isn't working module:objects

Comments

@pawamoy
Copy link
Member

pawamoy commented Apr 9, 2020

Describe the bug
Native namespace packages (without __init__.py modules) trigger the following kind of error when attempting to retrieve the top package's path:

ERROR   -  mkdocstrings.handlers.python: Collection failed: <module 'bot' (namespace)> is a built-in module
Traceback (most recent call last):
  File "/home/dadyarri/projects/bjorn/venv_bjorn/lib/python3.8/site-packages/pytkdocs/cli.py", line 179, in main
    print(json.dumps(process_json(line)))
  File "/home/dadyarri/projects/bjorn/venv_bjorn/lib/python3.8/site-packages/pytkdocs/cli.py", line 116, in process_json
    return process_config(json.loads(json_input))
  File "/home/dadyarri/projects/bjorn/venv_bjorn/lib/python3.8/site-packages/pytkdocs/cli.py", line 98, in process_config
    serialized_obj = serialize_object(obj)
  File "/home/dadyarri/projects/bjorn/venv_bjorn/lib/python3.8/site-packages/pytkdocs/serializer.py", line 143, in serialize_object
    relative_file_path=obj.relative_file_path,
  File "/home/dadyarri/projects/bjorn/venv_bjorn/lib/python3.8/site-packages/pytkdocs/objects.py", line 185, in relative_file_path
    top_package_path = Path(inspect.getabsfile(top_package)).parent
  File "/usr/lib/python3.8/inspect.py", line 720, in getabsfile
    _filename = getsourcefile(object) or getfile(object)
  File "/usr/lib/python3.8/inspect.py", line 696, in getsourcefile
    filename = getfile(object)
  File "/usr/lib/python3.8/inspect.py", line 659, in getfile
    raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'bot' (namespace)> is a built-in module

To Reproduce
Simply try to collect documentation for a native namespace package.

Expected behavior
No exception. We should find another way to determine the top package's path in that case.

Reference: mkdocstrings/mkdocstrings#79 (comment)

@pawamoy pawamoy added the bug Something isn't working label Apr 9, 2020
@shyamd
Copy link

shyamd commented Apr 23, 2020

I have a solution based on iterating from the top path down till we find a non-namespace package and using that, but it won't work well if the namespace package is further in. Making a test for this is going to be a giant pain.

Solution:

subspace_count = len(self.path.split("."))
namespaces = [".".join(self.path.split(".", maxsplit=maxsplit)[:-1]) for maxsplit in range(1, subspace_count)]

for namespace in namespaces:
    try:
        top_package = sys.modules[namespace]
    except KeyError:
        try:
            importlib.import_module(namespace)
        except ImportError:
            return ""
        top_package = sys.modules[namespace]
    try:
        top_package_path = Path(inspect.getabsfile(top_package)).parent
        return str(Path(self.file_path).relative_to(top_package_path.parent))
    except ValueError:
        return ""
    except TypeError:
        pass

pawamoy pushed a commit that referenced this issue Apr 25, 2020
Native namespace packages don't have an `__init__.py` module.
This commit adds support for finding relative file path
of such packages.

References: #19, #22.
pawamoy pushed a commit that referenced this issue Apr 25, 2020
Native namespace packages don't have an `__init__.py` module.
This commit adds support for finding relative file path
of such packages.

References: #19, #22.
@pawamoy
Copy link
Member Author

pawamoy commented Apr 25, 2020

Fixed by @shyamd in #22 ❤️

@pawamoy pawamoy closed this as completed Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working module:objects
Projects
None yet
Development

No branches or pull requests

2 participants