diff --git a/changelog.d/frame-adopt-replace-tables.added.md b/changelog.d/frame-adopt-replace-tables.added.md new file mode 100644 index 0000000000..a4b43985a8 --- /dev/null +++ b/changelog.d/frame-adopt-replace-tables.added.md @@ -0,0 +1 @@ +Add `Frame.replace_tables(**tables)` and `Frame.with_columns(entity, **columns)`, which build a new frame that shares the memory of unchanged tables and columns with the source frame when pandas copy-on-write is on, instead of deep-copying every table; with copy-on-write off they deep-copy. Both go through the new private `Frame._adopt`, which runs the constructor's full validation. The default `Frame` constructor still deep-copies, and no build code calls the new methods yet, so outputs are unchanged. A new test reads the Python source under `packages/*/src/` and `tools/` and fails on raw writes into array memory that copy-on-write does not protect: `setflags(write=True)`, item assignment through `Series.array`, pandas private attributes such as `._mgr` and `._ndarray`, `np.copyto`, and `out=` or `ufunc.at` into an array the code did not create. Graph nodes whose kernels hash `bundle.py`, including the `calibrate` and `simulate` parity pins, get new cache keys. diff --git a/packages/microcosm-frame/src/microcosm/frame/bundle.py b/packages/microcosm-frame/src/microcosm/frame/bundle.py index e16c3a45a7..c71f9b84c6 100644 --- a/packages/microcosm-frame/src/microcosm/frame/bundle.py +++ b/packages/microcosm-frame/src/microcosm/frame/bundle.py @@ -20,8 +20,14 @@ Frame operations return new frames. Entity-table access follows a read-only caller convention; integrity-critical receipts belong in the frame's private, immutable metadata rather than in mutable table columns. + +The constructor deep-copies every table it is given. :meth:`Frame.replace_tables` +and :meth:`Frame.with_columns` instead build a frame that shares the memory of +every unchanged column with its source frame when pandas copy-on-write is on, +and deep-copy when it is off (see :meth:`Frame._adopt`). """ +import re from collections.abc import Mapping from dataclasses import dataclass from typing import Any @@ -107,17 +113,132 @@ def __init__( mass_log: tuple[MassChangeRecord, ...] = (), metadata: Mapping[str, Any] | None = None, ) -> None: + self._assemble( + tables, + schema, + weights, + strata, + mass_log=mass_log, + metadata=metadata, + copy_data=True, + ) + + @classmethod + def _adopt( + cls, + tables: Mapping[str, pd.DataFrame], + schema: EntitySchema, + weights: Mapping[str, Weights], + strata: pd.Series | None = None, + *, + mass_log: tuple[MassChangeRecord, ...] = (), + metadata: Mapping[str, Any] | None = None, + ) -> "Frame": + """Build a frame that takes ownership of ``tables`` instead of copying them. + + When pandas copy-on-write is on, the frame stores the given DataFrame + objects themselves, so its columns share memory with any other + DataFrame that shares theirs. Copy-on-write then keeps a write made + through pandas to one of those DataFrames from reaching the others. + The caller hands the tables over and must not write to these + DataFrame objects afterwards: a write to the same object changes this + frame. + + Copy-on-write does not protect raw writes into the shared memory, for + example through ``Series.array``, the ``.base`` of a read-only view, + ``.asi8``, private pandas attributes, an array made writable, + ``ufunc.at``, ``np.copyto`` or ``out=`` + (``test_shared_buffer_writes.py`` refuses the direct forms of some of + these in ``packages/*/src/`` and ``tools/``), and through what + ``.values`` and ``to_numpy()`` return for extension-typed columns. + ``.values`` returns the column's own extension array, and a write + into it reaches every frame sharing the column, for example for the + nullable integer, float and boolean types, ``category`` and + python-backed strings, and under pandas 2.x pyarrow-backed types. + ``to_numpy()`` returns a writable view of the column's data for, for + example, the nullable integer, float and boolean types when the + column has no missing value, python-backed strings, and sparse + columns without gaps. + + When copy-on-write is off (the pandas 2.x default), a shallow copy + shares data even for ordinary pandas writes, so the tables and strata + are deep-copied exactly as the constructor copies them. The check is + made when the frame is built: a frame built while copy-on-write was on + relies on it staying on. + + Arguments and validation are the constructor's: every invariant is + checked and the same errors are raised, except that a table that is + not a DataFrame raises ``TypeError`` before any table is stored or + checked (the constructor calls each table's ``copy()`` and checks + what it returns, so it fails on a table without one). Weights are + stored as given (their arrays are read-only), and the strata are + shallow-copied when copy-on-write is on. + + Args: + tables: Entity and link tables, as for the constructor. + schema: The entity structure. + weights: Typed weight vectors keyed by entity name. + strata: Per-person provenance labels, or ``None`` for + :data:`DEFAULT_STRATUM`. + mass_log: Declared mass changes, as for the constructor. + metadata: Stage receipts, as for the constructor. + + Returns: + A new validated frame that owns ``tables``. + + Raises: + TypeError: If a table is not a :class:`pandas.DataFrame`, or as + the constructor would. + ValueError: As the constructor would on the same arguments. + """ + + frame = cls.__new__(cls) + frame._assemble( + tables, + schema, + weights, + strata, + mass_log=mass_log, + metadata=metadata, + copy_data=not _pandas_copy_on_write(), + require_dataframes=True, + ) + return frame + + def _assemble( + self, + tables: Mapping[str, pd.DataFrame], + schema: EntitySchema, + weights: Mapping[str, Weights], + strata: pd.Series | None, + *, + mass_log: tuple[MassChangeRecord, ...], + metadata: Mapping[str, Any] | None, + copy_data: bool, + require_dataframes: bool = False, + ) -> None: + """Store a frame's parts and check every constructor invariant. + + With ``copy_data``, each table and the strata are deep-copied (the + constructor); without it, the tables are stored as given and the + strata shallow-copied (:meth:`_adopt` under copy-on-write). With + ``require_dataframes``, a table that is not a DataFrame raises + ``TypeError`` (:meth:`_adopt`). + """ + self._schema = schema self._mass_log = tuple(mass_log) self._metadata = _freeze_metadata(metadata) declared_links = {link.name for link in schema.links} self._tables: dict[str, pd.DataFrame] = {} self._link_tables: dict[str, pd.DataFrame] = {} + if require_dataframes: + _require_dataframes(tables) for name, frame in tables.items(): target = self._link_tables if name in declared_links else self._tables - target[name] = frame.copy() + target[name] = frame.copy() if copy_data else frame self._weights = dict(weights) - self._strata = self._validated_strata(strata) + self._strata = self._validated_strata(strata, deep=copy_data) self._validate_tables() self._validate_linkage() self._validate_links() @@ -357,7 +478,9 @@ def _validate_reserved_weight_columns(self) -> None: "column; drop this column before constructing the frame." ) - def _validated_strata(self, strata: pd.Series | None) -> pd.Series: + def _validated_strata( + self, strata: pd.Series | None, *, deep: bool = True + ) -> pd.Series: person = self._tables.get(self._schema.person_entity) if person is None: raise ValueError( @@ -379,7 +502,7 @@ def _validated_strata(self, strata: pd.Series | None) -> pd.Series: ) if strata.isna().any(): raise ValueError("strata contains missing labels.") - out = strata.copy() + out = strata.copy(deep=deep) out.name = "stratum" return out @@ -803,6 +926,86 @@ def _mass_change_record( reason=change.reason, ) + def replace_tables(self, /, **changed_tables: pd.DataFrame) -> "Frame": + """Return a new frame with some entity or link tables replaced or added. + + The new frame holds a shallow copy of each replacement and of every + other table, so when pandas copy-on-write is on it shares their memory + with the replacements and with this frame, and a write made through + pandas to either side never reaches the other. Each frame still holds + its own DataFrame objects, so the caller may go on writing to the + DataFrames it passed. Raw writes into the shared memory are not + protected (see :meth:`_adopt`). Weights, strata, the mass log and + metadata are carried unchanged. When copy-on-write is off, every + table is deep-copied instead. + + Args: + **changed_tables: Replacement tables, or declared link tables to + add, keyed by entity or link name. + + Returns: + A new validated frame. + + Raises: + TypeError: If a replacement is not a :class:`pandas.DataFrame`, or + as construction would. + ValueError: If a name is neither a declared entity nor a declared + link, or the new tables break any constructor invariant (for + example, the weights or strata no longer align with them). + """ + + _require_dataframes(changed_tables) + tables = { + name: table.copy(deep=False) + for name, table in {**self._tables, **self._link_tables}.items() + } + tables.update( + {name: table.copy(deep=False) for name, table in changed_tables.items()} + ) + return Frame._adopt( + tables, + self._schema, + self._weights, + self._strata, + mass_log=self._mass_log, + metadata=self._metadata, + ) + + def with_columns(self, entity: str, /, **columns: Any) -> "Frame": + """Return a new frame with columns set on one entity's table. + + Each value is assigned with ``table[name] = values``, so a new name + adds a column and an existing name replaces one, under pandas' + assignment rules: a :class:`pandas.Series` aligns on the table's + index, and an array or list must have one value per row. pandas + copies a NumPy array on assignment, so the frame does not alias the + caller's array. The table's other columns and every other table are + shared with this frame as in :meth:`replace_tables`, with the same + limits. + + Args: + entity: An entity declared by the schema (not a link). + **columns: Column values keyed by column name. + + Returns: + A new validated frame. + + Raises: + TypeError, ValueError: As pandas raises them when it cannot + assign a value as a column (for example a set, or a value of + the wrong length), or as construction would. + ValueError: If ``entity`` is not declared by the schema, or the + new columns break a constructor invariant (for example a + name another table already carries, a reserved + ``{entity}_weight`` name, a duplicated or missing id, or + broken linkage). + """ + + table = self.table(entity).copy(deep=False) + for column_name, values in columns.items(): + table[column_name] = values + return self.replace_tables(**{entity: table}) + def broadcast(self, column: str, to: str = "person") -> pd.Series: """Map a group-table column onto persons via membership. @@ -1343,6 +1546,38 @@ def __repr__(self) -> str: return f"Frame({sizes}; weights[{weighted}]; strata={n_strata}{links})" +def _require_dataframes(tables: Mapping[str, Any]) -> None: + for name, table in tables.items(): + if not isinstance(table, pd.DataFrame): + raise TypeError( + f"Table {name!r} must be a pandas DataFrame, got " + f"{type(table).__name__}." + ) + + +def _pandas_copy_on_write() -> bool: + """Whether pandas copy-on-write is on in this process, checked at each call. + + pandas 2.x answers this itself with ``pandas._config.using_copy_on_write()``: + ``mode.copy_on_write`` must be ``True`` (it is ``False`` by default, or + ``"warn"``) and the data manager the default block manager. Reading + that function emits none of the deprecation warnings the options do. + pandas 3 removed that function: copy-on-write is always on there, and + its option is deprecated and ignored. Without the function, a version + from 3 on counts as on, and any other version reads + ``mode.copy_on_write``. + """ + + try: + from pandas._config import using_copy_on_write + except ImportError: + major_version = re.match(r"\d+", pd.__version__) + if major_version is not None and int(major_version.group()) >= 3: + return True + return pd.get_option("mode.copy_on_write") is True + return bool(using_copy_on_write()) + + @dataclass(frozen=True) class _FrozenMapping(Mapping[str, Any]): """Small pickle-safe immutable mapping used for frame metadata.""" diff --git a/packages/microcosm-frame/tests/engine_free/shared/test_frame_table_sharing.py b/packages/microcosm-frame/tests/engine_free/shared/test_frame_table_sharing.py new file mode 100644 index 0000000000..cb04eddb95 --- /dev/null +++ b/packages/microcosm-frame/tests/engine_free/shared/test_frame_table_sharing.py @@ -0,0 +1,633 @@ +"""Frames that share unchanged tables: ``_adopt``, ``replace_tables``, ``with_columns``. + +The default :class:`Frame` constructor deep-copies every table it is given. +``Frame._adopt`` instead stores the tables it is given without copying them +when pandas copy-on-write is on, so a frame built by ``replace_tables`` or +``with_columns`` shares the memory of every unchanged column with its source +frame. Copy-on-write keeps a write made through pandas on one frame from +reaching the other. When copy-on-write is off (the pandas 2.x default), a +shallow copy shares data even for ordinary pandas writes, so ``_adopt`` +deep-copies instead. Tests take that path through the ``copy_on_write_off`` +fixture, which turns copy-on-write off under pandas 2.x and, under pandas 3, +where it is always on, replaces the check ``_adopt`` makes, or by replacing +the check themselves (the parametrized ``cow_off`` cases). Every other test +runs with copy-on-write on. +""" + +import importlib +from collections.abc import Callable, Iterator +from typing import Any + +import numpy as np +import pandas as pd +import pytest + +from microcosm.frame import ( + EntitySchema, + Frame, + LinkSpec, + MassChange, + WeightKind, + Weights, +) +from microcosm.frame import bundle as bundle_module + +pandas_config = importlib.import_module("pandas._config") + +_SCHEMA = EntitySchema( + group_entities=("household", "firm"), + links=(LinkSpec(name="jobs", left_entity="person", right_entity="firm"),), +) + +_SOURCE_INCOME = [50_000.0, 0.0, 30_000.0, 20_000.0] +_SOURCE_RENT = [900.0, 1_200.0] + + +def _source_tables() -> dict[str, pd.DataFrame]: + """Four persons in two households and two firms, plus a jobs link table.""" + + return { + "person": pd.DataFrame( + { + "person_id": np.array([0, 1, 2, 3], dtype="int64"), + "person_household_id": np.array([1, 1, 2, 2], dtype="int64"), + "person_firm_id": np.array([10, 10, 20, 20], dtype="int64"), + "age": np.array([40, 8, 30, 28], dtype="int64"), + "income": np.array(_SOURCE_INCOME), + } + ), + "household": pd.DataFrame( + { + "household_id": np.array([1, 2], dtype="int64"), + "rent": np.array(_SOURCE_RENT), + "state": ["CA", "NY"], + } + ), + "firm": pd.DataFrame( + { + "firm_id": np.array([10, 20], dtype="int64"), + "payroll": np.array([1.0e6, 2.0e6]), + } + ), + "jobs": pd.DataFrame( + { + "person_id": np.array([0, 2, 3], dtype="int64"), + "firm_id": np.array([10, 20, 20], dtype="int64"), + } + ), + } + + +def _source_weights() -> dict[str, Weights]: + return { + "household": Weights(values=np.array([100.0, 200.0]), kind=WeightKind.DESIGN) + } + + +def _source_strata() -> pd.Series: + # Object dtype, as the frame's default strata: ``to_numpy()`` then returns + # a view of the stored array, so memory sharing can be checked. + return pd.Series(["cps", "cps", "puf", "puf"], index=pd.RangeIndex(4), dtype=object) + + +@pytest.fixture +def source_frame() -> Frame: + """A frame with a link table, strata, metadata and one mass-log entry.""" + + frame = Frame( + _source_tables(), + _SCHEMA, + _source_weights(), + _source_strata(), + metadata={"stage": "source"}, + ) + return frame.with_weights( + "household", + Weights(values=np.array([150.0, 300.0]), kind=WeightKind.CALIBRATED), + mass=MassChange(factor=1.5, reason="test calibration"), + ) + + +#: pandas 3 removed this function; its copy-on-write option is deprecated +#: there and has no effect. +_PANDAS_3 = not hasattr(pandas_config, "using_copy_on_write") + + +@pytest.fixture(autouse=True) +def copy_on_write_on() -> Iterator[None]: + """Turn copy-on-write on under pandas 2.x; pandas 3 always has it on.""" + + if _PANDAS_3: + yield + return + with pd.option_context("mode.copy_on_write", True): + yield + + +@pytest.fixture +def copy_on_write_off(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: + """Turn copy-on-write off: under pandas 2.x through its option, and under + pandas 3, where it cannot be off, by replacing the check ``_adopt`` makes.""" + + if _PANDAS_3: + monkeypatch.setattr(bundle_module, "_pandas_copy_on_write", lambda: False) + yield + return + with pd.option_context("mode.copy_on_write", False): + assert not bundle_module._pandas_copy_on_write() + yield + + +def _tables_of(frame: Frame) -> dict[str, pd.DataFrame]: + """Every entity table and link table the frame stores, by name.""" + + tables = {entity: frame.table(entity) for entity in frame.entities} + tables.update({name: frame.link(name) for name in frame.links}) + return tables + + +def _numeric_columns_sharing_memory( + left: dict[str, pd.DataFrame], right: dict[str, pd.DataFrame] +) -> dict[tuple[str, str], bool]: + """For each numeric column both table sets carry, whether its data is shared.""" + + sharing: dict[tuple[str, str], bool] = {} + for table_name, left_table in left.items(): + right_table = right.get(table_name) + if right_table is None: + continue + for column in left_table.select_dtypes("number").columns: + if column not in right_table.columns: + continue + sharing[(table_name, column)] = np.shares_memory( + left_table[column].to_numpy(), right_table[column].to_numpy() + ) + return sharing + + +class TestDefaultConstructor: + def test_constructor_deep_copies_every_table_and_the_strata(self) -> None: + tables = _source_tables() + strata = _source_strata() + frame = Frame(tables, _SCHEMA, _source_weights(), strata) + + for name, table in _tables_of(frame).items(): + assert table is not tables[name] + sharing = _numeric_columns_sharing_memory(_tables_of(frame), tables) + assert sharing, "the comparison found no numeric columns" + assert not any(sharing.values()), sharing + assert not np.shares_memory(frame.strata.to_numpy(), strata.to_numpy()) + + +class TestAdopt: + def test_adopt_stores_the_given_tables_without_copying(self) -> None: + tables = _source_tables() + strata = _source_strata() + frame = Frame._adopt(tables, _SCHEMA, _source_weights(), strata) + + for name, table in _tables_of(frame).items(): + assert table is tables[name] + assert frame.links == ("jobs",) + assert frame.strata is not strata + assert np.shares_memory(frame.strata.to_numpy(), strata.to_numpy()) + + @pytest.mark.parametrize("copy_on_write", [True, False], ids=["cow_on", "cow_off"]) + def test_adopt_refuses_a_table_that_is_not_a_dataframe( + self, monkeypatch: pytest.MonkeyPatch, copy_on_write: bool + ) -> None: + monkeypatch.setattr( + bundle_module, "_pandas_copy_on_write", lambda: copy_on_write + ) + tables: dict[str, Any] = {**_source_tables(), "jobs": None} + + with pytest.raises(TypeError, match="'jobs' must be a pandas DataFrame"): + Frame._adopt(tables, _SCHEMA, _source_weights()) + + def test_adopt_deep_copies_when_copy_on_write_is_off( + self, copy_on_write_off: None + ) -> None: + tables = _source_tables() + strata = _source_strata() + frame = Frame._adopt(tables, _SCHEMA, _source_weights(), strata) + + for name, table in _tables_of(frame).items(): + assert table is not tables[name] + sharing = _numeric_columns_sharing_memory(_tables_of(frame), tables) + assert sharing, "the comparison found no numeric columns" + assert not any(sharing.values()), sharing + assert not np.shares_memory(frame.strata.to_numpy(), strata.to_numpy()) + + def test_adopt_names_the_strata_without_renaming_the_callers_series( + self, + ) -> None: + strata = _source_strata().rename("provenance") + frame = Frame._adopt(_source_tables(), _SCHEMA, _source_weights(), strata) + + assert frame.strata.name == "stratum" + assert strata.name == "provenance" + assert frame.strata.tolist() == ["cps", "cps", "puf", "puf"] + + def test_adopt_carries_the_mass_log_and_metadata(self, source_frame) -> None: + frame = Frame._adopt( + _source_tables(), + _SCHEMA, + _source_weights(), + mass_log=source_frame.mass_log, + metadata={"stage": "adopted", "inputs": ["cps", "puf"]}, + ) + + assert frame.mass_log == source_frame.mass_log + assert dict(frame.metadata) == {"stage": "adopted", "inputs": ("cps", "puf")} + + +def _drop_table(name: str) -> Callable[[dict[str, Any]], None]: + def change(arguments: dict[str, Any]) -> None: + del arguments["tables"][name] + + return change + + +def _set_table(name: str, make: Callable[[pd.DataFrame], Any]) -> Callable: + def change(arguments: dict[str, Any]) -> None: + tables = arguments["tables"] + tables[name] = make(tables.get(name)) + + return change + + +def _set_cell(table: str, row: int, column: str, value: Any) -> Callable: + def change(arguments: dict[str, Any]) -> None: + arguments["tables"][table].loc[row, column] = value + + return change + + +def _set_argument(name: str, value: Any) -> Callable[[dict[str, Any]], None]: + def change(arguments: dict[str, Any]) -> None: + arguments[name] = value() if callable(value) else value + + return change + + +#: Each case breaks one constructor invariant; the ids name the invariant. +_INVALID_ARGUMENTS = { + "missing_entity_table": _drop_table("firm"), + "unknown_entity_table": _set_table( + "spaceship", lambda _table: pd.DataFrame({"spaceship_id": [1]}) + ), + "missing_person_id_column": _set_table( + "person", lambda table: table.drop(columns=["person_id"]) + ), + "duplicate_person_id": _set_cell("person", 1, "person_id", 0), + "missing_membership_column": _set_table( + "person", lambda table: table.drop(columns=["person_firm_id"]) + ), + "unsorted_group_ids": _set_table( + "household", lambda table: table.iloc[::-1].reset_index(drop=True) + ), + "orphaned_group_id": _set_table( + "firm", + lambda _table: pd.DataFrame( + {"firm_id": [10, 20, 30], "payroll": [1.0, 2.0, 3.0]} + ), + ), + "global_column_collision": _set_table( + "firm", lambda table: table.assign(rent=[1.0, 2.0]) + ), + "link_table_missing_id_column": _set_table( + "jobs", lambda table: table.drop(columns=["firm_id"]) + ), + "link_table_dangling_id": _set_cell("jobs", 0, "firm_id", 99), + "reserved_weight_column": _set_table( + "person", lambda table: table.assign(person_weight=1.0) + ), + "no_weights": _set_argument("weights", {}), + "weights_length_mismatch": _set_argument( + "weights", + { + "household": Weights( + values=np.array([1.0, 2.0, 3.0]), kind=WeightKind.DESIGN + ) + }, + ), + "weights_not_typed": _set_argument("weights", {"household": np.array([1.0, 2.0])}), + "weights_for_unknown_entity": _set_argument( + "weights", + { + **_source_weights(), + "galaxy": Weights(values=np.array([1.0]), kind=WeightKind.DESIGN), + }, + ), + "strata_misaligned": _set_argument( + "strata", lambda: pd.Series(["cps"] * 4, index=[5, 6, 7, 8]) + ), + "strata_not_a_series": _set_argument("strata", ["cps"] * 4), + "strata_missing_label": _set_argument( + "strata", lambda: pd.Series(["cps", None, "puf", "puf"]) + ), + "metadata_not_a_mapping": _set_argument("metadata", ["stage"]), + "metadata_unsupported_value": _set_argument("metadata", {"stage": object()}), +} + + +def _constructor_arguments(change: Callable[[dict[str, Any]], None]) -> dict: + arguments: dict[str, Any] = { + "tables": _source_tables(), + "schema": _SCHEMA, + "weights": _source_weights(), + "strata": _source_strata(), + "metadata": None, + } + change(arguments) + return arguments + + +@pytest.mark.parametrize("copy_on_write", [True, False], ids=["cow_on", "cow_off"]) +@pytest.mark.parametrize( + "change", list(_INVALID_ARGUMENTS.values()), ids=list(_INVALID_ARGUMENTS) +) +def test_adopt_raises_the_constructors_validation_errors( + monkeypatch: pytest.MonkeyPatch, change, copy_on_write: bool +) -> None: + monkeypatch.setattr(bundle_module, "_pandas_copy_on_write", lambda: copy_on_write) + with pytest.raises((TypeError, ValueError)) as constructor_error: + Frame(**_constructor_arguments(change)) + with pytest.raises(type(constructor_error.value)) as adopt_error: + Frame._adopt(**_constructor_arguments(change)) + + assert str(adopt_error.value) == str(constructor_error.value) + + +class TestReplaceTables: + def test_unchanged_tables_share_memory_with_the_source(self, source_frame) -> None: + # A shallow copy with one column replaced keeps sharing the others. + household = source_frame.table("household").copy(deep=False) + household["rent"] = [1_000.0, 1_300.0] + replaced = source_frame.replace_tables(household=household) + + assert replaced.table("household")["rent"].tolist() == [1_000.0, 1_300.0] + assert source_frame.table("household")["rent"].tolist() == _SOURCE_RENT + sharing = _numeric_columns_sharing_memory( + _tables_of(replaced), _tables_of(source_frame) + ) + unchanged = { + key: shared for key, shared in sharing.items() if key[0] != "household" + } + assert unchanged and all(unchanged.values()), unchanged + assert not sharing[("household", "rent")] + assert sharing[("household", "household_id")] + + def test_every_table_is_a_distinct_object(self, source_frame) -> None: + household = source_frame.table("household").copy() + replaced = source_frame.replace_tables(household=household) + + source_tables = _tables_of(source_frame) + for name, table in _tables_of(replaced).items(): + assert table is not source_tables[name] + assert replaced.table("household") is not household + assert np.shares_memory( + replaced.table("household")["rent"].to_numpy(), + household["rent"].to_numpy(), + ) + + def test_weights_strata_mass_log_and_metadata_are_carried( + self, source_frame + ) -> None: + replaced = source_frame.replace_tables( + firm=source_frame.table("firm").assign(payroll=[5.0, 6.0]) + ) + + assert type(replaced) is Frame + assert replaced.weights_for("household") is source_frame.weights_for( + "household" + ) + assert replaced.strata.equals(source_frame.strata) + assert replaced.mass_log == source_frame.mass_log + assert len(replaced.mass_log) == 1 + assert replaced.metadata == source_frame.metadata + assert replaced.links == ("jobs",) + + def test_a_link_table_can_be_replaced(self, source_frame) -> None: + jobs = pd.DataFrame({"person_id": [1, 3], "firm_id": [10, 20]}) + replaced = source_frame.replace_tables(jobs=jobs) + + assert replaced.link("jobs")["person_id"].tolist() == [1, 3] + assert source_frame.link("jobs")["person_id"].tolist() == [0, 2, 3] + + def test_a_later_write_to_the_passed_table_does_not_reach_the_frame( + self, source_frame + ) -> None: + household = source_frame.table("household").copy() + replaced = source_frame.replace_tables(household=household) + + household.loc[0, "rent"] = -1.0 + + assert replaced.table("household")["rent"].tolist() == _SOURCE_RENT + + def test_replace_tables_deep_copies_when_copy_on_write_is_off( + self, source_frame, copy_on_write_off: None + ) -> None: + household = source_frame.table("household").copy() + replaced = source_frame.replace_tables(household=household) + + against_source = _numeric_columns_sharing_memory( + _tables_of(replaced), _tables_of(source_frame) + ) + against_passed = _numeric_columns_sharing_memory( + _tables_of(replaced), {"household": household} + ) + assert against_source and not any(against_source.values()), against_source + assert against_passed and not any(against_passed.values()), against_passed + + def test_the_result_is_fully_validated(self, source_frame) -> None: + reversed_households = ( + source_frame.table("household").iloc[::-1].reset_index(drop=True) + ) + with pytest.raises(ValueError, match="sorted ascending"): + source_frame.replace_tables(household=reversed_households) + with pytest.raises( + ValueError, match=r"Unknown entity table\(s\): \['spaceship'\]" + ): + source_frame.replace_tables(spaceship=pd.DataFrame({"spaceship_id": [1]})) + + def test_a_table_that_is_not_a_dataframe_is_refused(self, source_frame) -> None: + with pytest.raises(TypeError, match="'household'.*DataFrame.*dict"): + source_frame.replace_tables(household={"household_id": [1, 2]}) + + +class TestWithColumns: + def test_columns_are_replaced_and_added(self, source_frame) -> None: + income = np.array([1.0, 2.0, 3.0, 4.0]) + updated = source_frame.with_columns("person", income=income, bonus=np.zeros(4)) + + assert updated.person["income"].tolist() == [1.0, 2.0, 3.0, 4.0] + assert updated.person["bonus"].tolist() == [0.0] * 4 + assert source_frame.person["income"].tolist() == _SOURCE_INCOME + assert "bonus" not in source_frame.person.columns + assert not np.shares_memory(updated.person["income"].to_numpy(), income) + + def test_unchanged_columns_share_memory_with_the_source(self, source_frame) -> None: + updated = source_frame.with_columns("person", income=np.ones(4)) + + sharing = _numeric_columns_sharing_memory( + _tables_of(updated), _tables_of(source_frame) + ) + unchanged = { + key: shared + for key, shared in sharing.items() + if key != ("person", "income") + } + assert unchanged and all(unchanged.values()), unchanged + assert not sharing[("person", "income")] + assert updated.person is not source_frame.person + + def test_weights_strata_mass_log_and_metadata_are_carried( + self, source_frame + ) -> None: + updated = source_frame.with_columns("household", rent=[1.0, 2.0]) + + assert updated.weights_for("household") is source_frame.weights_for("household") + assert updated.strata.equals(source_frame.strata) + assert updated.mass_log == source_frame.mass_log + assert updated.metadata == source_frame.metadata + assert updated.links == ("jobs",) + + def test_a_column_may_be_named_entity(self, source_frame) -> None: + updated = source_frame.with_columns("person", entity=np.arange(4)) + + assert updated.person["entity"].tolist() == [0, 1, 2, 3] + + def test_an_unknown_entity_is_named(self, source_frame) -> None: + with pytest.raises(ValueError, match="Unknown entity 'spaceship'"): + source_frame.with_columns("spaceship", fuel=[1.0]) + with pytest.raises(ValueError, match="Unknown entity 'jobs'"): + source_frame.with_columns("jobs", hours=[1.0, 2.0, 3.0]) + + def test_the_result_is_fully_validated(self, source_frame) -> None: + with pytest.raises(ValueError, match="globally unique"): + source_frame.with_columns("firm", rent=[1.0, 2.0]) + with pytest.raises(ValueError, match="sorted ascending"): + source_frame.with_columns("household", household_id=[2, 1]) + with pytest.raises(ValueError, match="reserved weight-column"): + source_frame.with_columns("person", person_weight=np.ones(4)) + + def test_with_columns_deep_copies_when_copy_on_write_is_off( + self, source_frame, copy_on_write_off: None + ) -> None: + updated = source_frame.with_columns("person", bonus=np.zeros(4)) + + sharing = _numeric_columns_sharing_memory( + _tables_of(updated), _tables_of(source_frame) + ) + assert sharing and not any(sharing.values()), sharing + + +def _frames_built_from(source_frame: Frame) -> dict[str, Frame]: + return { + "with_columns": source_frame.with_columns("person", bonus=np.zeros(4)), + "replace_tables": source_frame.replace_tables( + firm=source_frame.table("firm").assign(payroll=[5.0, 6.0]) + ), + } + + +@pytest.mark.parametrize("method", ["with_columns", "replace_tables"]) +def test_a_pandas_write_to_the_new_frame_leaves_the_source_unchanged( + source_frame, method: str +) -> None: + derived = _frames_built_from(source_frame)[method] + assert np.shares_memory( + derived.person["income"].to_numpy(), source_frame.person["income"].to_numpy() + ) + + derived.person.loc[0, "income"] = -1.0 + derived.table("household").loc[1, "rent"] = -1.0 + derived.link("jobs").loc[0, "firm_id"] = 20 + + assert derived.person["income"].tolist()[0] == -1.0 + assert source_frame.person["income"].tolist() == _SOURCE_INCOME + assert source_frame.table("household")["rent"].tolist() == _SOURCE_RENT + assert source_frame.link("jobs")["firm_id"].tolist() == [10, 20, 20] + source_frame.revalidate() + + +@pytest.mark.parametrize("method", ["with_columns", "replace_tables"]) +def test_a_pandas_write_to_the_source_leaves_the_new_frame_unchanged( + source_frame, method: str +) -> None: + derived = _frames_built_from(source_frame)[method] + + source_frame.person.loc[0, "income"] = -1.0 + source_frame.table("household").loc[1, "rent"] = -1.0 + + assert derived.person["income"].tolist() == _SOURCE_INCOME + assert derived.table("household")["rent"].tolist() == _SOURCE_RENT + + +class TestCopyOnWriteCheck: + """``_pandas_copy_on_write`` on each pandas it can run under. + + pandas 2.x provides ``pandas._config.using_copy_on_write``; pandas 3 + removed it. The tests replace or remove that function, and replace the + version and the option reader, to stand for each case. + """ + + @staticmethod + def _refuse_option_reads(name: str) -> object: + raise AssertionError(f"the check must not read option {name!r}") + + def test_pandas_3_has_copy_on_write_without_reading_the_option( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + monkeypatch.delattr(pandas_config, "using_copy_on_write", raising=False) + monkeypatch.setattr(bundle_module.pd, "__version__", "3.0.3") + monkeypatch.setattr(bundle_module.pd, "get_option", self._refuse_option_reads) + + assert bundle_module._pandas_copy_on_write() is True + + @pytest.mark.parametrize("in_force", [True, False], ids=["on", "off"]) + def test_pandas_2_uses_pandas_own_check( + self, monkeypatch: pytest.MonkeyPatch, in_force: bool + ) -> None: + monkeypatch.setattr( + pandas_config, "using_copy_on_write", lambda: in_force, raising=False + ) + monkeypatch.setattr(bundle_module.pd, "__version__", "2.3.3") + monkeypatch.setattr(bundle_module.pd, "get_option", self._refuse_option_reads) + + assert bundle_module._pandas_copy_on_write() is in_force + + @pytest.mark.parametrize( + ("version", "option_value", "expected"), + [ + ("2.3.3", True, True), + ("2.3.3", False, False), + ("2.3.3", "warn", False), + ("0+unknown", True, True), + ("0+unknown", False, False), + ("unknown", True, True), + ("", False, False), + ], + ids=[ + "enabled", + "disabled", + "warn_mode", + "undotted_on", + "undotted_off", + "no_leading_number", + "empty", + ], + ) + def test_without_pandas_own_check_the_version_or_the_option_decides( + self, + monkeypatch: pytest.MonkeyPatch, + version: str, + option_value: object, + expected: bool, + ) -> None: + options = {"mode.copy_on_write": option_value} + monkeypatch.delattr(pandas_config, "using_copy_on_write", raising=False) + monkeypatch.setattr(bundle_module.pd, "__version__", version) + monkeypatch.setattr(bundle_module.pd, "get_option", options.__getitem__) + + assert bundle_module._pandas_copy_on_write() is expected diff --git a/packages/microcosm-frame/tests/engine_free/shared/test_shared_buffer_writes.py b/packages/microcosm-frame/tests/engine_free/shared/test_shared_buffer_writes.py new file mode 100644 index 0000000000..e2841e6933 --- /dev/null +++ b/packages/microcosm-frame/tests/engine_free/shared/test_shared_buffer_writes.py @@ -0,0 +1,1445 @@ +"""A syntactic check for raw writes that pandas copy-on-write does not protect. + +:meth:`Frame.replace_tables` and :meth:`Frame.with_columns` build frames that +share the memory of every unchanged column with their source frame +(:meth:`Frame._adopt`). pandas copy-on-write keeps a write made through pandas +(``.loc``, column assignment, in-place methods) on one frame from reaching +the other, but it does not see a raw write into the shared memory. Checked +with pandas 3.0.3 and NumPy 2.4.6, each form below writes into the shared +memory without an error, so this test reads the scanned source with +:mod:`ast` and refuses it. + +This is a check on how code is written, not an analysis of where arrays come +from. It looks at one expression at a time and, for the last rule only, +follows a name to its own assignments in the same function. Each rule's name +is what a failure reports. + +``makes-array-writable`` + ``x.setflags(write=...)`` with anything but a false constant (a + positional first argument counts too), and assigning anything but a + false constant to ``x.flags.writeable``, ``x.flags["W"]`` or + ``x.flags["WRITEABLE"]``. The arrays ``to_numpy()`` and ``.values`` + return for NumPy-typed columns are read-only views of the shared memory; + this makes them writable. +``writes-through-series-array`` + Item assignment, plain or augmented (``x.array[i] = v``, + ``x.array[mask] += v``), into ``x.array``, and ``x.array.__setitem__(...)``, + where ``x`` is not ``self`` or ``cls``. ``Series.array`` is the column's + own array. +``pandas-private-attribute`` + Any use of ``._ndarray`` or ``._mgr``, and of ``._values``, ``._data``, + ``._mask`` or ``._codes`` on anything but ``self`` or ``cls`` (workspace + classes use those names for their own state). These reach pandas' + internal arrays. +``numpy-copyto`` + Any use of ``copyto`` as an attribute (``np.copyto``) or imported from + NumPy. +``writes-into-existing-array`` + ``out=`` given by keyword to a NumPy function or ufunc (named through + ``import numpy as np`` or ``from numpy import ...``) or to an ndarray + method that takes it (``_METHODS_WITH_OUT``), and the first argument of + ``np..at``, which writes even into a read-only array, unless that + array is created in the function. Created means: + + - a call, not itself given ``out=``, of a NumPy function that allocates + (``_ALLOCATING_NUMPY_FUNCTIONS``: ``np.zeros``, ``np.empty_like``, + ``np.where``, ...), of a ufunc, of ``np.array`` without ``copy=`` or + with ``copy=True``, of ``.to_numpy(copy=True)``, or of a random + ``Generator`` draw (``_RANDOM_DRAW_METHODS``: ``rng.random(n)``, ...); + - arithmetic or a comparison (``a - b``, ``a > 0``); + - ``.copy()`` with no arguments (except of ``.index`` or ``.columns``, or + a subscript of them: ``Index.copy()`` is shallow, and a slice of an + index shares its memory), or ``+``, ``*`` or ``|`` (which also + join lists and dicts), or a slice of these, but not an item of them, + because a copy or join of a dict or list shares its items; + - a subscript of the others; + - a local name, or a subscript of one, whose every assignment in the + function is one of these or ``None``, with at least one that is not + ``None``. A parameter, a name declared ``global`` or ``nonlocal``, and + a name bound any other way in the function (a loop, comprehension or + ``with`` target, unpacking, an import) are not created. + + ``.astype()`` does not count as created: a pandas object's conversion + to its own type shares its memory. + +``setflags``, ``Series.array[i] = value``, ``._ndarray``, ``._mgr``, +``np.copyto`` and ``out=`` are the writes first identified as unprotected. +``ufunc.at`` and the other private attributes were added after probes showed +that they write into shared memory too. + +Scope: every Python file under ``packages/*/src/`` (the source of every +workspace package, including microcosm-frame and the microcosm-build code that +builds US and UK frames) and under ``tools/`` (the release and build tools), +except files in ``node_modules`` or hidden directories. Tests and +``test_support/`` are not scanned. + +What this check does not see, so does not refuse: + +- arrays reached in any other way: through another name + (``values = x.array; values[0] = 1``), ``np.asarray(x.array)``, + ``x.array.to_numpy()``, ``.base`` or ``.asi8``, containers, closures, and + other functions' arguments and return values; +- positional ``out`` arguments, SciPy ufuncs, and the other functions and + methods that write in place (``np.put``, ``np.putmask``, + ``np.fill_diagonal``, ``np.random.shuffle``, ``arr.sort()``, ``arr.fill()``); +- what ``.values`` and ``to_numpy()`` return for extension-typed columns: + ``.values`` returns the column's own extension array (for example for the + nullable integer, float and boolean types, ``category`` and python-backed + strings), and ``to_numpy()`` a writable view of the column's data (for + example for the nullable types without missing values, python-backed + strings, and sparse columns without gaps); +- pandas objects built with ``copy=False`` over another object's array, and + ``np.asarray(index)``, which is writable although the index is shared even + between deep copies. + +Catching these needs a check at run time, such as hashing each shared column +when a frame is built and again before its outputs are written. + +A finding that cannot write into memory a pandas object shares goes in +``_REVIEWED_FINDINGS`` with the reason. +""" + +from __future__ import annotations + +import ast +import bisect +import hashlib +import re +from collections import defaultdict +from collections.abc import Iterable, Iterator +from dataclasses import dataclass +from pathlib import Path + +import numpy as np +import pytest + +from test_support.paths import paths_for + +_REPOSITORY = paths_for("microcosm-frame").repository + +#: Reviewed findings that cannot write into memory a pandas object shares, +#: keyed by (repository-relative path, enclosing function, rule, the +#: expression's source, a digest of the enclosing function's source) as a +#: failure prints it, with the reason. The function is ``Class.method`` or +#: ``outer.inner``, or ```` for module-level code. Any change to the +#: enclosing function changes the digest and needs a new review; a reason +#: that rests on other functions names them, and changing them needs one too. +_REVIEWED_FINDINGS: dict[tuple[str, str, str, str, str], str] = { + ( + "tools/verify_uk_identity_stability.py", + "_pre_split_household_weights", + "writes-into-existing-array", + "np.add.at(weights, lineage.root_positions, weights[lineage.copy_positions])", + "6cc4521d9bc0", + ): ( + "np.add.at writes into the array _pre_clone_household_weights returns, " + "which _pre_residential_household_weights creates with " + "np.asarray(...).copy() from the household weights. Review again if " + "either of those functions changes." + ), +} + +#: The functions a reviewed reason rests on, by repository-relative path and +#: name, with a digest of each one's source. A change to one needs the +#: reasons that name it reviewed again. +_REVIEWED_HELPERS: dict[tuple[str, str], str] = { + ( + "tools/verify_uk_identity_stability.py", + "_pre_clone_household_weights", + ): "5edc525581d7", + ( + "tools/verify_uk_identity_stability.py", + "_pre_residential_household_weights", + ): "e39cc12da417", +} + +_MAKES_ARRAY_WRITABLE = "makes-array-writable" +_WRITES_THROUGH_SERIES_ARRAY = "writes-through-series-array" +_PANDAS_PRIVATE_ATTRIBUTE = "pandas-private-attribute" +_NUMPY_COPYTO = "numpy-copyto" +_WRITES_INTO_EXISTING_ARRAY = "writes-into-existing-array" + +#: Private attributes refused on any object. +_ALWAYS_PRIVATE_ATTRIBUTES = frozenset({"_ndarray", "_mgr"}) +#: Private attributes refused unless read from ``self`` or ``cls``. +_FOREIGN_PRIVATE_ATTRIBUTES = frozenset({"_values", "_data", "_mask", "_codes"}) +_OWN_OBJECT_NAMES = frozenset({"self", "cls"}) + +#: NumPy functions whose result is new memory (unless given ``out=``), +#: checked for ndarrays and for integer, float and boolean Series. +#: ``np.clip``, ``np.tile``, ``np.round`` and ``np.take`` are left out: +#: given a pandas object, their result can share its memory. +_ALLOCATING_NUMPY_FUNCTIONS = frozenset( + { + "where", + "select", + "sort", + "argsort", + "unique", + "searchsorted", + "isin", + "bincount", + "cumsum", + "cumprod", + "interp", + "flatnonzero", + "outer", + "dot", + "empty", + "zeros", + "ones", + "full", + "empty_like", + "zeros_like", + "ones_like", + "full_like", + "arange", + "linspace", + "eye", + "identity", + "copy", + "concatenate", + "stack", + "hstack", + "vstack", + "column_stack", + "repeat", + } +) + +#: NumPy random Generator methods that draw a new array. +_RANDOM_DRAW_METHODS = frozenset( + { + "random", + "normal", + "uniform", + "integers", + "permutation", + "standard_normal", + "exponential", + "poisson", + "binomial", + } +) + +#: ndarray methods, and NumPy random Generator methods, that take ``out``. +_METHODS_WITH_OUT = frozenset( + { + "all", + "any", + "argmax", + "argmin", + "choose", + "clip", + "compress", + "cumprod", + "cumsum", + "dot", + "max", + "mean", + "min", + "prod", + "round", + "std", + "sum", + "take", + "trace", + "var", + "random", + "standard_normal", + "standard_exponential", + "standard_gamma", + "permuted", + } +) + +#: Text every refused expression contains. A module without any of it is not +#: parsed, and a statement whose lines hold none of it is not visited. +_TRIGGER_TEXT = re.compile( + r"setflags|writeable|WRITEABLE|\bflags\b|copyto|\.\s*array\b|\bout\s*=" + r"|\.\s*at\s*\(|\.\s*_(?:ndarray|mgr|values|data|mask|codes)\b" +) + +_NESTED_SCOPES = (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Lambda) + +#: What ``_created_kind`` returns: a new array, or a ``.copy()``. +_NEW_ARRAY = "new array" +_COPY = "copy" + + +@dataclass(frozen=True) +class Finding: + """One refused expression: where it is, which rule, and its source.""" + + path: str + line: int + function: str + rule: str + code: str + function_digest: str + + @property + def key(self) -> tuple[str, str, str, str, str]: + return (self.path, self.function, self.rule, self.code, self.function_digest) + + def describe(self) -> str: + code = self.code if len(self.code) <= 120 else self.code[:117] + "..." + return f"{self.path}:{self.line} in {self.function}: {self.rule}: {code}" + + +def _is_false_constant(expression: ast.expr) -> bool: + return isinstance(expression, ast.Constant) and not expression.value + + +def _is_none(expression: ast.expr | None) -> bool: + return isinstance(expression, ast.Constant) and expression.value is None + + +def _keyword(call: ast.Call, name: str) -> ast.keyword | None: + return next((keyword for keyword in call.keywords if keyword.arg == name), None) + + +def _is_true_constant(expression: ast.expr) -> bool: + return isinstance(expression, ast.Constant) and expression.value is True + + +def _is_slice(index: ast.expr) -> bool: + """Whether a subscript's index is a slice (``a[1:]``, ``a[:, 0:2]``).""" + + if isinstance(index, ast.Tuple): + return any(isinstance(part, ast.Slice) for part in index.elts) + return isinstance(index, ast.Slice) + + +def _is_own_object(expression: ast.expr) -> bool: + return isinstance(expression, ast.Name) and expression.id in _OWN_OBJECT_NAMES + + +def _is_series_array(expression: ast.expr) -> bool: + return ( + isinstance(expression, ast.Attribute) + and expression.attr == "array" + and not _is_own_object(expression.value) + ) + + +def _statements(tree: ast.Module) -> Iterator[ast.AST]: + """Every statement at any depth, without entering expressions.""" + + stack: list[ast.AST] = list(reversed(tree.body)) + while stack: + node = stack.pop() + yield node + for field_name in ("body", "orelse", "finalbody", "handlers", "cases"): + children = getattr(node, field_name, None) + if isinstance(children, list): + stack.extend(reversed(children)) + + +class _NumpyNames: + """The names one module binds to NumPy (``np``, ``from numpy import add``).""" + + def __init__(self, tree: ast.Module) -> None: + self._aliases: dict[str, str] = {} + for node in _statements(tree): + if isinstance(node, ast.Import): + for alias in node.names: + if alias.name == "numpy" or alias.name.startswith("numpy."): + if alias.asname: + self._aliases[alias.asname] = alias.name + else: + self._aliases["numpy"] = "numpy" + elif ( + isinstance(node, ast.ImportFrom) + and node.level == 0 + and node.module is not None + and (node.module == "numpy" or node.module.startswith("numpy.")) + ): + for alias in node.names: + self._aliases[alias.asname or alias.name] = ( + f"{node.module}.{alias.name}" + ) + + def resolve(self, expression: ast.expr) -> str | None: + """The dotted NumPy name of ``expression`` (``numpy.add.at``), if any.""" + + attributes: list[str] = [] + while isinstance(expression, ast.Attribute): + attributes.append(expression.attr) + expression = expression.value + if not isinstance(expression, ast.Name): + return None + root = self._aliases.get(expression.id) + if root is None: + return None + return ".".join([root, *reversed(attributes)]) + + +def _numpy_object(dotted_name: str) -> object | None: + found: object = np + for part in dotted_name.split(".")[1:]: + found = getattr(found, part, None) + if found is None: + return None + return found + + +def _nodes_in_scope(roots: Iterable[ast.AST]) -> Iterator[ast.AST]: + """The nodes of one function or module, without entering nested ones.""" + + stack = list(reversed(list(roots))) + while stack: + node = stack.pop() + yield node + if not isinstance(node, _NESTED_SCOPES): + stack.extend(reversed(list(ast.iter_child_nodes(node)))) + + +class _Scope: + """One function, class or module and its assignments, collected on first use. + + ``assignments`` maps a name to the value of each plain assignment to it, + with ``None`` for a binding of any other kind. + """ + + def __init__(self, qualified_name: str, node: ast.AST) -> None: + self.qualified_name = qualified_name + self.node = node + self._assignments: defaultdict[str, list[ast.expr | None]] | None = None + self._not_local: set[str] = set() + + def assignments_of(self, name: str) -> list[ast.expr | None] | None: + """The assignments of a local ``name``, or ``None`` if it is not local.""" + + if self._assignments is None: + self._collect() + if name in self._not_local: + return None + return self._assignments.get(name) or None + + def _collect(self) -> None: + assignments: defaultdict[str, list[ast.expr | None]] = defaultdict(list) + node = self.node + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)): + arguments = node.args + self._not_local.update( + argument.arg + for argument in ( + *arguments.posonlyargs, + *arguments.args, + *arguments.kwonlyargs, + arguments.vararg, + arguments.kwarg, + ) + if argument is not None + ) + body = [node.body] if isinstance(node, ast.Lambda) else node.body + + def bind_other(target: ast.AST) -> None: + # Only names the target binds: ``a[0], b = ...`` binds ``b`` and + # writes an item of ``a``. + if isinstance(target, ast.Name): + assignments[target.id].append(None) + elif isinstance(target, ast.Starred): + bind_other(target.value) + elif isinstance(target, (ast.Tuple, ast.List)): + for element in target.elts: + bind_other(element) + + for child in _nodes_in_scope(body): + if isinstance(child, (ast.Assign, ast.AnnAssign, ast.NamedExpr)): + if child.value is None: + continue + targets = ( + child.targets if isinstance(child, ast.Assign) else [child.target] + ) + for target in targets: + if isinstance(target, ast.Name): + assignments[target.id].append(child.value) + elif isinstance(target, (ast.Tuple, ast.List, ast.Starred)): + bind_other(target) + elif isinstance(child, (ast.For, ast.AsyncFor, ast.comprehension)): + bind_other(child.target) + elif isinstance(child, ast.withitem) and child.optional_vars is not None: + bind_other(child.optional_vars) + elif isinstance(child, ast.ExceptHandler) and child.name: + assignments[child.name].append(None) + elif isinstance(child, (ast.Import, ast.ImportFrom)): + for alias in child.names: + assignments[alias.asname or alias.name.split(".", 1)[0]].append( + None + ) + elif isinstance( + child, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef) + ): + assignments[child.name].append(None) + elif isinstance(child, (ast.Global, ast.Nonlocal)): + self._not_local.update(child.names) + elif isinstance(child, (ast.MatchAs, ast.MatchStar)) and child.name: + assignments[child.name].append(None) + elif isinstance(child, ast.MatchMapping) and child.rest: + assignments[child.rest].append(None) + self._assignments = assignments + + +def _trigger_lines(source: str) -> list[int] | None: + """The numbers of the lines holding trigger text, or ``None`` when lines + cannot be counted as the parser counts them (a lone carriage return).""" + + if "\r" in source and "\r" in source.replace("\r\n", ""): + return None + newlines = [match.start() for match in re.finditer("\n", source)] + return sorted( + { + bisect.bisect_left(newlines, match.start()) + 1 + for match in _TRIGGER_TEXT.finditer(source) + } + ) + + +class _Scanner(ast.NodeVisitor): + def __init__(self, source: str, path: str, tree: ast.Module) -> None: + self._source = source + self._path = path + self._numpy = _NumpyNames(tree) + self._scopes = [_Scope("", tree)] + self._trigger_lines = _trigger_lines(source) + self.findings: list[Finding] = [] + + def visit(self, node: ast.AST) -> None: + # Every rule looks at text inside the statement it reports, so a + # statement without trigger text cannot hold a finding. + if self._trigger_lines is not None and isinstance(node, ast.stmt): + decorators = getattr(node, "decorator_list", ()) + first = min([node.lineno, *(item.lineno for item in decorators)]) + index = bisect.bisect_left(self._trigger_lines, first) + last = node.end_lineno or node.lineno + if index >= len(self._trigger_lines) or self._trigger_lines[index] > last: + return + super().visit(node) + + # Scopes and reporting ------------------------------------------------ + + def _visit_scope(self, node: ast.AST, name: str) -> None: + enclosing = self._scopes[-1].qualified_name + qualified = name if enclosing == "" else f"{enclosing}.{name}" + # Decorators, defaults and base classes belong to the enclosing scope. + body = [node.body] if isinstance(node, ast.Lambda) else node.body + inside = {id(statement) for statement in body} + for child in ast.iter_child_nodes(node): + if id(child) not in inside: + self.visit(child) + self._scopes.append(_Scope(qualified, node)) + for statement in body: + self.visit(statement) + self._scopes.pop() + + def visit_FunctionDef(self, node: ast.FunctionDef) -> None: + self._visit_scope(node, node.name) + + def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None: + self._visit_scope(node, node.name) + + def visit_ClassDef(self, node: ast.ClassDef) -> None: + self._visit_scope(node, node.name) + + def visit_Lambda(self, node: ast.Lambda) -> None: + self._visit_scope(node, "") + + def _report(self, node: ast.AST, rule: str) -> None: + scope = self._scopes[-1] + if isinstance(scope.node, ast.Module): + function_source = self._source + else: + function_source = ast.get_source_segment(self._source, scope.node) or "" + segment = ast.get_source_segment(self._source, node) or ast.unparse(node) + self.findings.append( + Finding( + path=self._path, + line=node.lineno, + function=scope.qualified_name, + rule=rule, + code=" ".join(segment.split()), + function_digest=hashlib.sha256( + function_source.encode("utf-8") + ).hexdigest()[:12], + ) + ) + + # Created arrays -------------------------------------------------------- + + def _created_kind(self, expression: ast.expr) -> str | None: + """``_NEW_ARRAY`` for a new array; ``_COPY`` for a new object whose + items may be shared (a ``.copy()``, or ``+``, ``*`` or ``|``, which + also join containers); ``None`` for anything else.""" + + items_taken = False + while isinstance(expression, ast.Subscript): + items_taken = items_taken or not _is_slice(expression.slice) + expression = expression.value + kind = self._whole_created_kind(expression) + if kind == _COPY and items_taken: + return None + return kind + + def _whole_created_kind(self, expression: ast.expr) -> str | None: + if isinstance(expression, ast.BinOp): + joins = isinstance(expression.op, (ast.Add, ast.Mult, ast.BitOr)) + return _COPY if joins else _NEW_ARRAY + if isinstance(expression, (ast.UnaryOp, ast.Compare)): + return _NEW_ARRAY + if not isinstance(expression, ast.Call) or _keyword(expression, "out"): + return None + function = expression.func + name = self._numpy.resolve(function) + if name is None and isinstance(function, ast.Attribute): + if function.attr == "copy": + # ``Index.copy()`` is shallow by default, and a slice of an + # index shares the index's memory. + plain = not expression.args and not expression.keywords + copied = function.value + while isinstance(copied, ast.Subscript): + copied = copied.value + of_an_index = isinstance(copied, ast.Attribute) and ( + copied.attr in ("index", "columns") + ) + return _COPY if plain and not of_an_index else None + if function.attr == "to_numpy": + copy = _keyword(expression, "copy") + return _NEW_ARRAY if copy and _is_true_constant(copy.value) else None + if function.attr in _RANDOM_DRAW_METHODS: + return _NEW_ARRAY + if name is None: + return None + if name == "numpy.array": + copy = _keyword(expression, "copy") + return _NEW_ARRAY if copy is None or _is_true_constant(copy.value) else None + if name.removeprefix("numpy.") in _ALLOCATING_NUMPY_FUNCTIONS: + return _NEW_ARRAY + return _NEW_ARRAY if isinstance(_numpy_object(name), np.ufunc) else None + + def _is_created(self, expression: ast.expr) -> bool: + items_taken = False + while isinstance(expression, ast.Subscript): + items_taken = items_taken or not _is_slice(expression.slice) + expression = expression.value + if isinstance(expression, ast.Name): + assignments = self._scopes[-1].assignments_of(expression.id) + if assignments is None: + return False + values = [value for value in assignments if not _is_none(value)] + if not values or any(value is None for value in values): + return False + kinds = [self._created_kind(value) for value in values] + else: + kinds = [self._created_kind(expression)] + if None in kinds: + return False + return not (items_taken and _COPY in kinds) + + # Rules ----------------------------------------------------------------- + + def _written_arrays(self, call: ast.Call) -> list[ast.expr]: + """``out=`` given to NumPy or an array method, and ``ufunc.at``'s array.""" + + written: list[ast.expr] = [] + name = self._numpy.resolve(call.func) + function = call.func + output = _keyword(call, "out") + array_method = ( + isinstance(function, ast.Attribute) and function.attr in _METHODS_WITH_OUT + ) + if output is not None and (name is not None or array_method): + if isinstance(output.value, (ast.Tuple, ast.List)): + written.extend( + element for element in output.value.elts if not _is_none(element) + ) + elif not _is_false_constant(output.value): + written.append(output.value) + if name is not None and name.endswith(".at") and call.args: + if isinstance(_numpy_object(name[: -len(".at")]), np.ufunc): + written.append(call.args[0]) + return written + + def visit_Call(self, node: ast.Call) -> None: + function = node.func + if isinstance(function, ast.Attribute): + if function.attr == "setflags": + write = _keyword(node, "write") + flag = write.value if write is not None else None + if flag is None and node.args: + flag = node.args[0] + if flag is not None and not _is_false_constant(flag): + self._report(node, _MAKES_ARRAY_WRITABLE) + elif function.attr == "__setitem__" and _is_series_array(function.value): + self._report(node, _WRITES_THROUGH_SERIES_ARRAY) + for written in self._written_arrays(node): + if not self._is_created(written): + self._report(node, _WRITES_INTO_EXISTING_ARRAY) + self.generic_visit(node) + + def _check_target( + self, node: ast.AST, target: ast.expr, value: ast.expr | None + ) -> None: + if isinstance(target, (ast.Tuple, ast.List)): + for element in target.elts: + self._check_target(node, element, None) + elif isinstance(target, ast.Starred): + self._check_target(node, target.value, None) + elif isinstance(target, ast.Subscript): + container = target.value + if ( + isinstance(container, ast.Attribute) + and container.attr == "flags" + and isinstance(target.slice, ast.Constant) + and target.slice.value in ("W", "WRITEABLE") + ): + if value is None or not _is_false_constant(value): + self._report(node, _MAKES_ARRAY_WRITABLE) + elif _is_series_array(container): + self._report(node, _WRITES_THROUGH_SERIES_ARRAY) + elif ( + isinstance(target, ast.Attribute) + and target.attr == "writeable" + and isinstance(target.value, ast.Attribute) + and target.value.attr == "flags" + ): + if value is None or not _is_false_constant(value): + self._report(node, _MAKES_ARRAY_WRITABLE) + + def visit_Assign(self, node: ast.Assign) -> None: + for target in node.targets: + self._check_target(node, target, node.value) + self.generic_visit(node) + + def visit_AnnAssign(self, node: ast.AnnAssign) -> None: + # An annotation without a value writes nothing. + if node.value is not None: + self._check_target(node, node.target, node.value) + self.generic_visit(node) + + def visit_AugAssign(self, node: ast.AugAssign) -> None: + self._check_target(node, node.target, None) + self.generic_visit(node) + + def visit_Attribute(self, node: ast.Attribute) -> None: + if node.attr in _ALWAYS_PRIVATE_ATTRIBUTES or ( + node.attr in _FOREIGN_PRIVATE_ATTRIBUTES and not _is_own_object(node.value) + ): + self._report(node, _PANDAS_PRIVATE_ATTRIBUTE) + elif node.attr == "copyto": + self._report(node, _NUMPY_COPYTO) + self.generic_visit(node) + + def visit_ImportFrom(self, node: ast.ImportFrom) -> None: + if node.module is not None and ( + node.module == "numpy" or node.module.startswith("numpy.") + ): + if any(alias.name == "copyto" for alias in node.names): + self._report(node, _NUMPY_COPYTO) + self.generic_visit(node) + + +def scan_source(source: str, path: str) -> list[Finding]: + """Every refused expression in one module's source.""" + + if not _TRIGGER_TEXT.search(source): + return [] + tree = ast.parse(source, filename=path) + scanner = _Scanner(source, path, tree) + scanner.visit(tree) + return scanner.findings + + +def scanned_files(repository: Path) -> list[Path]: + """The Python files the scan reads: ``packages/*/src/`` and ``tools/``.""" + + roots = [*sorted(repository.glob("packages/*/src")), repository / "tools"] + files: list[Path] = [] + for root in roots: + for path in sorted(root.rglob("*.py")): + parts = path.relative_to(root).parts[:-1] + if any(part == "node_modules" or part.startswith(".") for part in parts): + continue + files.append(path) + return files + + +@dataclass(frozen=True) +class _WorkspaceScan: + paths: frozenset[str] + findings: tuple[Finding, ...] + + +@pytest.fixture(scope="module") +def workspace_scan() -> _WorkspaceScan: + paths: set[str] = set() + findings: list[Finding] = [] + for path in scanned_files(_REPOSITORY): + relative = path.relative_to(_REPOSITORY).as_posix() + paths.add(relative) + # utf-8-sig drops a byte-order mark, which ast.parse refuses. + findings.extend(scan_source(path.read_text(encoding="utf-8-sig"), relative)) + return _WorkspaceScan(frozenset(paths), tuple(findings)) + + +def test_the_scanned_source_has_no_unprotected_buffer_writes( + workspace_scan: _WorkspaceScan, +) -> None: + unreviewed = [ + finding + for finding in workspace_scan.findings + if finding.key not in _REVIEWED_FINDINGS + ] + + assert not unreviewed, ( + "These expressions can write into memory that frames share under " + "pandas copy-on-write (see this module's docstring for each rule). " + "Write through pandas instead (.loc, column assignment), or copy the " + "array first (np.array(values, copy=True)). A finding that cannot " + "write into memory a pandas object shares goes in " + "_REVIEWED_FINDINGS, under the key shown, with the reason.\n" + + "\n".join( + f"{finding.describe()}\n key: {finding.key!r}" for finding in unreviewed + ) + ) + + +def test_every_reviewed_finding_still_occurs(workspace_scan: _WorkspaceScan) -> None: + present = {finding.key for finding in workspace_scan.findings} + + assert not sorted(set(_REVIEWED_FINDINGS) - present), ( + "These reviewed findings no longer occur as reviewed. The expression " + "or its enclosing function changed, so review it again and update " + "its key, or remove the entry." + ) + + +def _definition(tree: ast.Module, qualified_name: str) -> ast.AST: + """The one function or class whose qualified name (``Class.method``) is + ``qualified_name``.""" + + found: list[ast.AST] = [] + stack: list[tuple[str, ast.AST]] = [("", node) for node in tree.body] + while stack: + prefix, node = stack.pop() + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): + name = f"{prefix}{node.name}" + if name == qualified_name: + found.append(node) + stack.extend((f"{name}.", child) for child in node.body) + assert len(found) == 1, f"{qualified_name} is defined {len(found)} times" + return found[0] + + +def test_the_functions_reviewed_reasons_rest_on_are_unchanged() -> None: + changed: list[str] = [] + for (relative, name), digest in _REVIEWED_HELPERS.items(): + source = (_REPOSITORY / relative).read_text(encoding="utf-8-sig") + function = _definition(ast.parse(source), name) + segment = ast.get_source_segment(source, function) or "" + current = hashlib.sha256(segment.encode("utf-8")).hexdigest()[:12] + if current != digest: + changed.append(f"{relative}:{name} now has digest {current!r}") + + assert not changed, ( + "A reviewed reason in _REVIEWED_FINDINGS rests on these functions, " + "which changed. Review the reasons that name them again, then update " + "_REVIEWED_HELPERS.\n" + "\n".join(changed) + ) + + +def test_the_scan_covers_every_package_and_the_us_frame_building_code( + workspace_scan: _WorkspaceScan, +) -> None: + packages_with_source = { + directory.name + for directory in (_REPOSITORY / "packages").iterdir() + if (directory / "src").is_dir() + } + scanned_packages = { + path.split("/")[1] + for path in workspace_scan.paths + if path.startswith("packages/") + } + + assert scanned_packages == packages_with_source + assert { + "packages/microcosm-frame/src/microcosm/frame/bundle.py", + "packages/microcosm-build/src/microcosm/build/frame_checkpoint.py", + "packages/microcosm-build/src/microcosm/build/us_runtime/acs_transfer.py", + "packages/microcosm-build/src/microcosm/build/us_runtime/stacked_spine.py", + "tools/build_us_fiscal_refresh_release.py", + "tools/build_us_multispine_pool.py", + } <= workspace_scan.paths + + +def test_the_scan_skips_node_modules_and_hidden_directories(tmp_path: Path) -> None: + for relative in ( + "packages/microcosm-example/src/microcosm/example/module.py", + "packages/microcosm-example/tests/test_module.py", + "tools/tool.py", + "tools/route_a/helper.py", + "tools/orrery-contract/node_modules/node-gyp/gyp/pylib/gyp/input.py", + "tools/.cache/generated.py", + ): + (tmp_path / relative).parent.mkdir(parents=True, exist_ok=True) + (tmp_path / relative).write_text("", encoding="utf-8") + + scanned = [ + path.relative_to(tmp_path).as_posix() for path in scanned_files(tmp_path) + ] + + assert scanned == [ + "packages/microcosm-example/src/microcosm/example/module.py", + "tools/route_a/helper.py", + "tools/tool.py", + ] + + +_NUMPY = "import numpy as np\n" + + +def _in_function(*body_lines: str, parameters: str = "values") -> str: + """A module importing NumPy with one function holding ``body_lines``.""" + + body = "".join(f" {line}\n" for line in body_lines) + return f"{_NUMPY}def change({parameters}):\n{body}" + + +#: Snippets the scan must refuse, with the one rule each must report. +_REFUSED_SNIPPETS = { + # makes-array-writable + "setflags_write_true": ( + "values.setflags(write=True)\nvalues[0] = 1.0\n", + _MAKES_ARRAY_WRITABLE, + ), + "setflags_positional": ("values.setflags(True)\n", _MAKES_ARRAY_WRITABLE), + "setflags_variable": ("values.setflags(write=flag)\n", _MAKES_ARRAY_WRITABLE), + "flags_writeable_attribute": ( + "values.flags.writeable = True\n", + _MAKES_ARRAY_WRITABLE, + ), + "flags_writeable_key": ( + 'values.flags["WRITEABLE"] = True\n', + _MAKES_ARRAY_WRITABLE, + ), + "flags_w_key": ('values.flags["W"] = 1\n', _MAKES_ARRAY_WRITABLE), + "flags_writeable_from_a_name": ( + "values.flags.writeable = flag\n", + _MAKES_ARRAY_WRITABLE, + ), + "flags_w_key_from_a_name": ('values.flags["W"] = flag\n', _MAKES_ARRAY_WRITABLE), + "flags_writeable_from_an_expression": ( + "values.flags.writeable = not frozen\n", + _MAKES_ARRAY_WRITABLE, + ), + # writes-through-series-array + "series_array_item": ( + 'frame.person["income"].array[0] = 1.0\n', + _WRITES_THROUGH_SERIES_ARRAY, + ), + "series_array_augmented": ( + "series.array[mask] += 1\n", + _WRITES_THROUGH_SERIES_ARRAY, + ), + "series_array_annotated": ( + "series.array[0]: float = 1.0\n", + _WRITES_THROUGH_SERIES_ARRAY, + ), + "out_parameter_rebound_on_one_path": ( + _in_function( + "if flag:", + " values = np.zeros(3)", + "np.add.at(values, [0], 1)", + parameters="values, flag", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_lambda_parameter_shadowing_a_module_array": ( + _NUMPY + + "values = np.zeros(3)\n" + + "add_one = lambda values: np.add(values, 1, out=values)\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_name_only_ever_none": ( + _in_function("buffer = None", "np.add(1, 1, out=buffer)", parameters=""), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_nonlocal_name": ( + _NUMPY + + "def outer():\n" + + " buffer = np.zeros(3)\n" + + " def inner():\n" + + " nonlocal buffer\n" + + " buffer = np.zeros(3)\n" + + " np.add(1, 1, out=buffer)\n" + + " return inner\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_imported_name_shadowing_a_new_array": ( + _in_function( + "buffer = np.zeros(3)", + "import buffer", + "np.add(1, 1, out=buffer)", + parameters="", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "ufunc_at_on_an_item_of_a_merged_dict": ( + _in_function( + 'merged = columns | {"w": weights}', + 'np.add.at(merged["income"], [0], 100)', + parameters="columns, weights", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "ufunc_at_on_an_item_of_joined_lists": ( + _in_function( + "joined = parts + extra", + "np.add.at(joined[0], [0], 1)", + parameters="parts, extra", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "ufunc_at_on_an_index_copy": ( + _in_function( + "ids = table.index.copy()", + "np.add.at(ids, positions, 1)", + parameters="table, positions", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "ufunc_at_on_a_copy_of_an_index_slice": ( + _in_function( + "ids = table.index[1:].copy()", + "np.add.at(ids, positions, 1)", + parameters="table, positions", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_standard_library_random_choice": ( + _in_function( + "chosen = random.choice(arrays)", + "np.add(chosen, 1, out=chosen)", + parameters="random, arrays", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "ufunc_at_on_an_item_of_a_copied_dict": ( + _in_function( + "updated = columns.copy()", + 'np.add.at(updated["income"], [0], 100)', + parameters="columns", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_astype": ( + _in_function( + "converted = series.astype(float)", + "np.add(converted, 1, out=converted)", + parameters="series", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "series_array_unpacked": ( + "series.array[0], other = 1.0, 2.0\n", + _WRITES_THROUGH_SERIES_ARRAY, + ), + "series_array_setitem_call": ( + "series.array.__setitem__(0, 1.0)\n", + _WRITES_THROUGH_SERIES_ARRAY, + ), + "index_array_item": ("table.index.array[0] = 7\n", _WRITES_THROUGH_SERIES_ARRAY), + # pandas-private-attribute + "ndarray_attribute": ( + "values = series.array._ndarray\n", + _PANDAS_PRIVATE_ATTRIBUTE, + ), + "manager_attribute": ("blocks = frame._mgr.blocks\n", _PANDAS_PRIVATE_ATTRIBUTE), + "manager_attribute_on_self": ( + "class Table:\n def blocks(self):\n return self._mgr.blocks\n", + _PANDAS_PRIVATE_ATTRIBUTE, + ), + "series_values_attribute": ("raw = series._values\n", _PANDAS_PRIVATE_ATTRIBUTE), + "masked_data": ("raw = series.array._data\n", _PANDAS_PRIVATE_ATTRIBUTE), + "masked_mask": ("series.array._mask[:] = False\n", _PANDAS_PRIVATE_ATTRIBUTE), + "categorical_codes": ( + "codes = series.cat.codes.array._codes\n", + _PANDAS_PRIVATE_ATTRIBUTE, + ), + # numpy-copyto + "copyto": (_NUMPY + "np.copyto(target, source)\n", _NUMPY_COPYTO), + "copyto_through_numpy": ( + "import numpy\nnumpy.copyto(target, source)\n", + _NUMPY_COPYTO, + ), + "copyto_imported": ( + "from numpy import copyto\ncopyto(target, source)\n", + _NUMPY_COPYTO, + ), + "copyto_imported_as_alias": ( + "from numpy import copyto as overwrite\n", + _NUMPY_COPYTO, + ), + "copyto_passed_as_function": ("apply_each(np.copyto, pairs)\n", _NUMPY_COPYTO), + # writes-into-existing-array + "out_parameter": ( + _in_function("np.add(values, 1, out=values)"), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_attribute": ( + _NUMPY + "np.multiply(scale, 2, out=self.buffer)\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_to_numpy": ( + _NUMPY + "np.add(scale, 1, out=series.to_numpy())\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_tuple": ( + _in_function("np.divmod(7, 2, out=(left, right))", parameters="left, right"), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_array_method": ( + _in_function("values.clip(0, 1, out=values)"), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_generator_method": ( + _in_function("rng.random(out=values)", parameters="rng, values"), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_through_imported_ufunc": ( + "from numpy import add\ndef change(values):\n add(1, 1, out=values)\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_name_bound_to_a_view": ( + _in_function( + "buffer = series.to_numpy()", + "np.add(buffer, 1, out=buffer)", + parameters="series", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_name_rebound_to_a_view": ( + _in_function( + "buffer = np.zeros(3)", + "buffer = series.to_numpy()", + "np.add(buffer, 1, out=buffer)", + parameters="series", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_name_bound_to_another_name": ( + _in_function( + "buffer = np.zeros(3)", + "alias = buffer", + "np.add(alias, 1, out=alias)", + parameters="", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_shallow_copy": ( + _in_function( + "shallow = table.copy(deep=False)", + "np.add(1, 1, out=shallow)", + parameters="table", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_array_without_copy": ( + _in_function( + "view = np.array(values, copy=None)", + "np.add(view, 1, out=view)", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_allocation_given_an_output": ( + _in_function( + "joined = np.concatenate(parts, out=values)", + "np.add(joined, 1, out=joined)", + parameters="parts, values", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_module_array_used_in_a_function": ( + _NUMPY + "buffer = np.zeros(3)\ndef change():\n np.add(1, 1, out=buffer)\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_global_declared": ( + _in_function( + "global buffer", + "buffer = np.zeros(3)", + "np.add(1, 1, out=buffer)", + parameters="", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_loop_target_shadowing_a_new_array": ( + _in_function( + "buffer = np.zeros(3)", + "for buffer in arrays:", + " np.add(buffer, 1, out=buffer)", + parameters="arrays", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_comprehension_target_shadowing_a_new_array": ( + _in_function( + "buffer = np.zeros(3)", + "return [np.add(buffer, 1, out=buffer) for buffer in arrays]", + parameters="arrays", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_with_target_shadowing_a_new_array": ( + _in_function( + "buffer = np.zeros(3)", + "with opener() as buffer:", + " np.add(buffer, 1, out=buffer)", + parameters="opener", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_unpacked_shadowing_a_new_array": ( + _in_function( + "buffer = np.zeros(3)", + "buffer, other = pair", + "np.add(buffer, 1, out=buffer)", + parameters="pair", + ), + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_lambda_parameter": ( + _NUMPY + "add_one = lambda values: np.add(values, 1, out=values)\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_in_a_decorator_over_a_function_binding_the_name": ( + _NUMPY + + "@register(np.add(1, 1, out=buffer))\n" + + "def change():\n" + + " buffer = np.zeros(3)\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "ufunc_at": ( + _in_function("np.add.at(values, [0], 1)"), + _WRITES_INTO_EXISTING_ARRAY, + ), + "ufunc_at_imported": ( + "from numpy import add\ndef count(values):\n add.at(values, [0], 1)\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_after_lone_carriage_returns": ( + "import numpy as np\rdef change(values):\r np.add(values, 1, out=values)\r", + _WRITES_INTO_EXISTING_ARRAY, + ), + "out_in_a_decorator_over_a_plain_function": ( + _NUMPY + + "@register(np.add(1, 1, out=buffer))\n" + + "def change():\n" + + " return 1\n", + _WRITES_INTO_EXISTING_ARRAY, + ), + "ufunc_at_on_to_numpy": ( + _NUMPY + "np.maximum.at(series.to_numpy(), positions, weights)\n", + _WRITES_INTO_EXISTING_ARRAY, + ), +} + + +@pytest.mark.parametrize( + ("source", "rule"), list(_REFUSED_SNIPPETS.values()), ids=list(_REFUSED_SNIPPETS) +) +def test_the_scanner_refuses_each_unprotected_write(source: str, rule: str) -> None: + findings = scan_source(source, "snippet.py") + + assert {finding.rule for finding in findings} == {rule}, findings + + +#: Snippets the scan must accept: reads, read-only flags, and writes into +#: arrays the function created. +_ACCEPTED_SNIPPETS = { + "setflags_read_only": "values.setflags(write=False)\nvalues.setflags(write=0)\n", + "setflags_alignment_only": "values.setflags(align=True)\n", + "flags_read_only": ( + 'values.flags.writeable = False\nvalues.flags["WRITEABLE"] = False\n' + ), + "series_array_reads": ( + "for value in series.array:\n" + + " total += value\n" + + "index = pd.Index(series.array, name='id')\n" + + "first = series.array[0]\n" + ), + "own_array_and_private_state": ( + "class Grid:\n" + + " def set(self, row, value):\n" + + " self.array[row] = value\n" + + " self._values[row] = value\n" + + " self._data.append(value)\n" + + " @classmethod\n" + + " def empty(cls):\n" + + " return cls._mask\n" + ), + "array_function_not_attribute": ( + _NUMPY + + "import pandas as pd\n" + + "values = np.array(series)\n" + + "values[0] = 1\n" + + "codes = pd.array([1, 2])\n" + + "codes[0] = 3\n" + ), + "out_new_array_inline": ( + _NUMPY + + "share = np.divide(part, total, out=np.zeros_like(part), where=total > 0)\n" + ), + "out_new_array_slice": _in_function( + "totals = np.empty(len(weights) + 1)", + "totals[0] = 0.0", + "np.cumsum(weights, out=totals[1:])", + parameters="weights", + ), + "out_copy": _in_function( + "total = first.copy()", + "np.add(total, later, out=total)", + parameters="first, later", + ), + "out_same_array_as_input": _in_function( + "draws = np.empty(count)", + "np.minimum(draws, 1.0, out=draws)", + "return draws", + parameters="count", + ), + "out_rebound_between_new_arrays": _in_function( + "out = np.zeros(3)", + "np.add.at(out, positions, weights)", + "out = np.where(mask, out, 0.0)", + "np.multiply(out, 2, out=out)", + parameters="positions, weights, mask", + ), + "out_none_placeholder_then_copy": _in_function( + "total: np.ndarray | None = None", + "for current in slices:", + " if total is None:", + " total = current.copy()", + " else:", + " np.add(total, current, out=total)", + parameters="slices", + ), + "out_ufunc_and_array_results": _in_function( + "scaled = np.exp(values)", + "np.multiply(scaled, 2, out=scaled)", + "owned = np.array(values)", + "np.add(owned, 1, out=owned)", + ), + "out_none": _NUMPY + "np.add(left, right, out=None)\n", + "out_none_pair": _NUMPY + "np.divmod(left, 2, out=(None, None))\n", + "out_keyword_of_workspace_functions": ( + "launch(pool_manifest=manifest, out=args.out)\n" + + "report.write(rows, out=destination)\n" + ), + "ufunc_at_new_array": _in_function( + "counts = np.zeros(size, dtype=np.int64)", + "np.add.at(counts, positions, 1)", + parameters="positions, size", + ), + "ufunc_without_out": _NUMPY + "total = np.add(left, right)\n", + "out_copies_the_failure_message_suggests": _in_function( + "weights = np.array(frame.w, copy=True)", + "np.add.at(weights, positions, 1)", + "values = frame.x.to_numpy(copy=True)", + "np.add(values, 1, out=values)", + parameters="frame, positions", + ), + "out_arithmetic_and_draws": _in_function( + "product = left * right", + "np.add(product, 1, out=product)", + "positive = left > 0", + "np.logical_not(positive, out=positive)", + "draws = rng.random(size)", + "np.multiply(draws, 2, out=draws)", + parameters="left, right, rng, size", + ), + "out_numpy_copy": _in_function( + "buffer = np.copy(values)", + "np.add(buffer, 1, out=buffer)", + "np.add.at(np.copy(values), [0], 1)", + ), + "out_names_bound_to_subscripts_of_new_arrays": _in_function( + "ordered = np.sort(values)[::-1]", + "np.cumsum(ordered, out=ordered)", + "row = np.zeros((2, size))[0]", + "np.add(row, 1, out=row)", + parameters="values, size", + ), + "out_items_of_a_new_array_assigned_by_unpacking": _in_function( + "bounds = np.empty(2)", + "bounds[0], bounds[1] = low, high", + "np.clip(bounds, 0, 1, out=bounds)", + "order = np.argsort(values)", + "order[0], order[-1] = order[-1], order[0]", + "np.add(order, 1, out=order)", + parameters="low, high, values", + ), + "out_multidimensional_slice_of_a_copy": _in_function( + "result = matrix.copy()", + "np.clip(result[:, 1:], 0, None, out=result[:, 1:])", + parameters="matrix", + ), + "out_slice_of_a_copy": _in_function( + "totals = first.copy()", + "np.cumsum(later, out=totals[1:])", + parameters="first, later", + ), + "annotation_without_a_value": ('series.array[0]: float\nvalues.flags["W"]: bool\n'), +} + + +@pytest.mark.parametrize( + "source", list(_ACCEPTED_SNIPPETS.values()), ids=list(_ACCEPTED_SNIPPETS) +) +def test_the_scanner_accepts_reads_and_writes_into_new_arrays(source: str) -> None: + assert scan_source(source, "snippet.py") == [] + + +def test_a_finding_names_its_file_line_function_and_digest() -> None: + method = ( + " def add(self, values):\n" + + " def inner(target):\n" + + " np.add(target, 1, out=target)\n" + + " inner(values)\n" + ) + source = _NUMPY + "class Aggregator:\n" + method + + (finding,) = scan_source(source, "pkg/module.py") + + assert finding.describe() == ( + "pkg/module.py:5 in Aggregator.add.inner: writes-into-existing-array: " + "np.add(target, 1, out=target)" + ) + inner_source = "def inner(target):\n np.add(target, 1, out=target)" + assert ( + finding.function_digest + == hashlib.sha256(inner_source.encode("utf-8")).hexdigest()[:12] + ) + + +def test_a_changed_function_changes_the_finding_key() -> None: + original = _in_function("np.add.at(values, [0], 1)") + changed = _in_function("values = values.T", "np.add.at(values, [0], 1)") + + (before,) = scan_source(original, "pkg/module.py") + (after,) = scan_source(changed, "pkg/module.py") + + assert before.code == after.code + assert before.key != after.key + + +def test_every_output_in_an_out_tuple_is_checked() -> None: + both_existing = _in_function( + "np.divmod(7, 2, out=(left, right))", parameters="left, right" + ) + second_existing = _in_function( + "np.divmod(7, 2, out=(np.zeros(1), right))", parameters="right" + ) + + assert len(scan_source(both_existing, "snippet.py")) == 2 + assert len(scan_source(second_existing, "snippet.py")) == 1 diff --git a/packages/microcosm-graph/tests/fixtures/parity/kernels/calibrate/pins.json b/packages/microcosm-graph/tests/fixtures/parity/kernels/calibrate/pins.json index da0abddedc..87c8d0d888 100644 --- a/packages/microcosm-graph/tests/fixtures/parity/kernels/calibrate/pins.json +++ b/packages/microcosm-graph/tests/fixtures/parity/kernels/calibrate/pins.json @@ -1 +1 @@ -{"dependencies":{"numpy":"2.4.6","pandas":"3.0.3","scipy":"1.17.1","torch":"2.12.0"},"implementation_hash":"3df328c2d5f449de45a543be8b5f4caf0cf2ff417542b0dc1251c7e4bfd33e64","kernel":"calibrate.adam@1","node":"calibrate","node_key":"98df2301f04fc79ffe90a07306a51d4fa8ad0cda6777d480ae6c172adef20dfe","numeric":"bitwise","platform":"arm64/darwin/py3.14","platforms":{"arm64/darwin/py3.14":{"direct":"direct.csv","node_key":"98df2301f04fc79ffe90a07306a51d4fa8ad0cda6777d480ae6c172adef20dfe"}},"seed":0} +{"dependencies":{"numpy":"2.4.6","pandas":"3.0.3","scipy":"1.17.1","torch":"2.12.0"},"implementation_hash":"584886552a44ac4208fb878f54a195fa9a6962324175ce7ae0991cafd1816a69","kernel":"calibrate.adam@1","node":"calibrate","node_key":"6bff4ad9837e10c290ecfd9778a1cd59ec0826a7bdb3fe03cc194a37feac3618","numeric":"bitwise","platform":"arm64/darwin/py3.14","platforms":{"arm64/darwin/py3.14":{"direct":"direct.csv","node_key":"6bff4ad9837e10c290ecfd9778a1cd59ec0826a7bdb3fe03cc194a37feac3618"}},"seed":0} diff --git a/packages/microcosm-graph/tests/fixtures/parity/kernels/simulate/pins.json b/packages/microcosm-graph/tests/fixtures/parity/kernels/simulate/pins.json index 1a3cecc086..d30dfcc3e5 100644 --- a/packages/microcosm-graph/tests/fixtures/parity/kernels/simulate/pins.json +++ b/packages/microcosm-graph/tests/fixtures/parity/kernels/simulate/pins.json @@ -1 +1 @@ -{"dependencies":{},"implementation_hash":"60664a4f28b84a693dd5f3b916ad04be6b9d160c090e549659a674b0c2d81e42","kernel":"simulate.rules@1","node":"simulate","node_key":"5968c72d2bb1f0c909946e3a75d6757ba3bd5d945c4dd95e2c9c680a993f3536","numeric":"bitwise","platform":"arm64/darwin/py3.14","platforms":{"arm64/darwin/py3.14":{"direct":"direct.csv","node_key":"5968c72d2bb1f0c909946e3a75d6757ba3bd5d945c4dd95e2c9c680a993f3536"}},"seed":null} +{"dependencies":{},"implementation_hash":"90753d809d5e9953a6c6b8c2a282e8dc675143d92d587fb144aba70ecd46b6ad","kernel":"simulate.rules@1","node":"simulate","node_key":"56247d886b9b25341f4c40ac18b9d331da6e692f3940f68268fe1c2a553d43e0","numeric":"bitwise","platform":"arm64/darwin/py3.14","platforms":{"arm64/darwin/py3.14":{"direct":"direct.csv","node_key":"56247d886b9b25341f4c40ac18b9d331da6e692f3940f68268fe1c2a553d43e0"}},"seed":null}