11from random import randint
22
33
4- def is_valid (voter_id ): # type: ( str) -> bool
4+ def is_valid (voter_id : str ) -> bool :
55 """
66 Check if a Brazilian voter id number is valid.
77 It does not verify if the voter id actually exists.
@@ -50,7 +50,7 @@ def is_valid(voter_id): # type: (str) -> bool
5050 return True
5151
5252
53- def _is_length_valid (voter_id ): # type: ( str) -> bool
53+ def _is_length_valid (voter_id : str ) -> bool :
5454 """
5555 Check if the length of the provided voter id is valid.
5656 Typically, the length should be 12, but there are cases for SP and MG where
@@ -73,7 +73,7 @@ def _is_length_valid(voter_id): # type: (str) -> bool
7373 return len (voter_id ) == 13 and federative_union in ["01" , "02" ]
7474
7575
76- def _get_sequential_number (voter_id ): # type: ( str) -> str
76+ def _get_sequential_number (voter_id : str ) -> str :
7777 """
7878 Some voter ids in São Paulo and Minas Gerais may have nine digits in their
7979 sequential number instead of eight. This fact does not compromise the
@@ -91,7 +91,7 @@ def _get_sequential_number(voter_id): # type: (str) -> str
9191 return voter_id [:8 ]
9292
9393
94- def _get_federative_union (voter_id ): # type: ( str) -> str
94+ def _get_federative_union (voter_id : str ) -> str :
9595 """
9696 Returns the two digits that represent the federative union for the given
9797 voter id. Indexing it backwards, as the sequential_number can have eight
@@ -107,7 +107,7 @@ def _get_federative_union(voter_id): # type: (str) -> str
107107 return voter_id [- 4 :- 2 ]
108108
109109
110- def _get_verifying_digits (voter_id ): # type: ( str) -> str
110+ def _get_verifying_digits (voter_id : str ) -> str :
111111 """
112112 Returns the two verifying digits for the given voter id. Indexing it
113113 backwards, as the sequential_number can have eight or nine digits.
@@ -122,7 +122,7 @@ def _get_verifying_digits(voter_id): # type: (str) -> str
122122 return voter_id [- 2 :]
123123
124124
125- def _is_federative_union_valid (federative_union ): # type: ( str) -> bool
125+ def _is_federative_union_valid (federative_union : str ) -> bool :
126126 """
127127 Check if the federative union is valid.
128128
@@ -137,7 +137,7 @@ def _is_federative_union_valid(federative_union): # type: (str) -> bool
137137 return federative_union in ["{:02d}" .format (i ) for i in range (1 , 29 )]
138138
139139
140- def _calculate_vd1 (sequential_number , federative_union ): # type: ( str, str ) -> bool
140+ def _calculate_vd1 (sequential_number : str , federative_union : str ) -> int :
141141 """
142142 Calculate the first verifying digit.
143143
@@ -178,7 +178,7 @@ def _calculate_vd1(sequential_number, federative_union): # type: (str, str) ->
178178 return vd1
179179
180180
181- def _calculate_vd2 (federative_union , vd1 ): # type: ( str, int) -> str
181+ def _calculate_vd2 (federative_union : str , vd1 : int ) -> int :
182182 """
183183 Calculate the second verifying digit.
184184
@@ -214,7 +214,7 @@ def _calculate_vd2(federative_union, vd1): # type: (str, int) -> str
214214 return vd2
215215
216216
217- def generate (federative_union = "ZZ" ) -> str :
217+ def generate (federative_union : str = "ZZ" ) -> str | None :
218218 """
219219 Generates a random valid Brazilian voter registration.
220220
@@ -263,9 +263,10 @@ def generate(federative_union="ZZ") -> str:
263263 vd1 = _calculate_vd1 (sequential_number , uf_number )
264264 vd2 = _calculate_vd2 (uf_number , vd1 )
265265 return f"{ sequential_number } { uf_number } { vd1 } { vd2 } "
266+ return None
266267
267268
268- def format_voter_id (voter_id ): # type: ( str) -> str
269+ def format_voter_id (voter_id : str ) -> str | None :
269270 """
270271 Format a voter ID for display with visual spaces.
271272
0 commit comments