teemtee / tmt

Test Management Tool
MIT License
85 stars 129 forks source link

tmt pre-commit link hook does not seem to work after packaging changes #2351

Open happz opened 1 year ago

happz commented 1 year ago

Looks like importlib.resources of unexpected version? Python version mismatch? files is available since Python 3.9, what version is pre-commit using?

$ hatch run dev:pre-commit run --all-files tmt-lint
tmt lint.................................................................Failed
- hook id: tmt-lint
- exit code: 128

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/happz/git/tmt/tmt/__init__.py", line 20, in <module>
    from tmt.base import Clean, Plan, Run, Status, Story, Test, Tree
  File "/home/happz/git/tmt/tmt/base.py", line 42, in <module>
    import tmt.checks
  File "/home/happz/git/tmt/tmt/checks/__init__.py", line 6, in <module>
    import tmt.steps.provision
  File "/home/happz/git/tmt/tmt/steps/__init__.py", line 34, in <module>
    import tmt.export
  File "/home/happz/git/tmt/tmt/export/__init__.py", line 42, in <module>
    TEMPLATES_DIRECTORY = tmt.utils.resource_files('export/templates')
  File "/home/happz/git/tmt/tmt/utils.py", line 5929, in resource_files
    return Path(importlib.resources.files(package)) / path  # type: ignore[arg-type]
AttributeError: module 'importlib.resources' has no attribute 'files'
fatal: Invalid path '/.fmf': No such file or directory
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/happz/git/tmt/tmt/__init__.py", line 20, in <module>
    from tmt.base import Clean, Plan, Run, Status, Story, Test, Tree
  File "/home/happz/git/tmt/tmt/base.py", line 42, in <module>
    import tmt.checks
  File "/home/happz/git/tmt/tmt/checks/__init__.py", line 6, in <module>
    import tmt.steps.provision
  File "/home/happz/git/tmt/tmt/steps/__init__.py", line 34, in <module>
    import tmt.export
  File "/home/happz/git/tmt/tmt/export/__init__.py", line 42, in <module>
    TEMPLATES_DIRECTORY = tmt.utils.resource_files('export/templates')
  File "/home/happz/git/tmt/tmt/utils.py", line 5929, in resource_files
    return Path(importlib.resources.files(package)) / path  # type: ignore[arg-type]
AttributeError: module 'importlib.resources' has no attribute 'files'
fatal: Invalid path '/.fmf': No such file or directory
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/happz/git/tmt/tmt/__init__.py", line 20, in <module>
    from tmt.base import Clean, Plan, Run, Status, Story, Test, Tree
  File "/home/happz/git/tmt/tmt/base.py", line 42, in <module>
    import tmt.checks
  File "/home/happz/git/tmt/tmt/checks/__init__.py", line 6, in <module>
    import tmt.steps.provision
  File "/home/happz/git/tmt/tmt/steps/__init__.py", line 34, in <module>
    import tmt.export
  File "/home/happz/git/tmt/tmt/export/__init__.py", line 42, in <module>
    TEMPLATES_DIRECTORY = tmt.utils.resource_files('export/templates')
  File "/home/happz/git/tmt/tmt/utils.py", line 5929, in resource_files
    return Path(importlib.resources.files(package)) / path  # type: ignore[arg-type]
AttributeError: module 'importlib.resources' has no attribute 'files'
fatal: Invalid path '/.fmf': No such file or directory
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/happz/git/tmt/tmt/__init__.py", line 20, in <module>
    from tmt.base import Clean, Plan, Run, Status, Story, Test, Tree
  File "/home/happz/git/tmt/tmt/base.py", line 42, in <module>
    import tmt.checks
  File "/home/happz/git/tmt/tmt/checks/__init__.py", line 6, in <module>
    import tmt.steps.provision
  File "/home/happz/git/tmt/tmt/steps/__init__.py", line 34, in <module>
    import tmt.export
  File "/home/happz/git/tmt/tmt/export/__init__.py", line 42, in <module>
    TEMPLATES_DIRECTORY = tmt.utils.resource_files('export/templates')
  File "/home/happz/git/tmt/tmt/utils.py", line 5929, in resource_files
    return Path(importlib.resources.files(package)) / path  # type: ignore[arg-type]
AttributeError: module 'importlib.resources' has no attribute 'files'
fatal: Invalid path '/.fmf': No such file or directory
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/happz/git/tmt/tmt/__init__.py", line 20, in <module>
    from tmt.base import Clean, Plan, Run, Status, Story, Test, Tree
  File "/home/happz/git/tmt/tmt/base.py", line 42, in <module>
    import tmt.checks
  File "/home/happz/git/tmt/tmt/checks/__init__.py", line 6, in <module>
    import tmt.steps.provision
  File "/home/happz/git/tmt/tmt/steps/__init__.py", line 34, in <module>
    import tmt.export
  File "/home/happz/git/tmt/tmt/export/__init__.py", line 42, in <module>
    TEMPLATES_DIRECTORY = tmt.utils.resource_files('export/templates')
  File "/home/happz/git/tmt/tmt/utils.py", line 5929, in resource_files
    return Path(importlib.resources.files(package)) / path  # type: ignore[arg-type]
AttributeError: module 'importlib.resources' has no attribute 'files'
fatal: Invalid path '/.fmf': No such file or directory
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/happz/git/tmt/tmt/__init__.py", line 20, in <module>
    from tmt.base import Clean, Plan, Run, Status, Story, Test, Tree
  File "/home/happz/git/tmt/tmt/base.py", line 42, in <module>
    import tmt.checks
  File "/home/happz/git/tmt/tmt/checks/__init__.py", line 6, in <module>
    import tmt.steps.provision
  File "/home/happz/git/tmt/tmt/steps/__init__.py", line 34, in <module>
    import tmt.export
  File "/home/happz/git/tmt/tmt/export/__init__.py", line 42, in <module>
    TEMPLATES_DIRECTORY = tmt.utils.resource_files('export/templates')
  File "/home/happz/git/tmt/tmt/utils.py", line 5929, in resource_files
    return Path(importlib.resources.files(package)) / path  # type: ignore[arg-type]
AttributeError: module 'importlib.resources' has no attribute 'files'
fatal: Invalid path '/.fmf': No such file or directory
happz commented 1 year ago

@psss @martinhoyer FYI, I'll try to enforce Python 3.9+

happz commented 1 year ago

Aha, might be my local system, with python3 being Python 3.8. I suppose forcing Python in pre-commit could break it for others, e.g. enforcing python3.9 when it's not installed on users' system...

martinhoyer commented 1 year ago

@happz This is a pre-commit virtualenv, not hatch. This does not seem like a regression introduced by the packaging changes?

For what you're doing, check out matrix, like it's being used in test env:

 [[tool.hatch.envs.test.matrix]]
 python = ["3.9", "3.11"]

so if you add something like this there..

diff --git a/pyproject.toml b/pyproject.toml
index 18a95e0b..c246ec2f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -139,6 +139,7 @@ description = "Testing environment"
 system-packages = true  # Install python3-nitrate to skip building psycopg2
 dependencies = [
     "pytest",
+    "typing-extensions>=4.4.0; python_version < '3.10'",
     "python-coveralls",
     "requre",
     "yq==3.1.1",
@@ -161,6 +162,7 @@ requre = [
     "pytest -vvv -ra --showlocals",
     "requre-patch purge --replaces :milestone_url:str:SomeText --replaces :latency:float:0 tests/integration/test_data/test_nitrate/*",
     ]
+tmt-lint = "python3 -V; python3 -m tmt lint --failed-only"

(typing-extensions needed by pint on 3.9)

[mhoyer@x1 tmt]$ hatch run test:tmt-lint
──────────────────────────────────────────── test.py3.9 ────────────────────────────────────────────
Python 3.9.18
─────────────────────────────────────────── test.py3.11 ────────────────────────────────────────────
Python 3.11.5

Might be worth bumping the high version to 3.12 for F39 and add both to make develop.

happz commented 1 year ago

Yeah, this is indeed the virtualenv spawned by pre-commit. I "resolved" this by getting rid of Python 3.8, but I wonder whether we can enforce pre-commit to install a minimal Python version. IIUIC, we can't - we can ask it to install a specific Python version, but that will break down once it's missing on one's workstation, that's hardly an improvement...

I'm dropping this from 1.28, I'll let it open for a while, but I suspect no silver bullet to be readily available.

martinhoyer commented 1 year ago

I think so, yeah. Only specific version can be specified, not minimal.

Not sure if using hatch envs in pre-commit is the way. Maybe.

martinhoyer commented 4 months ago

Just realized this is probably was probably due to importlib_resources vs importlib.resources, i.e. we should use importlib_resources on Python 3.9, like in https://github.com/teemtee/tmt/pull/294