simplify type hints

This commit is contained in:
jan 2022-02-28 23:37:48 -08:00
parent f4c2357737
commit bfee560693

View File

@ -33,15 +33,15 @@ class DeviceLibrary:
Each device is cached the first time it is used. The cache can Each device is cached the first time it is used. The cache can
be disabled by setting the `enable_cache` attribute to `False`. be disabled by setting the `enable_cache` attribute to `False`.
""" """
generators: Dict[str, Callable[[], 'Device']] generators: Dict[str, Callable[[], Device]]
cache: Dict[Union[str, Tuple[str, str]], 'Device'] cache: Dict[Union[str, Tuple[str, str]], Device]
enable_cache: bool = True enable_cache: bool = True
def __init__(self) -> None: def __init__(self) -> None:
self.generators = {} self.generators = {}
self.cache = {} self.cache = {}
def __setitem__(self, key: str, value: Callable[[], 'Device']) -> None: def __setitem__(self, key: str, value: Callable[[], Device]) -> None:
self.generators[key] = value self.generators[key] = value
if key in self.cache: if key in self.cache:
del self.cache[key] del self.cache[key]
@ -51,7 +51,7 @@ class DeviceLibrary:
if key in self.cache: if key in self.cache:
del self.cache[key] del self.cache[key]
def __getitem__(self, key: str) -> 'Device': def __getitem__(self, key: str) -> Device:
if self.enable_cache and key in self.cache: if self.enable_cache and key in self.cache:
logger.debug(f'found {key} in cache') logger.debug(f'found {key} in cache')
return self.cache[key] return self.cache[key]
@ -70,16 +70,16 @@ class DeviceLibrary:
def keys(self) -> Iterator[str]: def keys(self) -> Iterator[str]:
return iter(self.generators.keys()) return iter(self.generators.keys())
def values(self) -> Iterator['Device']: def values(self) -> Iterator[Device]:
return iter(self[key] for key in self.keys()) return iter(self[key] for key in self.keys())
def items(self) -> Iterator[Tuple[str, 'Device']]: def items(self) -> Iterator[Tuple[str, Device]]:
return iter((key, self[key]) for key in self.keys()) return iter((key, self[key]) for key in self.keys())
def __repr__(self) -> str: def __repr__(self) -> str:
return '<DeviceLibrary with keys ' + repr(list(self.generators.keys())) + '>' return '<DeviceLibrary with keys ' + repr(list(self.generators.keys())) + '>'
def set_const(self, const: 'Device') -> None: def set_const(self, const: Device) -> None:
""" """
Convenience function to avoid having to manually wrap Convenience function to avoid having to manually wrap
already-generated Device objects into callables. already-generated Device objects into callables.
@ -140,8 +140,8 @@ class DeviceLibrary:
def add_device( def add_device(
self, self,
name: str, name: str,
fn: Callable[[], 'Device'], fn: Callable[[], Device],
dev2pat: Callable[['Device'], 'Pattern'], dev2pat: Callable[[Device], Pattern],
prefix: str = '', prefix: str = '',
) -> None: ) -> None:
""" """
@ -172,7 +172,7 @@ class DeviceLibrary:
a second device with name `name` is also added (containing only a second device with name `name` is also added (containing only
this one). this one).
""" """
def build_dev() -> 'Device': def build_dev() -> Device:
dev = fn() dev = fn()
dev.pattern = dev2pat(dev) dev.pattern = dev2pat(dev)
dev.pattern.rename(prefix + name) dev.pattern.rename(prefix + name)
@ -203,7 +203,7 @@ class LibDeviceLibrary(DeviceLibrary):
DeviceLibrary.__init__(self) DeviceLibrary.__init__(self)
self.underlying = Library() self.underlying = Library()
def __setitem__(self, key: str, value: Callable[[], 'Device']) -> None: def __setitem__(self, key: str, value: Callable[[], Device]) -> None:
self.generators[key] = value self.generators[key] = value
if key in self.cache: if key in self.cache:
del self.cache[key] del self.cache[key]
@ -229,7 +229,7 @@ class LibDeviceLibrary(DeviceLibrary):
def add_library( def add_library(
self: L, self: L,
lib: Library, lib: Library,
pat2dev: Callable[['Pattern'], 'Device'], pat2dev: Callable[[Pattern], Device],
use_ours: Callable[[Union[str, Tuple[str, str]]], bool] = lambda name: False, use_ours: Callable[[Union[str, Tuple[str, str]]], bool] = lambda name: False,
use_theirs: Callable[[Union[str, Tuple[str, str]]], bool] = lambda name: False, use_theirs: Callable[[Union[str, Tuple[str, str]]], bool] = lambda name: False,
) -> L: ) -> L: