Skip to content

Commit 62e7808

Browse files
committed
merge: upstream PR #2383 (en_US county provider) + fixes
Adds US county data (3143 counties across 50 states + DC) to the en_US address provider, exposing three new methods: - county() -> random county from all counties - county_by_state() -> random county, optionally scoped to a state - county_with_state() -> 'County, State' Paths rewritten faker/ -> faker2/ for the renamed package. Fixes applied on top of the upstream patch: 1. counties.py: dropped the stray '"Geographic area name": []' entry, a leftover CSV header artifact that leaked into COUNTIES_BY_STATE. Its empty list made county_with_state() raise IndexError whenever that key was drawn. COUNTIES_BY_STATE now holds exactly 51 keys. 2. __init__.py: made the county_by_state() state argument optional (Optional[str] = None -> pick a random state), matching the existing postcode_in_state convention. Required for tests/test_factory.py:: test_documentor, which invokes every provider method with no args. 3. Reformatted with black (line-length 120); flake8 clean. Tests: 2742 passed, 12 skipped.
1 parent d2d9d78 commit 62e7808

2 files changed

Lines changed: 6404 additions & 0 deletions

File tree

faker2/providers/address/en_US/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Optional, Tuple
33

44
from ..en import Provider as AddressProvider
5+
from .counties import ALL_COUNTIES, COUNTIES_BY_STATE
56

67

78
class Provider(AddressProvider):
@@ -447,6 +448,22 @@ class Provider(AddressProvider):
447448
"PW",
448449
)
449450

451+
counties = ALL_COUNTIES
452+
counties_by_state = COUNTIES_BY_STATE
453+
454+
def county(self) -> str:
455+
return self.random_element(self.counties)
456+
457+
def county_by_state(self, state: Optional[str] = None) -> str:
458+
if state is None:
459+
state = self.random_element(list(self.counties_by_state))
460+
return self.random_element(self.counties_by_state[state])
461+
462+
def county_with_state(self) -> str:
463+
state = self.random_element(list(self.counties_by_state))
464+
county = self.random_element(self.counties_by_state[state])
465+
return f"{county}, {state}"
466+
450467
known_usps_abbr = states_abbr + territories_abbr + freely_associated_states_abbr
451468

452469
military_state_abbr = ("AE", "AA", "AP")

0 commit comments

Comments
 (0)