11import unicodedata
22from collections import OrderedDict
3+ from typing import Any , Dict , Iterator , OrderedDict as OrderedDictType
34from functools import total_ordering
45
5- def get_match_ordered_dict ():
6+ def get_match_ordered_dict () -> OrderedDictType [ str , type ] :
67 slots = OrderedDict ([
78 ('ruleId' , str ),
89 ('message' , str ),
@@ -32,7 +33,7 @@ def get_match_ordered_dict():
3233 }
3334
3435"""
35- def auto_type (obj ) :
36+ def auto_type (obj : Any ) -> Any :
3637 try :
3738 return int (obj )
3839 except ValueError :
@@ -44,7 +45,7 @@ def auto_type(obj):
4445@total_ordering
4546class Match :
4647 """Hold information about where a rule matches text."""
47- def __init__ (self , attrib ) :
48+ def __init__ (self , attrib : Dict [ str , Any ]) -> None :
4849 # Process rule.
4950 attrib ['category' ] = attrib ['rule' ]['category' ]['id' ]
5051 attrib ['ruleId' ] = attrib ['rule' ]['id' ]
@@ -63,8 +64,8 @@ def __init__(self, attrib):
6364 for k , v in attrib .items ():
6465 setattr (self , k , v )
6566
66- def __repr__ (self ):
67- def _ordered_dict_repr ():
67+ def __repr__ (self ) -> str :
68+ def _ordered_dict_repr () -> str :
6869 slots = list (get_match_ordered_dict ())
6970 slots += list (set (self .__dict__ ).difference (slots ))
7071 attrs = [slot for slot in slots
@@ -73,7 +74,7 @@ def _ordered_dict_repr():
7374
7475 return f'{ self .__class__ .__name__ } ({ _ordered_dict_repr ()} )'
7576
76- def __str__ (self ):
77+ def __str__ (self ) -> str :
7778 ruleId = self .ruleId
7879 s = f'Offset { self .offset } , length { self .errorLength } , Rule ID: { ruleId } '
7980 if self .message :
@@ -84,7 +85,7 @@ def __str__(self):
8485 return s
8586
8687 @property
87- def matchedText (self ):
88+ def matchedText (self ) -> str :
8889 """ Returns the text that garnered the error (without its surrounding context).
8990 """
9091 return self .context [self .offsetInContext :self .offsetInContext + self .errorLength ]
@@ -98,22 +99,22 @@ def select_replacement(self, index: int) -> None:
9899 raise ValueError (f'This Match\' s suggestions are numbered from 0 to { len (self .replacements ) - 1 } ' )
99100 self .replacements = [self .replacements [index ]]
100101
101- def __eq__ (self , other ) :
102+ def __eq__ (self , other : Any ) -> bool :
102103 return list (self ) == list (other )
103104
104- def __lt__ (self , other ) :
105+ def __lt__ (self , other : Any ) -> bool :
105106 return list (self ) < list (other )
106107
107- def __iter__ (self ):
108+ def __iter__ (self ) -> Iterator [ Any ] :
108109 return iter (getattr (self , attr ) for attr in get_match_ordered_dict ())
109110
110- def __setattr__ (self , key , value ) :
111+ def __setattr__ (self , key : str , value : Any ) -> None :
111112 try :
112113 value = get_match_ordered_dict ()[key ](value )
113114 except KeyError :
114115 return
115116 super ().__setattr__ (key , value )
116117
117- def __getattr__ (self , name ) :
118+ def __getattr__ (self , name : str ) -> Any :
118119 if name not in get_match_ordered_dict ():
119120 raise AttributeError (f'{ self .__class__ .__name__ !r} object has no attribute { name !r} ' )
0 commit comments