From d98cc7e1ef376413131173b903a631c4896b4ecf Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 01:53:37 +0100 Subject: [PATCH 01/12] Remove an unneeded field annotation --- src/processor_utils/units.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/processor_utils/units.py b/src/processor_utils/units.py index 598cb72..0897fac 100644 --- a/src/processor_utils/units.py +++ b/src/processor_utils/units.py @@ -31,7 +31,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.0, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -136,6 +136,6 @@ def __eq__(self, other: Any) -> Any: map(operator.is_, self.predecessors, other.predecessors) ) - model: UnitModel = field() + model: UnitModel predecessors: tuple[UnitModel, ...] = field(converter=sorted_models) From 44b753aa5b459df129171dc5bf9aefa95330e0a4 Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 02:40:19 +0100 Subject: [PATCH 02/12] Import symbols from their respective packages --- src/errors.py | 8 ++++---- src/processor_sim.py | 6 +++--- src/processor_utils/__init__.py | 22 ++++++++-------------- src/processor_utils/_checks.py | 10 +++++----- src/processor_utils/_port_defs.py | 10 +++++----- src/processor_utils/exception.py | 10 +++++----- src/processor_utils/units.py | 8 ++++---- src/program_utils.py | 8 ++++---- src/sim_services/__init__.py | 12 +++++------- src/sim_services/_instr_sinks.py | 6 +++--- src/type_checking.py | 8 ++++---- tests/test_caps.py | 8 ++++---- tests/test_compiler.py | 8 ++++---- tests/test_isa.py | 8 ++++---- tests/test_reg_access.py | 6 +++--- tests/test_sanity.py | 12 ++++++------ tests/test_sim.py | 6 +++--- tests/test_units.py | 8 ++++---- 18 files changed, 78 insertions(+), 86 deletions(-) diff --git a/src/errors.py b/src/errors.py index f739643..4bc5122 100644 --- a/src/errors.py +++ b/src/errors.py @@ -31,8 +31,8 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.86.1, python 3.11.7, Fedora release -# 39 (Thirty Nine) +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release +# 40 (Forty) # # notes: This is a private program. # @@ -47,7 +47,7 @@ import attr from attr import frozen -import fastcore.foundation +import fastcore.basics # Attrs doesn't honor class variables annotated with typing.Final(albeit # being mandated by PEP-591) and instead still treats them as instance @@ -118,7 +118,7 @@ def _init( {elem.key: elem.val.displayed for elem in elems} ) ) - val_extractor = fastcore.foundation.Self.val().stored() + val_extractor = fastcore.basics.Self.val().stored() # Pylance and pylint can't detect __attrs_init__ as an injected # method. # pylint: disable-next=no-member diff --git a/src/processor_sim.py b/src/processor_sim.py index c88f40f..37d5253 100755 --- a/src/processor_sim.py +++ b/src/processor_sim.py @@ -40,7 +40,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.92.2, python 3.12.4, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -57,7 +57,7 @@ from typing import Annotated, Any, IO from attr import frozen -from fastcore import foundation +from fastcore import basics import more_itertools import typer from typer import FileText @@ -114,7 +114,7 @@ def __str__(self) -> str: return ":".join( map( self._get_res_str, - [foundation.Self._stalled(), foundation.Self._unit()], + [basics.Self._stalled(), basics.Self._unit()], ) ) diff --git a/src/processor_utils/__init__.py b/src/processor_utils/__init__.py index de804a5..f20c1bb 100644 --- a/src/processor_utils/__init__.py +++ b/src/processor_utils/__init__.py @@ -31,7 +31,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.93.1, python 3.12.6, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -56,8 +56,8 @@ from typing import Any from attr import field, frozen -from fastcore import foundation -from fastcore.foundation import compose, mapt +from fastcore import basics +from fastcore.basics import compose, mapt import networkx from networkx import DiGraph, Graph @@ -336,11 +336,7 @@ def _chk_unit_width(unit: Mapping[object, int]) -> None: raise BadWidthError( f"Functional unit ${BadWidthError.UNIT_KEY} has a bad width " f"${BadWidthError.WIDTH_KEY}.", - *( - foundation.map_ex( - [UNIT_NAME_KEY, UNIT_WIDTH_KEY], unit, gen=True - ) - ), + *(basics.map_ex([UNIT_NAME_KEY, UNIT_WIDTH_KEY], unit, gen=True)), ) @@ -360,7 +356,7 @@ def _create_graph( edge_registry = IndexedSet[Collection[str]]( lambda edge: mapt(compose(ICaseString, unit_registry.get), edge) ) - cap_registry = IndexedSet[_CapabilityInfo](foundation.Self.name()) + cap_registry = IndexedSet[_CapabilityInfo](basics.Self.name()) for cur_unit in hw_units: _add_unit(flow_graph, cur_unit, unit_registry, cap_registry) @@ -453,7 +449,7 @@ def _get_preds( The function returns an iterator over predecessor units. """ - return foundation.map_ex(processor.predecessors(unit), unit_map, gen=True) + return basics.map_ex(processor.predecessors(unit), unit_map, gen=True) def _get_proc_units(graph: DiGraph) -> Generator[FuncUnit, None, None]: @@ -498,7 +494,7 @@ def _get_unit_entry(name: str, attrs: Mapping[str, Any]) -> UnitModel: The function returns the unit model. """ - lock_attrs = foundation.map_ex([UNIT_RLOCK_KEY, UNIT_WLOCK_KEY], attrs) + lock_attrs = basics.map_ex([UNIT_RLOCK_KEY, UNIT_WLOCK_KEY], attrs) return UnitModel( name, attrs[UNIT_WIDTH_KEY], @@ -644,9 +640,7 @@ def _sorted_units(hw_units: Iterable[Any]) -> tuple[Any, ...]: `hw_units` are the units to sort. """ - return container_utils.sorted_tuple( - hw_units, key=foundation.Self.model.name() - ) + return container_utils.sorted_tuple(hw_units, key=basics.Self.model.name()) @frozen diff --git a/src/processor_utils/_checks.py b/src/processor_utils/_checks.py index 6e426c8..1f35a2b 100644 --- a/src/processor_utils/_checks.py +++ b/src/processor_utils/_checks.py @@ -32,7 +32,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.91.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -53,7 +53,7 @@ from typing import Any, TypeVar from attr import frozen -from fastcore import foundation +from fastcore import basics import more_itertools from more_itertools import one import networkx @@ -151,7 +151,7 @@ def make_read_desc(cls, capability: object, start: object) -> typing.Self: `start` is the path start unit. """ - return cls(foundation.Self.read_lock(), "read", capability, start) + return cls(basics.Self.read_lock(), "read", capability, start) @classmethod def make_write_desc(cls, capability: object, start: object) -> typing.Self: @@ -162,7 +162,7 @@ def make_write_desc(cls, capability: object, start: object) -> typing.Self: `start` is the path start unit. """ - return cls(foundation.Self.write_lock(), "write", capability, start) + return cls(basics.Self.write_lock(), "write", capability, start) selector: Callable[[_SatInfo], int] @@ -587,7 +587,7 @@ def _get_anal_graph(processor: Graph) -> DiGraph: type_checking.call( width_graph.add_edges_from, - (foundation.map_ex(edge, new_nodes) for edge in processor.edges), + (basics.map_ex(edge, new_nodes) for edge in processor.edges), ) return width_graph diff --git a/src/processor_utils/_port_defs.py b/src/processor_utils/_port_defs.py index 0a7c6c9..f023b4d 100644 --- a/src/processor_utils/_port_defs.py +++ b/src/processor_utils/_port_defs.py @@ -31,8 +31,8 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.86.1, python 3.11.7, Fedora release -# 39 (Thirty Nine) +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release +# 40 (Forty) # # notes: This is a private program. # @@ -42,7 +42,7 @@ import typing from typing import Any -from fastcore import foundation +from fastcore import basics from networkx import DiGraph _T = typing.TypeVar("_T") @@ -79,8 +79,8 @@ def __init__(self, processor: DiGraph) -> None: from. """ - self._in_ports, self._out_ports = foundation.maps( - foundation.Self(processor), tuple, [get_in_ports, get_out_ports] + self._in_ports, self._out_ports = basics.maps( + basics.Self(processor), tuple, [get_in_ports, get_out_ports] ) @property diff --git a/src/processor_utils/exception.py b/src/processor_utils/exception.py index b751fe2..2d37d5b 100644 --- a/src/processor_utils/exception.py +++ b/src/processor_utils/exception.py @@ -32,7 +32,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.0, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -42,8 +42,8 @@ from typing import Final from attr import field, frozen -from fastcore import foundation -from fastcore.foundation import mapt +from fastcore import basics +from fastcore.basics import mapt from pydash import spread from errors import ElementValue, ErrorElement, EXCEPTION, SimErrorBase @@ -278,8 +278,8 @@ def _make_elem_val(comp: ComponentInfo) -> ElementValue: *( attr_getter(comp) for attr_getter in [ - foundation.Self.reporting_name(), - foundation.Self.std_name(), + basics.Self.reporting_name(), + basics.Self.std_name(), ] ) ) diff --git a/src/processor_utils/units.py b/src/processor_utils/units.py index 0897fac..fbf0a86 100644 --- a/src/processor_utils/units.py +++ b/src/processor_utils/units.py @@ -43,7 +43,7 @@ from typing import Any, Final from attr import field, frozen -from fastcore import foundation +from fastcore import basics from container_utils import sorted_tuple @@ -63,7 +63,7 @@ def sorted_models(models: Iterable[Any]) -> tuple[Any, ...]: `models` are the models to sort. """ - return sorted_tuple(models, key=foundation.Self.name()) + return sorted_tuple(models, key=basics.Self.name()) @frozen @@ -123,8 +123,8 @@ def __eq__(self, other: Any) -> Any: ( attr_get_func(unit) for attr_get_func in [ - foundation.Self.model(), - foundation.Self.predecessors(), + basics.Self.model(), + basics.Self.predecessors(), ] ) for unit in [self, other] diff --git a/src/program_utils.py b/src/program_utils.py index 9180ec6..1f5e9fa 100644 --- a/src/program_utils.py +++ b/src/program_utils.py @@ -31,7 +31,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.91.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -45,7 +45,7 @@ from typing import Final from attr import field, frozen -from fastcore import foundation +from fastcore import basics import pydash import container_utils @@ -75,7 +75,7 @@ def __init__(self, msg_tmpl: str, line: object, instr: object) -> None: """ self._init_simple( msg_tmpl, - foundation.mapt( + basics.mapt( pydash.spread(errors.ErrorElement), [[self.INSTR_KEY, instr], [self.LINE_NUM_KEY, line]], ), @@ -123,7 +123,7 @@ def read_program(prog_file: Iterable[str]) -> list[ProgInstruction]: """ prog = enumerate(map(str.strip, prog_file), 1) - reg_registry = IndexedSet[_OperandInfo](foundation.Self.name()) + reg_registry = IndexedSet[_OperandInfo](basics.Self.name()) return [ _create_instr(line_no, line, reg_registry) for line_no, line in prog diff --git a/src/sim_services/__init__.py b/src/sim_services/__init__.py index 6faa1e6..d0a71bc 100644 --- a/src/sim_services/__init__.py +++ b/src/sim_services/__init__.py @@ -31,7 +31,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.0, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -54,7 +54,7 @@ from typing import Any, TypeVar from attr import field, frozen, mutable -from fastcore import foundation +from fastcore import basics import more_itertools from container_utils import BagValDict @@ -120,7 +120,7 @@ def _(self) -> dict[str, UnitModel]: self.processor_desc.in_ports, self.processor_desc.in_out_ports, map( - foundation.Self.model(), + basics.Self.model(), chain( self.processor_desc.out_ports, self.processor_desc.internal_units, @@ -595,9 +595,7 @@ def _fill_unit( """ mov_res = unit.fill_unit(util_info, mem_busy) _clr_src_units( - sorted( - mov_res.moved, key=foundation.Self.index_in_host(), reverse=True - ), + sorted(mov_res.moved, key=basics.Self.index_in_host(), reverse=True), util_info, ) return mov_res.mem_used @@ -612,7 +610,7 @@ def _get_out_ports(processor: ProcessorDesc) -> "map[str]": """ return map( - foundation.Self.name(), + basics.Self.name(), chain( processor.in_out_ports, (port.model for port in processor.out_ports), diff --git a/src/sim_services/_instr_sinks.py b/src/sim_services/_instr_sinks.py index 655ab60..79a197b 100644 --- a/src/sim_services/_instr_sinks.py +++ b/src/sim_services/_instr_sinks.py @@ -31,7 +31,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.91.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -47,7 +47,7 @@ import attr from attr import field, frozen -import fastcore.foundation +import fastcore.basics import more_itertools from container_utils import BagValDict @@ -311,7 +311,7 @@ def _donors(self) -> "map[str]": `self` is this unit sink. """ - return map(fastcore.foundation.Self.name(), self.unit.predecessors) + return map(fastcore.basics.Self.name(), self.unit.predecessors) unit: processor_utils.units.FuncUnit diff --git a/src/type_checking.py b/src/type_checking.py index ec03e81..c51f091 100644 --- a/src/type_checking.py +++ b/src/type_checking.py @@ -32,7 +32,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.91.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -43,7 +43,7 @@ import typing from typing import Any -import fastcore.foundation +import fastcore.basics _T = typing.TypeVar("_T") @@ -67,9 +67,9 @@ def map_ex(seq: Any, map_func: Any, _: type[_T]) -> "map[_T]": `map_func` is the mapping function. `_` is the type of elements in the resulting mapped generator. I'm casting to map[_T] due to a missing explicit type hint for the - return type of the fastcore.foundation.map_ex function. + return type of the fastcore.basics.map_ex function. """ return typing.cast( - "map[_T]", fastcore.foundation.map_ex(seq, map_func, gen=True) + "map[_T]", fastcore.basics.map_ex(seq, map_func, gen=True) ) diff --git a/tests/test_caps.py b/tests/test_caps.py index 1c612fc..d68aa53 100755 --- a/tests/test_caps.py +++ b/tests/test_caps.py @@ -32,7 +32,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -41,7 +41,7 @@ from logging import WARNING -from fastcore import foundation +from fastcore import basics import pytest from pytest import mark, raises @@ -107,8 +107,8 @@ def test_unit_with_non_positive_width_raises_BadWidthError( chk_points = ( test_utils.ValInStrCheck(val_getter(ex_chk.value), exp_val) for val_getter, exp_val in [ - (foundation.Self.unit(), "full system"), - (foundation.Self.width(), bad_width), + (basics.Self.unit(), "full system"), + (basics.Self.width(), bad_width), ] ) test_utils.chk_error(chk_points, ex_chk.value) diff --git a/tests/test_compiler.py b/tests/test_compiler.py index 316b290..c82450f 100755 --- a/tests/test_compiler.py +++ b/tests/test_compiler.py @@ -32,7 +32,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -42,7 +42,7 @@ import itertools from logging import WARNING -from fastcore import foundation +from fastcore import basics import pytest from pytest import mark, raises @@ -234,8 +234,8 @@ def _chk_syn_err(syn_err, line_num, instr): chk_points = ( test_utils.ValInStrCheck(val_getter(syn_err), exp_val) for val_getter, exp_val in [ - (foundation.Self.instr(), instr), - (foundation.Self.line(), line_num), + (basics.Self.instr(), instr), + (basics.Self.line(), line_num), ] ) test_utils.chk_error(chk_points, syn_err) diff --git a/tests/test_isa.py b/tests/test_isa.py index 8da2497..9f507dd 100755 --- a/tests/test_isa.py +++ b/tests/test_isa.py @@ -32,7 +32,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -40,7 +40,7 @@ ############################################################ import logging -from fastcore import foundation +from fastcore import basics import pytest from pytest import raises @@ -70,8 +70,8 @@ def test_two_instructions_with_same_name_raise_DupElemError(self): chk_points = ( ValInStrCheck(elem_getter(ex_chk.value), unit) for elem_getter, unit in [ - (foundation.Self.new_element(), "add"), - (foundation.Self.old_element(), "ADD"), + (basics.Self.new_element(), "add"), + (basics.Self.old_element(), "ADD"), ] ) chk_error(chk_points, ex_chk.value) diff --git a/tests/test_reg_access.py b/tests/test_reg_access.py index 8218db9..99f5280 100755 --- a/tests/test_reg_access.py +++ b/tests/test_reg_access.py @@ -32,14 +32,14 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.86.1, python 3.11.7, Fedora release -# 39 (Thirty Nine) +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release +# 40 (Forty) # # notes: This is a private program. # ############################################################ -from fastcore.foundation import mapt +from fastcore.basics import mapt import pydash import pytest from pytest import mark diff --git a/tests/test_sanity.py b/tests/test_sanity.py index 5778f99..03b56e6 100755 --- a/tests/test_sanity.py +++ b/tests/test_sanity.py @@ -32,7 +32,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -40,7 +40,7 @@ ############################################################ from attr import frozen -from fastcore import foundation +from fastcore import basics import networkx import pytest from pytest import mark, raises @@ -235,8 +235,8 @@ def test_unconsumed_capabilitiy_raises_BlockedCapError(self): "inputPortWithUnconsumedCapability.yaml", ) chk_params = [ - ("Capability", foundation.Self.capability(), "Capability MEM"), - ("port", foundation.Self.port(), "port input"), + ("Capability", basics.Self.capability(), "Capability MEM"), + ("port", basics.Self.port(), "port input"), ] chk_error( ( @@ -342,8 +342,8 @@ def _get_test_units(proc_desc, lock_prop): **{prop: True for prop in [unit_lock, lock_prop]}, } for unit_getter, unit_lock in [ - (foundation.Self.in_unit(), UNIT_RLOCK_KEY), - (foundation.Self.out_unit(), UNIT_WLOCK_KEY), + (basics.Self.in_unit(), UNIT_RLOCK_KEY), + (basics.Self.out_unit(), UNIT_WLOCK_KEY), ] ] diff --git a/tests/test_sim.py b/tests/test_sim.py index 082ac54..86db6c1 100755 --- a/tests/test_sim.py +++ b/tests/test_sim.py @@ -32,7 +32,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.0, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -41,7 +41,7 @@ from itertools import chain, starmap -import fastcore.foundation +import fastcore.basics import more_itertools import pydash import pytest @@ -165,7 +165,7 @@ def test_earlier_instructions_are_propagated_first(self): in_units, ) assert simulate( - fastcore.foundation.mapt( + fastcore.basics.mapt( pydash.spread(HwInstruction), [[[], "R12", "MEM"], [["R11", "R15"], "R14", "ALU"]], ), diff --git a/tests/test_units.py b/tests/test_units.py index fc10f26..c0a3f3e 100755 --- a/tests/test_units.py +++ b/tests/test_units.py @@ -32,14 +32,14 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.0, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. # ############################################################ -from fastcore import foundation +from fastcore import basics import pytest import test_utils @@ -78,8 +78,8 @@ def test_two_units_with_same_name_raise_DupElemError( chk_points = ( test_utils.ValInStrCheck(elem_getter(ex_chk.value), unit) for elem_getter, unit in [ - (foundation.Self.new_element(), dup_unit), - (foundation.Self.old_element(), "full system"), + (basics.Self.new_element(), dup_unit), + (basics.Self.old_element(), "full system"), ] ) test_utils.chk_error(chk_points, ex_chk.value) From ed7eb9133e8dd779fcb1d7b2b96d2811448272fa Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 04:23:45 +0100 Subject: [PATCH 03/12] Drop MutableSet usage --- src/reg_access.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/reg_access.py b/src/reg_access.py index 24f93d6..6d7808c 100644 --- a/src/reg_access.py +++ b/src/reg_access.py @@ -31,14 +31,14 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.91.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. # ############################################################ -from collections import abc +import collections.abc import enum from enum import auto import typing @@ -60,10 +60,12 @@ class AccessGroup: access_type: object - reqs: abc.MutableSet[object] = field(converter=set, factory=set) + reqs: set[object] = field(converter=set, factory=set) -def _rev_groups(lst: abc.Reversible[AccessGroup]) -> list[AccessGroup]: +def _rev_groups( + lst: collections.abc.Reversible[AccessGroup], +) -> list[AccessGroup]: """Return the reversed list of the given one. `lst` is the list to reverse. From a088300c4f8a63c44db785ad0b9e5a5dd1afbd24 Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 04:30:38 +0100 Subject: [PATCH 04/12] Fix some stupid pylance errors --- src/container_utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/container_utils.py b/src/container_utils.py index e3ff05e..2ee1c7f 100644 --- a/src/container_utils.py +++ b/src/container_utils.py @@ -31,7 +31,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.91.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -201,7 +201,9 @@ def __getitem__(self, key: _KT) -> list[_VT]: `key` is the key to retrieve whose list. """ - return self._dict[key] + # Pylance isn't smart enough to figure out that self._dict[key] + # has the same type as list[_VT]. + return typing.cast(list[_VT], self._dict[key]) def __len__(self) -> int: """Count the number of elements in this dictionary. From 5646724b5c53f35768b17462aa7cc41a0f0fa999 Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 05:00:21 +0100 Subject: [PATCH 05/12] Fix problems with attrs converters and factories --- src/container_utils.py | 5 ++++- src/processor_utils/units.py | 15 +++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/container_utils.py b/src/container_utils.py index 2ee1c7f..4d6dbbd 100644 --- a/src/container_utils.py +++ b/src/container_utils.py @@ -253,6 +253,9 @@ def _format_elems(self) -> str: starmap(lambda key, val_lst: f"{key!r}: {val_lst}", elems) ) + # defaultdict isn't strictly required here, but pylance can't + # understand that factory products are passed anyway to the + # converter. _dict: defaultdict[_KT, list[_VT]] = field( - converter=_val_lst_dict, factory=dict + converter=_val_lst_dict, factory=defaultdict ) diff --git a/src/processor_utils/units.py b/src/processor_utils/units.py index fbf0a86..3c7a932 100644 --- a/src/processor_utils/units.py +++ b/src/processor_utils/units.py @@ -38,7 +38,7 @@ # ############################################################ -from collections.abc import Iterable +import collections.abc import operator from typing import Any, Final @@ -57,7 +57,7 @@ UNIT_WIDTH_KEY: Final = "width" -def sorted_models(models: Iterable[Any]) -> tuple[Any, ...]: +def sorted_models(models: collections.abc.Iterable[Any]) -> tuple[Any, ...]: """Create a sorted list of the given models. `models` are the models to sort. @@ -75,15 +75,6 @@ class LockInfo: wr_lock: object -def _sorted_caps(caps: Iterable[Any]) -> tuple[Any, ...]: - """Create a sorted list of the given capabilities. - - `caps` are the capabilities to sort. - - """ - return sorted_tuple(caps) - - @frozen class UnitModel: """Functional unit model""" @@ -101,7 +92,7 @@ def needs_mem(self, cap: object) -> bool: width: int - capabilities: tuple[str, ...] = field(converter=_sorted_caps) + capabilities: tuple[str, ...] = field(converter=sorted_tuple) lock_info: LockInfo From 8f0a28e87d99c361f28f6ebdc91e5d20993dd359 Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 05:11:08 +0100 Subject: [PATCH 06/12] Use attr.frozen with RegAccQBuilder --- src/reg_access.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/reg_access.py b/src/reg_access.py index 6d7808c..6857e4c 100644 --- a/src/reg_access.py +++ b/src/reg_access.py @@ -111,17 +111,10 @@ def dequeue(self, req_owner: object) -> None: _queue: list[AccessGroup] = field(converter=_rev_groups) +@frozen class RegAccQBuilder: """Access request queue builder""" - def __init__(self) -> None: - """Create an access queue builder. - - `self` is this access queue builder. - - """ - self._queue: list[AccessGroup] = [] - def append(self, req_type: AccessType, req_owner: int) -> None: """Append a new read request to the built queue. @@ -155,3 +148,5 @@ def _can_merge(self, req_type: object) -> bool: and typing.cast(bool, self._queue) and self._queue[-1].access_type == AccessType.READ ) + + _queue: list[AccessGroup] = field(factory=list, init=False) From 2d4b1490d0193e070861c68a9c2b7b89d6b06cb9 Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 06:05:53 +0100 Subject: [PATCH 07/12] Update a constructor signature --- src/processor_utils/__init__.py | 2 +- src/processor_utils/_port_defs.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/processor_utils/__init__.py b/src/processor_utils/__init__.py index f20c1bb..219d7be 100644 --- a/src/processor_utils/__init__.py +++ b/src/processor_utils/__init__.py @@ -626,7 +626,7 @@ def _prep_proc_desc(processor: DiGraph) -> None: """ _checks.chk_cycles(processor) - port_info = _port_defs.PortGroup(processor) + port_info = _port_defs.PortGroup(basics.Self(processor)) _optimization.clean_struct(processor) _optimization.rm_empty_units(processor) _optimization.chk_terminals(processor, port_info) diff --git a/src/processor_utils/_port_defs.py b/src/processor_utils/_port_defs.py index f023b4d..8cdc5fe 100644 --- a/src/processor_utils/_port_defs.py +++ b/src/processor_utils/_port_defs.py @@ -42,7 +42,7 @@ import typing from typing import Any -from fastcore import basics +import fastcore.basics from networkx import DiGraph _T = typing.TypeVar("_T") @@ -71,16 +71,16 @@ def get_out_ports(processor: DiGraph) -> Generator[Any, None, None]: class PortGroup: """Port group information""" - def __init__(self, processor: DiGraph) -> None: - """Extract port information from the given processor. + def __init__(self, proc_supplier: object) -> None: + """Extract port information from the indicated processor. `self` is this port group. - `processor` is the processor to extract port group information - from. + `proc_supplier` is a functor that takes a function and calls it + with a pre-configured processor. """ - self._in_ports, self._out_ports = basics.maps( - basics.Self(processor), tuple, [get_in_ports, get_out_ports] + self._in_ports, self._out_ports = fastcore.basics.maps( + proc_supplier, tuple, [get_in_ports, get_out_ports] ) @property From a64640fa53e6dd2b6039be0dc0d896da4ebbc55d Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 06:14:30 +0100 Subject: [PATCH 08/12] Use attr.frozen with PortGroup --- src/processor_utils/__init__.py | 4 +++- src/processor_utils/_port_defs.py | 32 ++++++++++++++----------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/processor_utils/__init__.py b/src/processor_utils/__init__.py index 219d7be..7e6f446 100644 --- a/src/processor_utils/__init__.py +++ b/src/processor_utils/__init__.py @@ -626,7 +626,9 @@ def _prep_proc_desc(processor: DiGraph) -> None: """ _checks.chk_cycles(processor) - port_info = _port_defs.PortGroup(basics.Self(processor)) + port_info = _port_defs.PortGroup( # type: ignore[call-arg] + basics.Self(processor) + ) _optimization.clean_struct(processor) _optimization.rm_empty_units(processor) _optimization.chk_terminals(processor, port_info) diff --git a/src/processor_utils/_port_defs.py b/src/processor_utils/_port_defs.py index 8cdc5fe..a43123d 100644 --- a/src/processor_utils/_port_defs.py +++ b/src/processor_utils/_port_defs.py @@ -38,10 +38,12 @@ # ############################################################ +import collections.abc from collections.abc import Generator, Iterable import typing from typing import Any +import attr import fastcore.basics from networkx import DiGraph @@ -68,6 +70,7 @@ def get_out_ports(processor: DiGraph) -> Generator[Any, None, None]: return _get_ports(processor.out_degree) +@attr.frozen class PortGroup: """Port group information""" @@ -79,27 +82,20 @@ def __init__(self, proc_supplier: object) -> None: with a pre-configured processor. """ - self._in_ports, self._out_ports = fastcore.basics.maps( - proc_supplier, tuple, [get_in_ports, get_out_ports] + # Pylance and pylint can't detect __attrs_init__ as an injected + # method. + # pylint: disable-next=no-member + self.__attrs_init__( # type: ignore[attr-defined] + *( + fastcore.basics.maps( + proc_supplier, tuple, [get_in_ports, get_out_ports] + ) + ) ) - @property - def in_ports(self) -> Any: - """Input ports + in_ports: collections.abc.Collection[object] - `self` is this port group. - - """ - return self._in_ports - - @property - def out_ports(self) -> Any: - """Output ports - - `self` is this port group. - - """ - return self._out_ports + out_ports: Iterable[object] def _get_ports(degrees: Iterable[Iterable[_T]]) -> Generator[_T, None, None]: From 3b3d83b3a9b72fd2611055b53f879be75cb9826d Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 06:56:18 +0100 Subject: [PATCH 09/12] Abstract a linting workaround --- src/errors.py | 9 +++------ src/processor_utils/_port_defs.py | 8 ++++---- src/type_checking.py | 12 ++++++++++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/errors.py b/src/errors.py index 4bc5122..6a3cc8a 100644 --- a/src/errors.py +++ b/src/errors.py @@ -49,6 +49,8 @@ from attr import frozen import fastcore.basics +import type_checking + # Attrs doesn't honor class variables annotated with typing.Final(albeit # being mandated by PEP-591) and instead still treats them as instance # attributes. That's why I'm using auto_attribs=False and marking the @@ -119,12 +121,7 @@ def _init( ) ) val_extractor = fastcore.basics.Self.val().stored() - # Pylance and pylint can't detect __attrs_init__ as an injected - # method. - # pylint: disable-next=no-member - self.__attrs_init__( # type: ignore[attr-defined] - *(map(val_extractor, elems)) - ) + type_checking.attrs_init(self, *(map(val_extractor, elems))) def _init_simple( self, diff --git a/src/processor_utils/_port_defs.py b/src/processor_utils/_port_defs.py index a43123d..78dc8ac 100644 --- a/src/processor_utils/_port_defs.py +++ b/src/processor_utils/_port_defs.py @@ -47,6 +47,8 @@ import fastcore.basics from networkx import DiGraph +import type_checking + _T = typing.TypeVar("_T") @@ -82,10 +84,8 @@ def __init__(self, proc_supplier: object) -> None: with a pre-configured processor. """ - # Pylance and pylint can't detect __attrs_init__ as an injected - # method. - # pylint: disable-next=no-member - self.__attrs_init__( # type: ignore[attr-defined] + type_checking.attrs_init( + self, *( fastcore.basics.maps( proc_supplier, tuple, [get_in_ports, get_out_ports] diff --git a/src/type_checking.py b/src/type_checking.py index c51f091..41508fa 100644 --- a/src/type_checking.py +++ b/src/type_checking.py @@ -48,6 +48,18 @@ _T = typing.TypeVar("_T") +def attrs_init(attrs_obj: Any, *attr_vals: object) -> None: + """Initialize the attributes of an object. + + `attrs_obj` is an attrs-defined object. + `attr_vals` are the desired values of the object attributes. + Pylance and pylint can't detect __attrs_init__ as an injected + method. + + """ + attrs_obj.__attrs_init__(*attr_vals) + + def call(func: collections.abc.Callable[..., _T], *args: object) -> _T: """Call the given functor with arguments. From 6e4049bce9e3cb9f8c74ee342c21abcb1f09110f Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 07:24:18 +0100 Subject: [PATCH 10/12] Make 100% coverage gating --- test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.sh b/test.sh index 2115d05..4f0c448 100755 --- a/test.sh +++ b/test.sh @@ -27,7 +27,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.86.1, Fedora release 39 (Thirty Nine) +# environment: Visual Studio Code 1.95.1, Fedora release 40 (Forty) # # notes: This is a private program. # @@ -36,7 +36,7 @@ set -e # just running a sample test module to make sure test modules are executable on # their own tests/test_containers.py -pytest --cov src --flake8 --pylint $* +pytest --cov src --cov-fail-under 100 --flake8 --pylint $* black --check . PYRIGHT_PYTHON_IGNORE_WARNINGS=1 pyright cd src From 7b6ba50022bdcf19a5b6065c20697ec19113ab8e Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 07:25:47 +0100 Subject: [PATCH 11/12] Ditch a type checking workaround Because: fastcore.basics.Self can do the trick. --- src/container_utils.py | 4 ++-- src/processor_utils/__init__.py | 11 ++++++----- src/processor_utils/_checks.py | 6 ++---- src/processor_utils/_optimization.py | 6 +++--- src/program_utils.py | 5 ++--- src/type_checking.py | 13 ------------- 6 files changed, 15 insertions(+), 30 deletions(-) diff --git a/src/container_utils.py b/src/container_utils.py index 4d6dbbd..b9e4652 100644 --- a/src/container_utils.py +++ b/src/container_utils.py @@ -48,11 +48,11 @@ from typing import Any, Generic, Optional, TypeVar from attr import field, frozen +import fastcore.basics import more_itertools import pydash from str_utils import format_obj -import type_checking _KT = TypeVar("_KT") _T = TypeVar("_T") @@ -186,7 +186,7 @@ def __eq__(self, other: Any) -> Any: assert type(other) is type(self) other_items = tuple(other.items()) lst_pairs = ( - (type_checking.call(sorted, lst) for lst in [val_lst, self[key]]) + (fastcore.basics.Self(lst)(sorted) for lst in [val_lst, self[key]]) for key, val_lst in other_items ) item_lst_pair: list[collections.abc.Sized] = [self, other_items] diff --git a/src/processor_utils/__init__.py b/src/processor_utils/__init__.py index 7e6f446..807e223 100644 --- a/src/processor_utils/__init__.py +++ b/src/processor_utils/__init__.py @@ -66,7 +66,6 @@ from errors import UndefElemError from str_utils import ICaseString import type_checking -from type_checking import call from . import _checks, _optimization, _port_defs, units from .exception import BadEdgeError, BadWidthError, DupElemError from .units import ( @@ -298,7 +297,7 @@ def _add_rev_edges(graph: Graph) -> None: for pred in unit.predecessors if pred.name in graph ), - call(graph.nodes, _UNIT_KEY), + basics.Self(_UNIT_KEY)(graph.nodes), ) graph.add_edges_from(chain.from_iterable(edges)) @@ -464,7 +463,9 @@ def _get_proc_units(graph: DiGraph) -> Generator[FuncUnit, None, None]: unit: _get_unit_entry(unit, graph.nodes[unit]) for unit in graph } return ( - call(FuncUnit, unit_map[name], _get_preds(graph, name, unit_map)) + basics.Self(unit_map[name], _get_preds(graph, name, unit_map))( + FuncUnit + ) for name in graph ) @@ -730,8 +731,8 @@ def _make_processor(proc_graph: DiGraph) -> ProcessorDesc: case _: in_out_ports.append(unit.model) - return call( - ProcessorDesc, in_ports, out_ports, in_out_ports, internal_units + return basics.Self(in_ports, out_ports, in_out_ports, internal_units)( + ProcessorDesc ) diff --git a/src/processor_utils/_checks.py b/src/processor_utils/_checks.py index 1f35a2b..6f7651b 100644 --- a/src/processor_utils/_checks.py +++ b/src/processor_utils/_checks.py @@ -59,7 +59,6 @@ import networkx from networkx import DiGraph, Graph -import type_checking from . import cap_anal_utils, exception, _port_defs, units from .exception import BlockedCapError, ComponentInfo, PathLockError from .units import UNIT_CAPS_KEY, UNIT_WIDTH_KEY @@ -585,9 +584,8 @@ def _get_anal_graph(processor: Graph) -> DiGraph: for idx, unit in hw_units: _update_graph(idx, unit, processor, width_graph, new_nodes) - type_checking.call( - width_graph.add_edges_from, - (basics.map_ex(edge, new_nodes) for edge in processor.edges), + basics.Self(basics.map_ex(edge, new_nodes) for edge in processor.edges)( + width_graph.add_edges_from ) return width_graph diff --git a/src/processor_utils/_optimization.py b/src/processor_utils/_optimization.py index d1919ef..894e850 100644 --- a/src/processor_utils/_optimization.py +++ b/src/processor_utils/_optimization.py @@ -31,7 +31,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.93.1, python 3.12.6, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -41,10 +41,10 @@ from collections import abc from logging import warning +import fastcore.basics import networkx from networkx import DiGraph, Graph -import type_checking from .exception import DeadInputError from . import _port_defs from .units import UNIT_CAPS_KEY @@ -93,7 +93,7 @@ def rm_empty_units(processor: Graph) -> None: The function removes units with no capabilities from the processor. """ - unit_entries = tuple(type_checking.call(processor.nodes, UNIT_CAPS_KEY)) + unit_entries = tuple(fastcore.basics.Self(UNIT_CAPS_KEY)(processor.nodes)) for unit, capabilities in unit_entries: if not capabilities: diff --git a/src/program_utils.py b/src/program_utils.py index 1f5e9fa..4edae9f 100644 --- a/src/program_utils.py +++ b/src/program_utils.py @@ -54,7 +54,6 @@ from errors import UndefElemError from program_defs import HwInstruction, ProgInstruction from str_utils import ICaseString -import type_checking _T = typing.TypeVar("_T") @@ -163,8 +162,8 @@ def _create_instr( """ src_line_info = _get_line_parts(line_num, line_txt) dst, *sources = _get_operands(src_line_info, line_num, reg_registry) - return type_checking.call( - ProgInstruction, sources, dst, src_line_info.instruction, line_num + return basics.Self(sources, dst, src_line_info.instruction, line_num)( + ProgInstruction ) diff --git a/src/type_checking.py b/src/type_checking.py index 41508fa..c171e0d 100644 --- a/src/type_checking.py +++ b/src/type_checking.py @@ -39,7 +39,6 @@ # ############################################################ -import collections.abc import typing from typing import Any @@ -60,18 +59,6 @@ def attrs_init(attrs_obj: Any, *attr_vals: object) -> None: attrs_obj.__attrs_init__(*attr_vals) -def call(func: collections.abc.Callable[..., _T], *args: object) -> _T: - """Call the given functor with arguments. - - `func` is the function to call. - `args` are the arguments to call the function with. - This function is used whenever pylance can't match arguments with - the inferred types of parameters. - - """ - return func(*args) - - def map_ex(seq: Any, map_func: Any, _: type[_T]) -> "map[_T]": """Map an iterable using a mapping function. From 96c335f68a1c1270a5f7a71147a770a4a9eb3abf Mon Sep 17 00:00:00 2001 From: Mohammed El-Afifi Date: Sun, 3 Nov 2024 07:37:36 +0100 Subject: [PATCH 12/12] Use attr.frozen with ValInStrCheck --- tests/test_utils.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index a7bf256..abaa030 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -31,7 +31,7 @@ # # author: Mohammed El-Afifi (ME) # -# environment: Visual Studio Code 1.89.1, python 3.11.9, Fedora release +# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release # 40 (Forty) # # notes: This is a private program. @@ -41,6 +41,7 @@ from itertools import starmap from os.path import join +import attr import yaml import test_env @@ -50,6 +51,7 @@ from processor_utils.units import LockInfo, UnitModel import program_utils import sim_services.sim_defs +import type_checking TEST_DATA_DIR = join(test_env.TEST_DIR, "data") @@ -221,6 +223,7 @@ def read_prog_file(file_name): return program_utils.read_program(prog_file) +@attr.frozen class ValInStrCheck: """Verification point for checking a string contains a value""" @@ -234,7 +237,7 @@ def __init__(self, real_val, exp_val): """ assert real_val == exp_val - self._value = exp_val + type_checking.attrs_init(self, exp_val) def check(self, msg, start_index): """Check that the message contains the associated value. @@ -250,6 +253,8 @@ def check(self, msg, start_index): assert start_index >= 0 return start_index + _value: object + def _load_yaml(test_dir, file_name): """Read a test YAML file.