Skip to content

Commit

Permalink
fix: double-check importing correctly errors if render neither behave…
Browse files Browse the repository at this point in the history
…s as a package nor isolated module
  • Loading branch information
philtweir committed Sep 9, 2024
1 parent 3bff5e5 commit 7ae5a7a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/_lib/unfrender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Testing example renderer.
Correctly fails to import to show a broken module being handled.
"""

from typing import Unpack, TypedDict
from dewret.workflow import Workflow

# This lacking a relative import, while extra itself
# uses one is what breaks the module. It cannot be both
# a package and not-a-package. This is importable by
# adding a . before extra. If instead, you try to avoid
# relative imports altogether, and change .other -> other
# in extra, it will also break, as the directory of this
# file is not in the PATH.
from extra import JUMP # type: ignore

class UnfrenderRendererConfiguration(TypedDict):
allow_complex_types: bool

def default_config() -> UnfrenderRendererConfiguration:
return {
"allow_complex_types": True
}

def render_raw(
workflow: Workflow, **kwargs: Unpack[UnfrenderRendererConfiguration]
) -> dict[str, str]:
return {"JUMP": str(JUMP)}
14 changes: 14 additions & 0 deletions tests/test_render_module.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Check renderers can be imported live."""

import pytest
from pathlib import Path
from dewret.tasks import construct
from dewret.render import get_render_method
Expand Down Expand Up @@ -41,3 +42,16 @@ def test_can_load_render_module() -> None:
It probably got made with JUMP=1.0
""",
}

def test_get_correct_import_error_if_unable_to_load_render_module() -> None:
"""TODO: Docstrings."""
unfrender_py = Path(__file__).parent / "_lib/unfrender.py"
with pytest.raises(ImportError) as exc:
get_render_method(unfrender_py)

entry = exc.traceback[-1]
assert Path(entry.path).resolve() == (
Path(__file__).parent / "_lib" / "extra.py"
).resolve()
assert entry.relline == 2
assert "attempted relative import with no known parent package" in str(exc.value)

0 comments on commit 7ae5a7a

Please sign in to comment.