pydeps icon indicating copy to clipboard operation
pydeps copied to clipboard

incorrectly attaching dependency with relative imports in submodules

Open admuresan opened this issue 3 years ago • 1 comments

This is an issue I noticed if for example I have a sub module that imports form its relative namespace without going global.

Take for example the following module structure:

main    |-- __init__.py    |-- sub_modulue        |-- __init__.py        |-- file.py

If file.py has a line like: from . import x

where x is defined in the __init__.py of sub_module, then the dependency solver claims that file imports the __init__.py of main not of its relative submodule.

If I change the line in file.py to: from ..sub_module import x it seems to correctly identify the dependency.

I like using the less specific notation since it lets me reorganize without any changes to code, but it's not a major inconvenience to change.

Great tool. I use to get an ordered list of the files in my module based on inheritance.

admuresan avatar Nov 25 '22 20:11 admuresan

Hi @admuresan and thank you for your interest in pydeps.

I think there might be something else going on, although I'm not entirely sure what.

I've created a tool that converts directories to yaml and back again, which pydeps uses internally for testing, but is also great for communicating test-cases. It's available from PyPI (pip install yamldirs).

If I understand you correctly, you have this structure:

mypkg:
  __init__.py: ''
  sub_module:
    __init__.py: x = 42
    fname.py: |
      from . import x

(save it as fname.yaml and run yamldirs fname.yaml and it will extract the file structure).

I would guess that it could be related to the dummy python file that pydeps creates when running to have a starting point for analyzing your module (you can find the contents by running pydeps with the -LDEBUG flag if you're really interested..)

Could you create a yamldirs snippet that demonstrates your problem?

thebjorn avatar Nov 26 '22 01:11 thebjorn