Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: module.__name__ not set set correctly during exec in test collection #374

Closed
3 tasks done
NickCrews opened this issue May 2, 2023 · 2 comments · Fixed by #373
Closed
3 tasks done

BUG: module.__name__ not set set correctly during exec in test collection #374

NickCrews opened this issue May 2, 2023 · 2 comments · Fixed by #373
Labels
bug Something isn't working

Comments

@NickCrews
Copy link
Contributor

NickCrews commented May 2, 2023

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pytask.
  • (optional) I have confirmed this bug exists on the main branch of pytask.

Problem description

First of all, thanks for this library! A lot of great stuff in here.

See #373 for a PR that adds a failing test.

While I'm not sure what the root cause is / who's fault it is, the actual method of failure is something like this:
Say I have a file task_a.py. In

@hookimpl
def pytask_collect_file(
session: Session, path: Path, reports: list[CollectionReport]
) -> list[CollectionReport] | None:
"""Collect a file."""
if any(path.match(pattern) for pattern in session.config["task_files"]):
spec = importlib_util.spec_from_file_location(path.stem, str(path))
if spec is None:
raise ImportError(f"Can't find module {path.stem!r} at location {path}.")
mod = importlib_util.module_from_spec(spec)
spec.loader.exec_module(mod)
, the module spec is created with a module name of path.stem, so in my case it is task_a. But then IFF you have used a from __future__ import annotations, then you go down this code path where dataclasses tries to call sys.modules.get("task_a"), and "task_a" is not importable by itself.

I think we need to be setting the module.name to something else, so it actually is findable within sys.modules, or import/exec it slightly differently?

Expected Output

@NickCrews NickCrews added the bug Something isn't working label May 2, 2023
@tobiasraabe
Copy link
Member

Hi @NickCrews, thanks for the kind words, and thank you for the bug report + test 🙇.

This seems tricky. I hopefully have some time over the weekend to look into this. My current guess is that the import mechanism is not doing enough, and maybe it needs some of the sys.path modification magic pytest is doing. Here for future investigations:

@NickCrews
Copy link
Contributor Author

That looks like it would fix the problem, but I'm not sure which importmode should be used. pytests docs on the subject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants