Skip to content

Fix determinism in state_abbr() #1829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions faker/providers/address/en_US/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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