Skip to content

Commit

Permalink
Fix determinism in state_abbr() (#1829)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
DavidCain committed Mar 23, 2023
1 parent 2c39895 commit 7cd3ac5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions faker/providers/address/en_US/__init__.py
@@ -1,5 +1,5 @@
from collections import OrderedDict
from typing import Optional, Set
from typing import Optional, Tuple

from ..en import Provider as AddressProvider

Expand Down Expand Up @@ -516,11 +516,11 @@ def state_abbr(
:param include_freely_associated_states: If True, freely-associated states will be included.
If False, sovereign states in free association with the US will be excluded.
"""
abbreviations: Set[str] = set(self.states_abbr)
abbreviations: Tuple[str, ...] = self.states_abbr
if include_territories:
abbreviations.update(self.territories_abbr)
abbreviations += self.territories_abbr
if include_freely_associated_states:
abbreviations.update(self.freely_associated_states_abbr)
abbreviations += self.freely_associated_states_abbr
return self.random_element(abbreviations)

def postcode(self) -> str:
Expand Down
6 changes: 6 additions & 0 deletions tests/providers/test_address.py
Expand Up @@ -626,6 +626,12 @@ def test_postalcode_in_state(self, faker, num_samples):
with pytest.raises(Exception):
faker.postalcode_in_state("XX")

def test_state_abbr_determinism(self, faker):
faker.seed_instance(0)
first = faker.state_abbr()
faker.seed_instance(0)
assert faker.state_abbr() == first


class TestEsCo:
"""Test es_CO address provider methods"""
Expand Down

0 comments on commit 7cd3ac5

Please sign in to comment.