From 82384ca6ed9e098bea703eff3594bbeb47f96e98 Mon Sep 17 00:00:00 2001 From: TheZoq2 <frans.skarman@protonmail.com> Date: Mon, 13 Feb 2023 13:28:08 +0100 Subject: [PATCH] Allow string literals to be passed to Name --- b_asic/graph_component.py | 15 +++++++++++++-- b_asic/special_operations.py | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/b_asic/graph_component.py b/b_asic/graph_component.py index 86018c8d..b889a3fc 100644 --- a/b_asic/graph_component.py +++ b/b_asic/graph_component.py @@ -7,9 +7,20 @@ Contains the base for all components with an ID in a signal flow graph. from abc import ABC, abstractmethod from collections import deque from copy import copy, deepcopy -from typing import Any, Dict, Generator, Iterable, Mapping, NewType, cast +from typing import Any, Dict, Generator, Iterable, Mapping, NewType, TypeAlias, cast +import sys + + +# We want to be able to initialize Name with String literals, but still have the +# benefit of static type checking that we don't pass non-names to name locations. +# However, until python 3.11 a string literal type was not available. In those versions, +# we'll fall back on just aliasing `str` => Name. +if sys.version_info >= (3, 11): + from typing import LiteralString + Name: TypeAlias = NewType("Name", str) | LiteralString +else: + Name = str -Name = NewType("Name", str) TypeName = NewType("TypeName", str) GraphID = NewType("GraphID", str) GraphIDNumber = NewType("GraphIDNumber", int) diff --git a/b_asic/special_operations.py b/b_asic/special_operations.py index c0609dd5..ee053221 100644 --- a/b_asic/special_operations.py +++ b/b_asic/special_operations.py @@ -28,12 +28,12 @@ class Input(AbstractOperation): _execution_time = 0 - def __init__(self, name: Name = Name("")): + def __init__(self, name: Name = ""): """Construct an Input operation.""" super().__init__( input_count=0, output_count=1, - name=Name(name), + name=name, latency_offsets={"out0": 0}, ) self.set_param("value", 0) -- GitLab