99import pickle
1010import sys
1111import zipfile
12+ from collections .abc import Generator , Iterable , Sequence
1213from os import PathLike
1314from pathlib import Path
14- from typing import Any , Dict , Generator , Iterable , List , Sequence , Tuple , Union
15+ from typing import Any
1516from xml .etree .ElementTree import XML , fromstring
1617
1718from charset_normalizer import detect
@@ -32,7 +33,7 @@ def __new__(cls, name, bases, dict_):
3233class Ns_IO (metaclass = Ns_IO_Meta ):
3334 # Type checker does not detect definition in Ns_IO_Meta, so declare here to
3435 # silence the "access unknown member warning"
35- SUPPORTED_EXTENSIONS : Tuple [str , ...] = tuple ()
36+ SUPPORTED_EXTENSIONS : tuple [str , ...] = tuple ()
3637
3738 DOCX_NAMESPACE = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
3839 DOCX_PARA = DOCX_NAMESPACE + "p"
@@ -95,7 +96,7 @@ def read_odt(cls, path: str) -> str:
9596 return "\n " .join ("" .join (node .itertext ()) for node in paragraphs )
9697
9798 @classmethod
98- def suffix (cls , file_path : Union [ str , PathLike ] , * , strip_dot : bool = False ) -> str :
99+ def suffix (cls , file_path : str | PathLike , * , strip_dot : bool = False ) -> str :
99100 """
100101 >>> suffix('my/library/setup.py')
101102 .py
@@ -112,14 +113,14 @@ def suffix(cls, file_path: Union[str, PathLike], *, strip_dot: bool = False) ->
112113 return extension
113114
114115 @classmethod
115- def supports (cls , file_path : Union [ str , PathLike ] ) -> bool :
116+ def supports (cls , file_path : str | PathLike ) -> bool :
116117 # Can instead use hasattr(f"read_{extension}").
117118 # The SUPPORTED_EXTENSIONS is required by ns_main_cli:74 to list
118119 # supported extensions to users.
119120 return cls .suffix (file_path , strip_dot = True ) in cls .SUPPORTED_EXTENSIONS
120121
121122 @classmethod
122- def not_supports (cls , file_path : Union [ str , PathLike ] ) -> bool :
123+ def not_supports (cls , file_path : str | PathLike ) -> bool :
123124 return not cls .supports (file_path )
124125
125126 @classmethod
@@ -152,16 +153,14 @@ def is_writable(cls, filename: str) -> Ns_Procedure_Result:
152153 return True , None
153154
154155 @classmethod
155- def _check_file_extension (
156- cls , file_path : Union [str , PathLike ], valid_extensions : Union [str , Tuple [str , ...]]
157- ):
156+ def _check_file_extension (cls , file_path : str | PathLike , valid_extensions : str | tuple [str , ...]):
158157 if not os_path .isfile (file_path ):
159158 raise FileNotFoundError (f"File { file_path } does not exist" )
160159 if not str (file_path ).endswith (valid_extensions ):
161160 raise ValueError (f"{ file_path } does not have a valid extension" )
162161
163162 @classmethod
164- def load_pickle_lzma (cls , file_path : Union [ str , PathLike ] ) -> Any :
163+ def load_pickle_lzma (cls , file_path : str | PathLike ) -> Any :
165164 cls ._check_file_extension (file_path , (".pickle.lzma" , ".pkl.lzma" ))
166165
167166 with open (file_path , "rb" ) as f :
@@ -171,30 +170,30 @@ def load_pickle_lzma(cls, file_path: Union[str, PathLike]) -> Any:
171170 return pickle .loads (data_pickle )
172171
173172 @classmethod
174- def load_pickle (cls , file_path : Union [ str , PathLike ] ) -> Any :
173+ def load_pickle (cls , file_path : str | PathLike ) -> Any :
175174 cls ._check_file_extension (file_path , (".pickle" , ".pkl" ))
176175
177176 with open (file_path , "rb" ) as f :
178177 data_pickle = f .read ()
179178 return pickle .loads (data_pickle )
180179
181180 @classmethod
182- def load_lzma (cls , file_path : Union [ str , PathLike ] ) -> bytes :
181+ def load_lzma (cls , file_path : str | PathLike ) -> bytes :
183182 cls ._check_file_extension (file_path , ".lzma" )
184183
185184 with open (file_path , "rb" ) as f :
186185 data_lzma = f .read ()
187186 return lzma .decompress (data_lzma )
188187
189188 @classmethod
190- def load_json (cls , file_path : Union [ str , PathLike ] ) -> Any :
189+ def load_json (cls , file_path : str | PathLike ) -> Any :
191190 cls ._check_file_extension (file_path , ".json" )
192191
193192 with open (file_path , "rb" ) as f :
194193 return json .load (f )
195194
196195 @classmethod
197- def dump_json (cls , data : Any , path : Union [ str , PathLike ] ) -> None :
196+ def dump_json (cls , data : Any , path : str | PathLike ) -> None :
198197 try :
199198 with open (path , "w" ) as f :
200199 json .dump (data , f , ensure_ascii = False , indent = 2 )
@@ -203,7 +202,7 @@ def dump_json(cls, data: Any, path: Union[str, PathLike]) -> None:
203202 cls .dump_json (data , path )
204203
205204 @classmethod
206- def dump_bytes (cls , data : bytes , path : Union [ str , PathLike ] ) -> None :
205+ def dump_bytes (cls , data : bytes , path : str | PathLike ) -> None :
207206 try :
208207 with open (path , "wb" ) as f :
209208 f .write (data )
@@ -212,7 +211,7 @@ def dump_bytes(cls, data: bytes, path: Union[str, PathLike]) -> None:
212211 cls .dump_bytes (data , path )
213212
214213 @classmethod
215- def get_verified_ifile_list (cls , ifile_list : Iterable [str ]) -> List [str ]:
214+ def get_verified_ifile_list (cls , ifile_list : Iterable [str ]) -> list [str ]:
216215 verified_ifile_list = []
217216 for path in ifile_list :
218217 # File path
@@ -245,10 +244,10 @@ def get_verified_ifile_list(cls, ifile_list: Iterable[str]) -> List[str]:
245244 return verified_ifile_list
246245
247246 @classmethod
248- def get_verified_subfiles_list (cls , subfiles_list : List [ List [str ]]) -> List [ List [str ]]:
247+ def get_verified_subfiles_list (cls , subfiles_list : list [ list [str ]]) -> list [ list [str ]]:
249248 verified_subfiles_list = []
250249 for subfiles in subfiles_list :
251- verified_subfiles : List [str ] = cls .get_verified_ifile_list (subfiles )
250+ verified_subfiles : list [str ] = cls .get_verified_ifile_list (subfiles )
252251 if len (verified_subfiles ) == 1 :
253252 logging .critical (
254253 f"Only 1 subfile provided: ({ verified_subfiles .pop ()} ). There should be 2"
@@ -271,15 +270,15 @@ def ensure_unique_filestem(cls, stem: str, existing_stems: Sequence[str]) -> str
271270class Ns_Cache :
272271 CACHE_EXTENSION = ".pickle.lzma"
273272 # fpath_cname: { "/absolute/path/to/foo.txt": "foo.pickle.lzma", ... }
274- fpath_cname : Dict [str , str ] = (
273+ fpath_cname : dict [str , str ] = (
275274 Ns_IO .load_json (CACHE_INFO_PATH )
276275 if CACHE_INFO_PATH .exists () and os_path .getsize (CACHE_INFO_PATH ) > 0
277276 else {}
278277 )
279278 info_changed : bool = False
280279
281280 @classmethod
282- def get_cache_path (cls , file_path : str ) -> Tuple [str , bool ]:
281+ def get_cache_path (cls , file_path : str ) -> tuple [str , bool ]:
283282 """
284283 return (cache_path, available: whether the cache is usable)
285284 """
@@ -306,7 +305,7 @@ def get_cache_path(cls, file_path: str) -> Tuple[str, bool]:
306305 return cache_path , True
307306
308307 @classmethod
309- def _size_fmt (cls , filesize : Union [ int , float ] , suffix : str = "B" ) -> str :
308+ def _size_fmt (cls , filesize : int | float , suffix : str = "B" ) -> str :
310309 # https://github.com/gaogaotiantian/viztracer/blob/3ecd46aa0e70df7dd78f720a2660d6da211c4a51/src/viztracer/util.py#L12
311310 for unit in ("" , "Ki" , "Mi" , "Gi" ):
312311 if abs (filesize ) < 1024.0 :
@@ -315,7 +314,7 @@ def _size_fmt(cls, filesize: Union[int, float], suffix: str = "B") -> str:
315314 return f"{ filesize :.1f} { 'Ti' } { suffix } "
316315
317316 @classmethod
318- def yield_cname_cpath_csize_fpath (cls ) -> Generator [Tuple [str , str , str , str ], None , None ]:
317+ def yield_cname_cpath_csize_fpath (cls ) -> Generator [tuple [str , str , str , str ], None , None ]:
319318 for file_path , cache_name in Ns_Cache .fpath_cname .items ():
320319 cache_path = Ns_Cache ._name2path (cache_name )
321320 if not os_path .exists (cache_path ):
0 commit comments