high-energy-55500
05/08/2024, 3:21 PMhigh-energy-55500
05/08/2024, 3:21 PM[pants-infer].unowned_dependency_behavior = "error"
)
# 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()
# tests/BUILD
python_tests(
overrides={
"test.py": {
"dependencies": ["tests/html_examples:html-resources"],
},
},
)
# 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
broad-processor-92400
05/09/2024, 12:07 AM__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 ๐high-energy-55500
05/09/2024, 1:16 PM__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:
# pants.toml
...
[python-infer]
unowned_dependency_behavior = "error"
use_rust_parser = true
...
for reference, this is the error message:
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.
broad-processor-92400
05/09/2024, 11:25 PMcareful-address-89803
05/13/2024, 5:33 AM___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