11import math
22import re
33from abc import ABC , abstractmethod
4- from typing import Any , Dict , List , Optional , OrderedDict , Tuple , Type
4+ from collections import OrderedDict
5+ from typing import Any
56
67from .format_extras import Kind
78
8- Correction = Tuple [int , str ]
9+ Correction = tuple [int , str ]
910
1011
1112class FormattingRule (ABC ):
@@ -23,7 +24,7 @@ class FormattingRule(ABC):
2324
2425 def __init__ (self , properties : OrderedDict ):
2526 self ._properties = properties
26- self ._corrections : List [Correction ] = []
27+ self ._corrections : list [Correction ] = []
2728
2829 # Universal properties:
2930
@@ -34,13 +35,13 @@ def __init__(self, properties: OrderedDict):
3435 "tab_width" , default = self ._indent_size , value_type = int
3536 )
3637
37- self ._indent_style : Optional [ str ] = self ._properties .get ("indent_style" , None )
38+ self ._indent_style : str | None = self ._properties .get ("indent_style" , None )
3839
3940 self ._indent_str : str = " " * self ._indent_size
4041 if self ._indent_style and self ._indent_style == "tab" :
4142 self ._indent_str = "\t "
4243
43- self ._end_of_line : Optional [ str ] = self ._properties .get ("end_of_line" , None )
44+ self ._end_of_line : str | None = self ._properties .get ("end_of_line" , None )
4445 options = {"lf" : "\n " , "cr" : "\r " , "crlf" : "\r \n " }
4546 self ._line_ending : str = options .get (self ._end_of_line , "\n " )
4647
@@ -58,7 +59,7 @@ def get_property(
5859 self ,
5960 name : str ,
6061 default : Any = None ,
61- value_type : Optional [ Type ] = None ,
62+ value_type : type | None = None ,
6263 ) -> Any :
6364 """Get item from ``_properties``, parsing as needed.
6465
@@ -81,7 +82,7 @@ def get_property(
8182 return value_type (value )
8283
8384 @abstractmethod
84- def format (self , content : List [str ], kind : Optional [ Kind ] = None ):
85+ def format (self , content : list [str ], kind : Kind | None = None ):
8586 """Fun rule to format text.
8687
8788 :param content: Text to format (changed in place!)
@@ -96,7 +97,7 @@ def add_correction(self, message: str, line_nr: int):
9697 """
9798 self ._corrections .append ((line_nr , message ))
9899
99- def consume_corrections (self ) -> List [Correction ]:
100+ def consume_corrections (self ) -> list [Correction ]:
100101 """Return listed corrections and reset list."""
101102 corrections = self ._corrections
102103 self ._corrections = []
@@ -112,7 +113,7 @@ class FormatTabs(FormattingRule):
112113 def __init__ (self , * args ):
113114 super ().__init__ (* args )
114115
115- def format (self , content : List [str ], kind : Optional [ Kind ] = None ):
116+ def format (self , content : list [str ], kind : Kind | None = None ):
116117 if self ._indent_style == "tab" :
117118 re_search = self ._re_spaces
118119 elif self ._indent_style == "space" :
@@ -165,7 +166,7 @@ def __init__(self, *args):
165166 "trim_trailing_whitespace" , False , value_type = bool
166167 )
167168
168- def format (self , content : List [str ], kind : Optional [ Kind ] = None ):
169+ def format (self , content : list [str ], kind : Kind | None = None ):
169170 if not self ._remove_tr_ws :
170171 return # Nothing to do
171172 for i , line in enumerate (content ):
@@ -185,7 +186,7 @@ def __init__(self, *args):
185186 "insert_final_newline" , False , value_type = bool
186187 )
187188
188- def format (self , content : List [str ], kind : Optional [ Kind ] = None ):
189+ def format (self , content : list [str ], kind : Kind | None = None ):
189190 if not self ._insert_final_newline :
190191 return
191192
@@ -232,7 +233,7 @@ def __init__(self, *args):
232233 else :
233234 raise ValueError (f"Unrecognized file ending `{ self ._line_ending } `" )
234235
235- def format (self , content : List [str ], kind : Optional [ Kind ] = None ):
236+ def format (self , content : list [str ], kind : Kind | None = None ):
236237 if self ._end_of_line is None :
237238 return # Nothing specified
238239
@@ -279,7 +280,7 @@ def __init__(self, *args):
279280
280281 self ._re_newlines = re .compile (r"[\r\n]+$" )
281282
282- def format (self , content : List [str ], kind : Optional [ Kind ] = None ):
283+ def format (self , content : list [str ], kind : Kind | None = None ):
283284 if not self ._align :
284285 return # Disabled by config
285286
@@ -288,14 +289,14 @@ def format(self, content: List[str], kind: Optional[Kind] = None):
288289
289290 self .format_argument_list (content )
290291
291- def format_argument_list (self , content : List [str ]):
292+ def format_argument_list (self , content : list [str ]):
292293 """Format entire declaration section"""
293294
294295 # Get variable definitions, split up and keyed by content index:
295- variable_definitions : Dict [int , List [ Optional [ str ]]] = {}
296+ variable_definitions : dict [int , list [ str ]] | None = {}
296297
297298 # Biggest size of each chunk across all lines:
298- max_chunk_sizes : List [ Optional [ int ] ] = [None ] * 3
299+ max_chunk_sizes : list [ int | None ] = [None ] * 3
299300
300301 for i , line in enumerate (content ):
301302 match = self ._re_variable .match (line )
@@ -414,7 +415,7 @@ def __init__(self, *args):
414415 re .VERBOSE | re .MULTILINE ,
415416 )
416417
417- def format (self , content : List [str ], kind : Optional [ Kind ] = None ):
418+ def format (self , content : list [str ], kind : Kind | None = None ):
418419 if self ._parentheses is None :
419420 return # Nothing to do
420421
@@ -465,12 +466,12 @@ def format(self, content: List[str], kind: Optional[Kind] = None):
465466 @staticmethod
466467 def find_and_match_braces (
467468 text : str , brace_left : str = "(" , brace_right : str = ")"
468- ) -> Tuple [int , int ]:
469+ ) -> tuple [int , int ]:
469470 """Step through braces in a string.
470471
471472 Note that levels can step into negative.
472473
473- :return: Tuple of (strpos, level), where strpos is the zero-index position of
474+ :return: tuple of (strpos, level), where strpos is the zero-index position of
474475 the brace itself and level is the nested level it indicates
475476 """
476477 level = 0
0 commit comments