1- from typing import Dict
2- from typing import List
3- from typing import Union
4-
51from .model import Block
62from .model import DuplicateBlockKeyBlock
73from .model import Entry
@@ -19,7 +15,7 @@ class Library:
1915
2016 def __init__ (
2117 self ,
22- blocks : Union [ List [ Block ], None ] = None ,
18+ blocks : list [ Block ] | None = None ,
2319 fail_on_duplicate_key : bool = True ,
2420 ):
2521 self ._blocks = []
@@ -28,7 +24,7 @@ def __init__(
2824 if blocks is not None :
2925 self .add (blocks , fail_on_duplicate_key = fail_on_duplicate_key )
3026
31- def add (self , blocks : Union [ List [ Block ], Block ] , fail_on_duplicate_key : bool = True ):
27+ def add (self , blocks : list [ Block ] | Block , fail_on_duplicate_key : bool = True ):
3228 """Add blocks to library.
3329
3430 The adding is key-safe, i.e., it is made sure that no duplicate keys are added
@@ -64,7 +60,7 @@ def add(self, blocks: Union[List[Block], Block], fail_on_duplicate_key: bool = T
6460 block = self ._add_to_dicts (block )
6561 self ._blocks .append (block )
6662
67- def _find_duplicate_keys (self , blocks : List [Block ]) -> List [str ]:
63+ def _find_duplicate_keys (self , blocks : list [Block ]) -> list [str ]:
6864 """Keys of blocks that would become duplicates when added to the library."""
6965 duplicate_keys = []
7066 seen_entry_keys = set (self ._entries_by_key )
@@ -91,7 +87,7 @@ def _block_index(self, block: Block) -> int:
9187 # No identity match; fall back to equality (raises ValueError if not found).
9288 return self ._blocks .index (block )
9389
94- def remove (self , blocks : Union [ List [ Block ], Block ] ):
90+ def remove (self , blocks : list [ Block ] | Block ):
9591 """Remove blocks from library.
9692
9793 If equal duplicate blocks exist in the library, the exact (identical)
@@ -143,9 +139,7 @@ def replace(self, old_block: Block, new_block: Block, fail_on_duplicate_key: boo
143139 raise ValueError ("Duplicate key found." )
144140
145141 @staticmethod
146- def _cast_to_duplicate (
147- prev_block_with_same_key : Union [Entry , String ], duplicate : Union [Entry , String ]
148- ):
142+ def _cast_to_duplicate (prev_block_with_same_key : Entry | String , duplicate : Entry | String ):
149143 if not (
150144 isinstance (prev_block_with_same_key , type (duplicate ))
151145 or isinstance (duplicate , type (prev_block_with_same_key ))
@@ -196,44 +190,44 @@ def _add_to_dicts(self, block):
196190 return block
197191
198192 @property
199- def blocks (self ) -> List [Block ]:
193+ def blocks (self ) -> list [Block ]:
200194 """All blocks in the library, preserving order of insertion."""
201195 return self ._blocks
202196
203197 @property
204- def failed_blocks (self ) -> List [ParsingFailedBlock ]:
198+ def failed_blocks (self ) -> list [ParsingFailedBlock ]:
205199 """All blocks that could not be parsed, preserving order of insertion."""
206200 return [b for b in self ._blocks if isinstance (b , ParsingFailedBlock )]
207201
208202 @property
209- def strings (self ) -> List [String ]:
203+ def strings (self ) -> list [String ]:
210204 """All @string blocks in the library, preserving order of insertion."""
211205 return list (self ._strings_by_key .values ())
212206
213207 @property
214- def strings_dict (self ) -> Dict [str , String ]:
208+ def strings_dict (self ) -> dict [str , String ]:
215209 """Dict representation of all @string blocks in the library."""
216210 return self ._strings_by_key .copy ()
217211
218212 @property
219- def entries (self ) -> List [Entry ]:
213+ def entries (self ) -> list [Entry ]:
220214 """All entry (@article, ...) blocks in the library, preserving order of insertion."""
221215 # Note: Taking this from the entries dict would be faster, but does not preserve order
222216 # e.g. in cases where `replace` has been called.
223217 return [b for b in self ._blocks if isinstance (b , Entry )]
224218
225219 @property
226- def entries_dict (self ) -> Dict [str , Entry ]:
220+ def entries_dict (self ) -> dict [str , Entry ]:
227221 """Dict representation of all entry blocks in the library."""
228222 return self ._entries_by_key .copy ()
229223
230224 @property
231- def preambles (self ) -> List [Preamble ]:
225+ def preambles (self ) -> list [Preamble ]:
232226 """All @preamble blocks in the library, preserving order of insertion."""
233227 return [block for block in self ._blocks if isinstance (block , Preamble )]
234228
235229 @property
236- def comments (self ) -> List [ Union [ ExplicitComment , ImplicitComment ] ]:
230+ def comments (self ) -> list [ ExplicitComment | ImplicitComment ]:
237231 """All comment blocks in the library, preserving order of insertion."""
238232 return [
239233 block for block in self ._blocks if isinstance (block , (ExplicitComment , ImplicitComment ))
0 commit comments