Skip to content

List the states of a country by its country code - #1586

Merged
frenck merged 1 commit into
mainfrom
frenck/states-country-code
Oct 1, 2026
Merged

frenck merged 1 commit into
mainfrom
frenck/states-country-code

Conversation

@frenck

@frenck frenck commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Breaking change

states() takes a country_code (like "NL") instead of a country (like "The Netherlands"). states() was added in #1583 and has not been released, so this does not affect anyone yet.

Proposed change

Found in a third review of the library. states() (#1583) filtered on the name the API stores for a country, like "The Netherlands". That is neither the code nor the name countries() returns ("Netherlands"), so a country picker built from countries() found no states at all.

states() now takes a country_code, like search(country_code=...) and Country.code. The library looks up the API's own name for that country through /json/countries, so this costs one extra request. Names like "Coted Ivoire" are why that lookup is needed: pycountry would never produce them.

An unknown code returns no states. That includes XX, which the API really lists in /json/countries with an empty name; using that empty name as the filter would have returned the states of every country. The test fixture includes that entry.

Live: states(country_code="NL") returns the 154 Dutch states, lowercase codes work too, and XX and ZZ return nothing.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

  • This PR fixes or closes issue: fixes # None
  • This PR is related to: List codecs and states #1583
  • Link to a separate documentation pull request: None

Checklist

  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and poetry run pytest passes locally. A pull request cannot be merged unless CI is green.
  • poetry run prek run --all-files passes (lint, format, and type checks).
  • No commented-out or dead code is left in the pull request.

The API filters states on its own name for a country, like "The
Netherlands", which matches neither the code nor the name countries()
returns. Take a country code instead, and look up the API name.
@frenck frenck added the new-feature New features or options. label Oct 1, 2026
@frenck
frenck merged commit e8b2e0b into main Oct 1, 2026
31 checks passed
@frenck
frenck deleted the frenck/states-country-code branch October 1, 2026 19:35
@frenck frenck mentioned this pull request Oct 2, 2026
4 of 11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature New features or options.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant