File size: 2,049 Bytes
5e0b58b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Apply narrow alpha.4 typing fixes to legacy modules in the working tree."""

from __future__ import annotations

from pathlib import Path


def replace_once(path: Path, old: str, new: str) -> None:
    """Replace one known source fragment or fail loudly."""
    text = path.read_text(encoding="utf-8")
    if old not in text:
        if new in text:
            return
        raise RuntimeError(f"expected fragment not found in {path}")
    path.write_text(text.replace(old, new, 1), encoding="utf-8")


def main() -> int:
    """Apply the three non-storage mypy fixes identified in alpha.3."""
    replace_once(
        Path("src/manipulation/manipulator.py"),
        "nid = self.network.add_neuron(coord)",
        "nid = int(self.network.add_neuron(coord))",
    )
    engine = Path("src/self_organization/engine.py")
    replace_once(
        engine,
        (
            "from src.core.spatial_index import iter_neighbour_coords, "
            "pack_coords, unpack_coords"
        ),
        "from src.core.spatial_index import (\n"
        "    Coord5D,\n"
        "    iter_neighbour_coords,\n"
        "    pack_coords,\n"
        "    unpack_coords,\n"
        ")",
    )
    replace_once(
        engine,
        "def _find_free_coord(self, neuron_id: int):",
        "def _find_free_coord(self, neuron_id: int) -> Coord5D | None:",
    )
    learning_lab = Path("src/experiments/learning_lab.py")
    replace_once(
        learning_lab,
        "from src.core.network import NeuralNetwork",
        "from src.core.network import ConfigDict, NeuralNetwork",
    )
    replace_once(
        learning_lab,
        (
            "network = NeuralNetwork(dict(config), "
            'random.Random(int(config.get("seed", 42))))'
        ),
        "network = NeuralNetwork(\n"
        "        cast(ConfigDict, dict(config)),\n"
        '        random.Random(int(config.get("seed", 42))),\n'
        "    )",
    )
    print("Applied alpha.4 legacy typing fixes.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())