77 :copyright: Copyright 2021 Shengyu Zhang
88 :license: BSD, see LICENSE for details.
99"""
10- from typing import Tuple , Dict , Iterator , List , Set , Optional , Union , Any
10+ from typing import Iterator , Any
1111from enum import Enum , auto
1212from dataclasses import dataclass
1313import pickle
@@ -32,7 +32,7 @@ class SchemaError(AnyExtensionError):
3232class Object (object ):
3333 objtype :str
3434 name :str
35- attrs :Dict [str ,str ]
35+ attrs :dict [str ,str ]
3636 content :str
3737
3838 def hexdigest (self ) -> str :
@@ -90,20 +90,20 @@ def _as_plain(self, rawval:str) -> str:
9090 return rawval
9191
9292
93- def _as_words (self , rawval :str ) -> List [str ]:
93+ def _as_words (self , rawval :str ) -> list [str ]:
9494 assert self .form == self .Form .WORDS
9595 assert rawval is not None
9696 return [x .strip () for x in rawval .split (' ' ) if x .strip () != '' ]
9797
9898
99- def _as_lines (self , rawval :str ) -> List [str ]:
99+ def _as_lines (self , rawval :str ) -> list [str ]:
100100 assert self .form == self .Form .LINES
101101 assert rawval is not None
102102 return rawval .split ('\n ' )
103103
104104
105105
106- def value_of (self , rawval :Optional [ str ] ) -> Union [ None , str , List [str ] ]:
106+ def value_of (self , rawval :str | None ) -> None | str | list [str ]:
107107 if rawval is None :
108108 assert not self .required
109109 return None
@@ -139,7 +139,7 @@ class Schema(object):
139139
140140 # Object fields
141141 name :Field
142- attrs :Dict [str ,Field ]
142+ attrs :dict [str ,Field ]
143143 content :Field
144144
145145 # Class-wide shared template environment
@@ -160,9 +160,9 @@ class Schema(object):
160160 ambiguous_reference_template :str
161161
162162 def __init__ (self , objtype :str ,
163- name :Optional [ Field ] = Field (unique = True , referenceable = True ),
164- attrs :Dict [str ,Field ]= {},
165- content :Optional [ Field ] = Field (),
163+ name :Field | None = Field (unique = True , referenceable = True ),
164+ attrs :dict [str ,Field ]= {},
165+ content :Field | None = Field (),
166166 description_template :str = '{{ content }}' ,
167167 reference_template :str = '{{ title }}' ,
168168 missing_reference_template :str = '{{ title }} (missing reference)' ,
@@ -199,7 +199,7 @@ def __init__(self, objtype:str,
199199 has_unique = field .unique
200200
201201
202- def object (self , name :Optional [ str ] , attrs :Dict [str ,str ], content :Optional [ str ] ) -> Object :
202+ def object (self , name :str | None , attrs :dict [str ,str ], content :str | None ) -> Object :
203203 """Generate a object"""
204204 obj = Object (objtype = self .objtype ,
205205 name = name ,
@@ -211,11 +211,11 @@ def object(self, name:Optional[str], attrs:Dict[str,str], content:Optional[str])
211211 return obj
212212
213213
214- def fields_of (self , obj :Object ) -> Iterator [Tuple [str ,Field ,Union [ None , str , List [str ] ]]]:
214+ def fields_of (self , obj :Object ) -> Iterator [tuple [str ,Field ,None | str | list [str ]]]:
215215 """
216216 Helper method for returning all fields of object and its raw values.
217217 -> Iterator[field_name, field_instance, field_value],
218- while the field_value is Union[ string_value, string_list_value] .
218+ while the field_value is string_value| string_list_value.
219219 """
220220 if self .name :
221221 yield (self .NAME_KEY , self .name , self .name .value_of (obj .name ) if obj else None )
@@ -225,22 +225,22 @@ def fields_of(self, obj:Object) -> Iterator[Tuple[str,Field,Union[None,str,List[
225225 yield (self .CONTENT_KEY , self .content , self .content .value_of (obj .content ) if obj else None )
226226
227227
228- def name_of (self , obj :Object ) -> Union [ None , str , List [str ] ]:
228+ def name_of (self , obj :Object ) -> None | str | list [str ]:
229229 assert obj
230230 return self .content .value_of (obj .name )
231231
232232
233- def attrs_of (self , obj :Object ) -> Dict [str ,Union [ None , str , List [str ] ]]:
233+ def attrs_of (self , obj :Object ) -> dict [str ,None | str | list [str ]]:
234234 assert obj
235235 return {k : f .value_of (obj .attrs .get (k )) for k , f in self .attrs .items ()}
236236
237237
238- def content_of (self , obj :Object ) -> Union [ None , str , List [str ] ]:
238+ def content_of (self , obj :Object ) -> None | str | list [str ]:
239239 assert obj
240240 return self .content .value_of (obj .content )
241241
242242
243- def identifier_of (self , obj :Object ) -> Tuple [ Optional [ str ] ,str ]:
243+ def identifier_of (self , obj :Object ) -> tuple [ str | None ,str ]:
244244 """
245245 Return unique identifier of object.
246246 If there is not any unique field, return (None, obj.hexdigest()) instead.
@@ -259,7 +259,7 @@ def identifier_of(self, obj:Object) -> Tuple[Optional[str],str]:
259259 return None , obj .hexdigest ()
260260
261261
262- def title_of (self , obj :Object ) -> Optional [ str ] :
262+ def title_of (self , obj :Object ) -> str | None :
263263 """Return title (display name) of object."""
264264 assert obj
265265 name = self .name .value_of (obj .name )
@@ -271,7 +271,7 @@ def title_of(self, obj:Object) -> Optional[str]:
271271 return None
272272
273273
274- def references_of (self , obj :Object ) -> Set [ Tuple [str ,str ]]:
274+ def references_of (self , obj :Object ) -> set [ tuple [str ,str ]]:
275275 """Return all references (referenceable fields) of object"""
276276 assert obj
277277 refs = []
@@ -287,16 +287,16 @@ def references_of(self, obj:Object) -> Set[Tuple[str,str]]:
287287 return set (refs )
288288
289289
290- def _context_without_object (self ) -> Dict [str ,Union [ str , List [str ] ]]:
290+ def _context_without_object (self ) -> dict [str ,str | list [str ]]:
291291 return {
292292 self .TYPE_KEY : self .objtype ,
293293 }
294294
295295
296- def _context_of (self , obj :Object ) -> Dict [str ,Union [ str , List [str ] ]]:
296+ def _context_of (self , obj :Object ) -> dict [str ,str | list [str ]]:
297297 context = self ._context_without_object ()
298298
299- def set_if_not_none (key : str , val : Union [ str , List [ str ]] ) -> None :
299+ def set_if_not_none (key , val ) -> None :
300300 if val is not None :
301301 context [key ] = val
302302 set_if_not_none (self .NAME_KEY , self .name_of (obj ))
@@ -308,7 +308,7 @@ def set_if_not_none(key:str, val:Union[str,List[str]]) -> None:
308308 return context
309309
310310
311- def render_description (self , obj :Object ) -> List [str ]:
311+ def render_description (self , obj :Object ) -> list [str ]:
312312 assert obj
313313 tmpl = TemplateEnvironment ().from_string (self .description_template )
314314 description = tmpl .render (self ._context_of (obj ))
0 commit comments