From abef8771db5f204cc304799bb8772756432483d1 Mon Sep 17 00:00:00 2001 From: jan Date: Fri, 7 Apr 2023 16:48:40 -0700 Subject: [PATCH] fixes to subtree and lshift, as well as some cast() improvements --- masque/library.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/masque/library.py b/masque/library.py index 4c12125..c4cb41e 100644 --- a/masque/library.py +++ b/masque/library.py @@ -142,6 +142,7 @@ class Library(Mapping[str, 'Pattern'], metaclass=ABCMeta): """ Return a new `Library`, containing only the specified patterns and the patterns they reference (recursively). + Dangling references do not cause an error. Args: tops: Name(s) of patterns to keep @@ -152,7 +153,7 @@ class Library(Mapping[str, 'Pattern'], metaclass=ABCMeta): if isinstance(tops, str): tops = (tops,) - keep: set[str] = self.referenced_patterns(tops) - set((None,)) # type: ignore + keep = cast(set[str], self.referenced_patterns(tops) - set((None,))) keep |= set(tops) filtered = {kk: vv for kk, vv in self.items() if kk in keep} @@ -251,7 +252,7 @@ class Library(Mapping[str, 'Pattern'], metaclass=ABCMeta): flatten_single(top) assert None not in flattened.values() - return flattened # type: ignore + return cast(dict[str, 'Pattern'], flattened) def get_name( self, @@ -592,8 +593,11 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta) if len(other) == 1: name = next(iter(other)) else: + if not isinstance(other, Library): + other = WrapROLibrary(other) + tops = other.tops() - if len(other.tops()) > 1: + if len(tops) > 1: raise LibraryError('Received a library containing multiple topcells!') name = tops[0] @@ -762,6 +766,7 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta) """ Return a new `Library`, containing only the specified patterns and the patterns they reference (recursively). + Dangling references do not cause an error. Args: tops: Name(s) of patterns to keep @@ -772,11 +777,11 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta) if isinstance(tops, str): tops = (tops,) - keep: set[str] = self.referenced_patterns(tops) - set((None,)) # type: ignore + keep = cast(set[str], self.referenced_patterns(tops) - set((None,))) keep |= set(tops) new = type(self)() - for key in keep: + for key in keep - set(self.keys()): new._merge(key, self, key) return new