Skip to content

Commit 7cd3ac5

Browse files
authored
Fix determinism in state_abbr() (#1829)
Unfortunately, by changing the code to use sets (unordered by definition) from simple tuples, I broke the determinism of `state_abbr()`. Apologies! Changing it back to use a simple ordered collection fixes the problem. (This probably makes sense to ship as 18.3.1, since there's no change in the API)
1 parent 2c39895 commit 7cd3ac5

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

faker/providers/address/en_US/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections import OrderedDict
2-
from typing import Optional, Set
2+
from typing import Optional, Tuple
33

44
from ..en import Provider as AddressProvider
55

@@ -516,11 +516,11 @@ def state_abbr(
516516
:param include_freely_associated_states: If True, freely-associated states will be included.
517517
If False, sovereign states in free association with the US will be excluded.
518518
"""
519-
abbreviations: Set[str] = set(self.states_abbr)
519+
abbreviations: Tuple[str, ...] = self.states_abbr
520520
if include_territories:
521-
abbreviations.update(self.territories_abbr)
521+
abbreviations += self.territories_abbr
522522
if include_freely_associated_states:
523-
abbreviations.update(self.freely_associated_states_abbr)
523+
abbreviations += self.freely_associated_states_abbr
524524
return self.random_element(abbreviations)
525525

526526
def postcode(self) -> str:

tests/providers/test_address.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,12 @@ def test_postalcode_in_state(self, faker, num_samples):
626626
with pytest.raises(Exception):
627627
faker.postalcode_in_state("XX")
628628

629+
def test_state_abbr_determinism(self, faker):
630+
faker.seed_instance(0)
631+
first = faker.state_abbr()
632+
faker.seed_instance(0)
633+
assert faker.state_abbr() == first
634+
629635

630636
class TestEsCo:
631637
"""Test es_CO address provider methods"""

0 commit comments

Comments
 (0)