minor annoyance, but it looks like pants thinks it...
# general
h
minor annoyance, but it looks like pants thinks it can't infer a dependency when it should actually be able to? I'm not sure how to best summarize the problem, so more details in thread ๐Ÿงต
MWE (with
[pants-infer].unowned_dependency_behavior = "error"
)
Copy code
# tests/test.py
from importlib import resources
from tests.html_examples import __package__ as html_package

def test_example():
    assert resources.files(html_package).joinpath("example.html").exists()
    assert resources.files(html_package).joinpath("example.html").read_text()
Copy code
# tests/BUILD
python_tests(
    overrides={
        "test.py": {
            "dependencies": ["tests/html_examples:html-resources"],
        },
    },
)
Copy code
# tests/html_examples/BUILD
resources(
    name="html-resources",
    sources=["*.html"],
)
running
pants test ::
will raise a
UnownedDependencyError
with Pants complaining that it cannot infer the owners for
tests.html_examples.__package__
. however, adding a
# pants: no-infer-dep
comment allows the test to pass, indicating to me that it's able to find the dependency just fine. alternatively, if I add a
tests/html_examples/__init__.py
file and a
python_sources()
to
tests/html_examples/BUILD
, pants is also able to pass just fine, even though the init file should be unnecessary when using
importlib.resources
b
Hm, I would guess Pants is assuming that Python's "normal" rules for modules apply, i.e.
__init__.py
is required, and doesn't realise that this particular incantation is valid too. Could you file an issue? The MWE is great, it'd be good to have the required
pants.toml
included too ๐Ÿ‘
h
it sometimes feels like python's rules are always in flux, but as of Python 3.3, namespace packages are allowed, i.e. packages with no
__init__.py
file.
importlib.resources
supports namespace packages, though
pkgutil
doesn't my
pants.toml
is fairly bare, with the only thing i believe to be relevant here is the dependency behavior mentioned above:
Copy code
# pants.toml
...
[python-infer]
unowned_dependency_behavior = "error"
use_rust_parser = true
...
for reference, this is the error message:
Copy code
pants test ::    
14:13:54.50 [ERROR] 1 Exception encountered:

Engine traceback:
  in `test` goal

UnownedDependencyError: Pants cannot infer owners for the following imports in the target tests/test.py:

  * tests.html_examples.__package__ (line: 9)

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/2.20/docs/using-pants/troubleshooting-common-issues#import-errors-and-missing-dependencies> for common problems.
b
Ah, I see. Can you file an issue? Thanks
c
I think it might be that we don't handle
___package___
, so Pants can't make the dep link or find the owner. But the explicit dependency in the BUILD file will cause html-resources to show up. So even if Pants doesn't have an owner for that import, Python will still be able to import from that file/package