Skip to content

Source root not correctly used #10026

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
Julfried opened this issue Oct 15, 2024 · 2 comments · Fixed by #10036
Closed

Source root not correctly used #10026

Julfried opened this issue Oct 15, 2024 · 2 comments · Fixed by #10036
Labels
Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning pyreverse Related to pyreverse component

Comments

@Julfried
Copy link
Contributor

Julfried commented Oct 15, 2024

Bug description

I have a package that sits in a child folder of the current working directory (./src). Now if I want to to run pyreverse for this module I add ./src as a source-root. However pyreverse does not find the module.

Here is an example:

ex/
└── src/
    ├── __init__.py
    └── mymodule/
        ├── __init__.py
        └── testmodule.py

Configuration

No response

Command used

pyreverse -o html --source-root ./src mymodule

Pylint output

raise ImportError(f"No module named {'.'.join(module_parts)}")
ImportError: No module named mymodule

Expected behavior

When specifiy source-roots, pyreverse should also search the modules in this path

Pylint version

pylint 3.3.1
astroid 3.3.5
Python 3.12.7 | packaged by conda-forge | (main, Oct 4 2024, 15:47:54) [MSC v.1941 64 bit (AMD64)]

OS / Environment

win64

Additional dependencies

No response

@Julfried Julfried added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 15, 2024
@Pierre-Sassoulas Pierre-Sassoulas added pyreverse Related to pyreverse component Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 17, 2024
@webknjaz
Copy link
Contributor

webknjaz commented Jan 8, 2025

FTR the linked PR hasn't fixed a no-name-in-module I'm seeing in an src layout with an implicit PEP 420 namespace. No idea why, though.

@Julfried
Copy link
Contributor Author

Julfried commented Jan 9, 2025

Thanks for pointing this out and extending the tests!

I believe the issue lies in the fallback logic here:

# Fall back to legacy discovery by looking for __init__.py upwards
while True:
    if not os.path.exists(os.path.join(dirname, "__init__.py")):
        return dirname

This approach works only for explicit namespaces but fails for implicit ones (PEP 420), as they don't require __init__.py. The "legacy" name might be misleading here. Instead, I think there should be two distinct logic paths:

  1. Explicit namespaces: Identify the module by looking for __init__.py.
  2. Implicit namespaces: If no __init__.py, check if .py files exist in the directory (though this might not be sufficient in all cases).
  3. Error handling: Throw an exception if neither case is satisfied.

My fix mainly addressed scenarios where the source root is a sub-folder of the working directory. This issue seems related but also requires handling implicit namespaces properly. In the next few weeks ,I’ll be short on time, but happy to assist further once there's more clarity!


Reference: I think this could also be linked to the issues I observed in #10077

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning pyreverse Related to pyreverse component
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants