<#18080 Dependency inference for Python sources fa...
# github-notifications
q
#18080 Dependency inference for Python sources fails to report imports in fully qualified style when a non-existing module is addressed Issue created by AlexTereshenkov Describe the bug Dependency inference for Python sources fails to report imports in fully qualified style when a non-existing module is addressed. On the https://github.com/pantsbuild/example-python, diff:
Copy code
diff --git a/helloworld/greet/greeting.py b/helloworld/greet/greeting.py
index 6a5eb1a..f4d5227 100644
--- a/helloworld/greet/greeting.py
+++ b/helloworld/greet/greeting.py
@@ -10,6 +10,7 @@ import pkg_resources
 
 from helloworld.translator.translator import LanguageTranslator
 
+from helloworld.translator.foobar import baz
 
 class Greeter:
     def __init__(
diff --git a/pants.toml b/pants.toml
index fc5bff0..24926c0 100644
--- a/pants.toml
+++ b/pants.toml
@@ -62,3 +62,6 @@ resolves = { python-default = "python-default.lock"}
 #  problematic system Pythons. See
 #  <https://www.pantsbuild.org/docs/python-interpreter-compatibility#changing-the-interpreter-search-path>.
 search_path = ["<PATH>", "<PYENV>"]
+
+[python-infer]
+unowned_dependency_behavior = "error"
running on latest Pants main:
Copy code
$ ./pants_from_sources dependencies helloworld/greet/greeting.py
[ERROR] 1 Exception encountered:

Engine traceback:
  in `dependencies` goal

UnownedDependencyError: Pants cannot infer owners for the following imports in the target helloworld/greet/greeting.py:lib:

  * helloworld.translator.foobar.baz (line: 13)

If you do not expect an import to be inferrable, add `# pants: no-infer-dep` to the import line. Otherwise, see <https://www.pantsbuild.org/v2.16/docs/troubleshooting#import-errors-and-missing-dependencies> for common problems.
correctly reports the module non-existing module. With this diff:
Copy code
diff --git a/helloworld/greet/greeting.py b/helloworld/greet/greeting.py
index 6a5eb1a..bab281f 100644
--- a/helloworld/greet/greeting.py
+++ b/helloworld/greet/greeting.py
@@ -10,6 +10,7 @@ import pkg_resources
 
 from helloworld.translator.translator import LanguageTranslator
 
+import helloworld.translator.foobar
 
 class Greeter:
     def __init__(
diff --git a/pants.toml b/pants.toml
index fc5bff0..24926c0 100644
--- a/pants.toml
+++ b/pants.toml
@@ -62,3 +62,6 @@ resolves = { python-default = "python-default.lock"}
 #  problematic system Pythons. See
 #  <https://www.pantsbuild.org/docs/python-interpreter-compatibility#changing-the-interpreter-search-path>.
 search_path = ["<PATH>", "<PYENV>"]
+
+[python-infer]
+unowned_dependency_behavior = "error"
running
Copy code
$ ./pants_from_sources dependencies helloworld/greet/greeting.py
//:reqs#setuptools
//:reqs#types-setuptools
helloworld/greet:translations
helloworld/translator/translator.py:lib
raises no errors. Pants version
2.16.0.dev5
OS Linux Additional info This may be done by design, but it feels like a bug to me as Pants should know that there's no file
foobar.py
in the
helloworld.translator
package. I thought first that this is done because the
foobar
could be a variable coming from the
__init__.py
of the package, but IIRC then the import statement should be written as
from helloworld.translator import foobar
, one can't write
import helloworld.translator.foobar
to read variable
foobar
declared in the
helloworld/translator/__init__.py
file. pantsbuild/pants