Skip to content

Commit 38e5b3e

Browse files
feat: adiciona suporte a anotações modernas em voter_id (#638)
Co-authored-by: Nilton Pimentel <63605485+niltonpimentel02@users.noreply.github.com>
1 parent a9bc983 commit 38e5b3e

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020

2121
- Suporte a anotações modernas `legal_process` [#622](https://github.com/brazilian-utils/brutils-python/pull/634)
2222

23+
### Fixed
24+
25+
- Suporte a anotações modernas `voter_id` [#623](https://github.com/brazilian-utils/brutils-python/pull/638)
26+
2327
## [2.3.0] - 2025-10-07
2428

2529
### Added

brutils/voter_id.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from 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

tests/test_voter_id.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_valid_voter_id(self):
1818
# test a valid voter id number
1919
voter_id = "217633460930"
2020
self.assertIs(is_valid(voter_id), True)
21+
self.assertIsInstance(is_valid(voter_id), bool)
2122

2223
def test_invalid_voter_id(self):
2324
# test an invalid voter id number (dv1 & UF fail)
@@ -59,12 +60,18 @@ def test_get_voter_id_parts(self):
5960
verifying_digits = _get_verifying_digits(voter_id)
6061

6162
self.assertEqual(sequential_number, "12345678")
63+
self.assertIsInstance(sequential_number, str)
64+
6265
self.assertEqual(federative_union, "AB")
66+
self.assertIsInstance(federative_union, str)
67+
6368
self.assertEqual(verifying_digits, "12")
69+
self.assertIsInstance(verifying_digits, str)
6470

6571
def test_valid_length_verify(self):
6672
voter_id = "123456789012"
6773
self.assertIs(_is_length_valid(voter_id), True)
74+
self.assertIsInstance(_is_length_valid(voter_id), bool)
6875

6976
def test_invalid_length_verify(self):
7077
voter_id = "12345678AB123" # Invalid length
@@ -79,6 +86,7 @@ def test_calculate_vd1(self):
7986
self.assertIs(_calculate_vd1("42750359", "02"), 1)
8087
# test edge case: rest is 10, declare vd1 as 0
8188
self.assertIs(_calculate_vd1("73146415", "03"), 0)
89+
self.assertIsInstance(_calculate_vd1("73146415", "03"), int)
8290

8391
def test_calculate_vd2(self):
8492
self.assertIs(_calculate_vd2("02", 7), 2)
@@ -90,6 +98,7 @@ def test_calculate_vd2(self):
9098
# edge case: if UF is "02" (for MG) and rest == 0
9199
# declare dv2 as 1 instead
92100
self.assertIs(_calculate_vd2("02", 8), 1)
101+
self.assertIsInstance(_calculate_vd2("02", 8), int)
93102

94103
def test_generate_voter_id(self):
95104
# test if is_valid a voter id from MG
@@ -103,13 +112,16 @@ def test_generate_voter_id(self):
103112
# test if is_valid a voter id from foreigner
104113
voter_id = generate()
105114
self.assertIs(is_valid(voter_id), True)
115+
self.assertIsInstance(voter_id, str)
106116

107117
# test if UF is not valid
108118
voter_id = generate(federative_union="XX")
109119
self.assertIs(is_valid(voter_id), False)
120+
self.assertIsNone(voter_id)
110121

111122
def test_format_voter_id(self):
112123
self.assertEqual(format_voter_id("277627122852"), "2776 2712 28 52")
124+
self.assertIsInstance(format_voter_id("277627122852"), str)
113125
self.assertIsNone(format_voter_id("00000000000"))
114126
self.assertIsNone(format_voter_id("0000000000a"))
115127
self.assertIsNone(format_voter_id("000000000000"))

0 commit comments

Comments
 (0)