# SPDX-License-Identifier: MIT """MARIA imports nothing from the board (docs/start-here.md "Module shape"), and the PyPy layout trap: an instance with 80 or more attributes becomes a dict and its hot loop runs about three times slower (the family's PyPy 80-attribute note).""" from __future__ import annotations import ast from pathlib import Path from a7800_python.board import Board from a7800_python.cartridge.connector import Linear from a7800_python.machine import NTSC SRC = Path(__file__).resolve().parents[1] / "src" / "a7800_python" def imports_of(path: Path) -> set[str]: names = set() for node in ast.walk(ast.parse(path.read_text(encoding="utf-8"))): if isinstance(node, ast.ImportFrom): names.add(("." * node.level) + (node.module or "")) elif isinstance(node, ast.Import): names.update(alias.name for alias in node.names) return names def test_maria_imports_nothing_from_the_board(): assert imports_of(SRC / "maria.py") <= {"__future__", "collections.abc"} def test_the_hot_objects_stay_under_eighty_attributes(): board = Board(NTSC, Linear(bytes(0x4000)), None) for obj in (board, board.maria, board.tia, board.riot): assert len(vars(obj)) < 80, type(obj).__name__ # a2600-python's TIA sits at 79 by its own count: this board adds nothing to it. assert len(vars(board.tia)) <= 79