Repository navigation
Replace pycountry with country names from the Unicode CLDR - #1634
Merged
Merged
Conversation
pycountry was only used to turn a country code into a name, and it reads its database from disk on first use, which ends up in the event loop of an asyncio application. Ship the names instead, generated from the Unicode CLDR by a script: the common English names browsers and operating systems use, like "South Korea" instead of "Korea, Republic of". Kosovo is part of it, so its special case goes. pycountry stays as a development dependency, for the generator and the tests.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The bundled CLDR data needs its required license notice, and the generator needs offline automated tests.
Review effort: Balanced
Findings: 1
Open (2)
What changed in this PR
Replaces runtime pycountry lookups with bundled CLDR country names, avoiding database I/O in the async client.
Changes:
- Uses common English country names, including Kosovo.
- Adds a reproducible generator and country-name tests.
- Moves pycountry to development dependencies and updates tooling guidance.
| File | Description |
|---|---|
| tests/test_models.py | Updates the Kosovo test docstring. |
| tests/test_lists.py | Updates the Kosovo explanatory comment. |
| tests/test_country_names.py | Tests code coverage and name lookup behavior. |
| src/radios/models.py | Resolves names through the bundled dictionary. |
| src/radios/country_names.py | Adds generated CLDR country names. |
| scripts/update_country_names.py | Generates the country-name module. |
| pyproject.toml | Moves pycountry to development dependencies and configures script linting. |
| poetry.lock | Updates pycountry’s version and dependency group. |
| AGENTS.md | Documents the generated data and maintenance workflow. |
| .pre-commit-config.yaml | Excludes generated names from spelling checks. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
The Unicode License V3 of the CLDR asks for its copyright and permission notice to come with every copy of the data. The generator now puts it in country_names.py, and the package declares MIT AND Unicode-3.0.
4 of 11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Breaking change
Country.name,Station.countryand the namescountries()returns are now the common English names from the Unicode CLDR, instead of the formal ISO names from pycountry. 47 countries get another name, for example "South Korea" instead of "Korea, Republic of", "Iran" instead of "Iran, Islamic Republic of", "Vietnam" instead of "Viet Nam", "Taiwan" instead of "Taiwan, Province of China", "Russia" instead of "Russian Federation", and "Bosnia & Herzegovina" instead of "Bosnia and Herzegovina". Code that compares country names should compare country codes instead.pycountryis no longer a dependency.Proposed change
radios used pycountry for one thing: turning a country code into a name. pycountry reads its 13 MB database from disk on first use, and that first use happens in
countries(), inside the event loop of an asyncio application. Home Assistant works around that by loading the database in an executor first, which a review on home-assistant/core#184085 rightly pointed out under theasync-dependencyrule.So this ships the country names with the library instead:
src/radios/country_names.pyholdsCOUNTRY_NAMES, the 250 ISO 3166-1 countries plus Kosovo, by code. It is plain Python, so a lookup never does I/O.scripts/update_country_names.pygenerates the file, so updating it to a newer CLDR is one command. pycountry stays as a development dependency, for that script and for the tests.country_name()goes.A new test checks the names cover every ISO 3166-1 country plus Kosovo and nothing else, and pins a few of the choices above. codespell skips the generated file, as it reads "FO", the Faroe Islands, as a typo.
Once this is released, Home Assistant can drop pycountry from the Radio Browser integration, along with its executor workaround.
Type of change
Additional information
Checklist
poetry run pytestpasses locally. A pull request cannot be merged unless CI is green.poetry run prek run --all-filespasses (lint, format, and type checks).