Skip to content

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

Closed
@NickCrews

Description

@NickCrews
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions