Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
366 changes: 358 additions & 8 deletions pkg-py/src/commons/_measures.py

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions pkg-py/tests/measure_sources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Two traps for the next person editing this directory: the top level is
itself a fixture case, so adding any `.py` file here changes the expected
measure list in `test_semantic_layer_reads_a_directory_without_recursing`;
and the collision check scans every sibling file, so adding a top-level file
named after any importable module breaks every path-loading test at once.
6 changes: 6 additions & 0 deletions pkg-py/tests/measure_sources/broken/broken_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Raises at import time, to test that a failed load does not dirty sys.modules.

Lives in a subdirectory so a non-recursive directory scan never reaches it.
"""

raise RuntimeError("boom")
11 changes: 11 additions & 0 deletions pkg-py/tests/measure_sources/broken/self_removing_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Deletes its own sys.modules entry, then raises.

Regression fixture for _load_module_from_path's cleanup: it must not turn
this into a KeyError and swallow the real import error.
"""

import sys

del sys.modules[__name__]

raise RuntimeError("boom")
5 changes: 5 additions & 0 deletions pkg-py/tests/measure_sources/collision_a/shared_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Shares a file name with collision_b/shared_lib.py on purpose."""


def value() -> int:
return 1
12 changes: 12 additions & 0 deletions pkg-py/tests/measure_sources/collision_a/uses_shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Imports shared_lib by its bare name, registering it in sys.modules under
that name for the rest of the process.
"""

from shared_lib import value # type: ignore[missing-import]

from commons._measures import measure


@measure(description="From directory a.")
def a_measure() -> int:
return value()
9 changes: 9 additions & 0 deletions pkg-py/tests/measure_sources/collision_b/shared_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Shares a file name with collision_a/shared_lib.py on purpose.

Loading anything from this directory must fail once collision_a's
shared_lib.py has already been imported under the bare name "shared_lib".
"""


def value() -> int:
return 2
13 changes: 13 additions & 0 deletions pkg-py/tests/measure_sources/duplicate_helpers/a_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""First file, sorted before b_file.py in this directory."""

from commons._measures import measure


def helper() -> int:
"""A helper this file's measure calls."""
return 1


@measure(description="Measure a.")
def measure_a() -> int:
return helper()
17 changes: 17 additions & 0 deletions pkg-py/tests/measure_sources/duplicate_helpers/b_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Second file, sorted after a_file.py; defines a same-named helper.

Proves directory scanning keeps the first file's source for a colliding
helper name.
"""

from commons._measures import measure


def helper() -> int:
"""A colliding helper name; this definition must lose to a_file's."""
return 2


@measure(description="Measure b.")
def measure_b() -> int:
return helper()
12 changes: 12 additions & 0 deletions pkg-py/tests/measure_sources/nested/orders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Shares a file name with the parent directory's orders.py on purpose.

Directory loading must not reach it, and loading it explicitly must not
collide with the other orders.py in sys.modules.
"""

from commons._measures import measure


@measure(description="Count of nested orders.")
def nested_order_count() -> int:
return 1
20 changes: 20 additions & 0 deletions pkg-py/tests/measure_sources/orders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Measures loaded from a path by the test suite."""

from typing import Annotated, Any

from pydantic import Field

from commons._measures import Injected, measure


def double(x: int) -> int:
"""A helper the measure calls. Not a measure itself."""
return x * 2


@measure(description="Count of orders.")
def order_count(
region: Annotated[str, Field(description="The sales region.")],
warehouse: Injected[Any],
) -> int:
return double(1)
17 changes: 17 additions & 0 deletions pkg-py/tests/measure_sources/reentrant/composes_a_sibling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Calls semantic_layer() on another path during its own import.

A non-reentrant lock around the import machinery would deadlock here: this
module's own load already holds _IMPORT_LOCK when the line below tries to
acquire it again on the same thread.
"""

from pathlib import Path

from commons._measures import measure, semantic_layer

NESTED_LAYER = semantic_layer(Path(__file__).parent.parent / "nested" / "orders.py")


@measure(description="Outer measure.")
def outer_measure() -> int:
return 1
8 changes: 8 additions & 0 deletions pkg-py/tests/measure_sources/revenue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""A second file in the same directory, to prove directory loading."""

from commons._measures import measure


@measure(description="Total revenue.")
def total_revenue() -> int:
return 100
6 changes: 6 additions & 0 deletions pkg-py/tests/measure_sources/sibling_imports/helper_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""A helper file a sibling measure file imports directly."""


def double(x: int) -> int:
"""Doubles a value; imported by a sibling file, not a measure itself."""
return x * 2
12 changes: 12 additions & 0 deletions pkg-py/tests/measure_sources/sibling_imports/uses_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Imports a sibling file by plain absolute import, the way an ordinary
Python module does.
"""

from helper_lib import double # type: ignore[missing-import]

from commons._measures import measure


@measure(description="Doubled count.")
def doubled_count() -> int:
return double(21)
10 changes: 10 additions & 0 deletions pkg-py/tests/measure_sources/stdlib_collision/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Named after a standard library module on purpose: loading this must fail
before the directory ever goes on sys.path.
"""

from commons._measures import measure


@measure(description="Should never load.")
def unreachable_measure() -> int:
return 1
Loading
Loading