I'm running into an issue where pants can't infer ...
# general
g
I'm running into an issue where pants can't infer a dependency when importing something from an
__init__.py
say...
Copy code
# module_a/sub_dir/__init__.py
from module_a.sub_dir.other_thing import other_thing
then
Copy code
# module_b/thing.py

from module_a.sub_dir import other_thing
pants is failing to infer the deps in
module_b/thing.py
I tried setting
[python-infer].init_files
to
always
with no luck.
Copy code
[python-infer]
init_files = "always"
b
Does importing the non-init path work as you’d expect in module b?
👍 1
g
I lied. It's different. It's constants and methods defined in the
__init__.py
that can't be inferred.
Copy code
# module_a/sub_dir/__init__.py
MY_CONST = "hello"
Copy code
# module_b/thing.py

from module_a.sub_dir import MY_CONST
I'm not really sure what was going on but I was able to solve this by having the python_sources target depend on itself...
Copy code
python_sources(
    name="my_name",
    dependencies=[":my_name"],
)
well, it turns out I do know what was partially causing it. I had split out
__init__.py
into it's own python_source target. This broke things:
Copy code
python_source(
    name="init",
    source="__init__.py",
    dependencies=[":my_name"],
)

python_sources(
    name="my_name",
)
b
Ah yes, having a file owned by two targets will likely confuse pants about which one to choose for dep inference. If you haven’t solved it you can either: • adjust
my_name
sources
to exclude
__init__.py
. Something like
sources = [“*.py”, “!__init__.py”]
• Have just one target and use
overrides
to customise the file(s) that need it: https://www.pantsbuild.org/2.19/docs/using-pants/key-concepts/targets-and-build-files#target-generation I recommend the second
👍 1
g
I wish the error message was clearer, kind of like what happens when you have multiple owners of a 3rd party dep where it says, "Hey, I don't know if I should use 3rd party dep from A or B"
b
Yes, that would be great. If you have a moment, could you file an issue with a reproducer of the bad diagnostic? Thanks!
👍 1