Skip to content

Replace pycountry with country names from the Unicode CLDR - #1634

Merged
frenck merged 2 commits into
mainfrom
frenck/drop-pycountry
Oct 4, 2026
Merged

frenck merged 2 commits into
mainfrom
frenck/drop-pycountry

Conversation

@frenck

@frenck frenck commented Oct 4, 2026

Copy link
Copy Markdown
Owner

Breaking change

Country.name, Station.country and the names countries() 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.

pycountry is 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 the async-dependency rule.

So this ships the country names with the library instead:

  • src/radios/country_names.py holds COUNTRY_NAMES, the 250 ISO 3166-1 countries plus Kosovo, by code. It is plain Python, so a lookup never does I/O.
  • The names come from the English locale of the Unicode CLDR 48.2.3, the data browsers and operating systems use. Those are the common names people know, which suits a list of stations better than the formal ISO names: "South Korea", "Bolivia", "Taiwan". For six countries, the short name CLDR also has reads better in such a list, and is used instead: Congo (DRC), Congo (Republic), Hong Kong, Macao, Myanmar and Palestine. The typographic apostrophe in Côte d'Ivoire becomes a plain one, which is what people type.
  • scripts/update_country_names.py generates 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.
  • Kosovo is part of the names, so its special case in 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

  • 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

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.

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.
@frenck frenck added refactor Improvement of existing code, not introducing new features. breaking-change A breaking change for existing users. labels Oct 4, 2026
@frenck
frenck requested a balanced review from Copilot October 4, 2026 08:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 1 Medium severity

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.

Comment thread src/radios/country_names.py Resolved
Comment thread scripts/update_country_names.py Resolved
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.
@frenck
frenck merged commit caf18e8 into main Oct 4, 2026
30 checks passed
@frenck
frenck deleted the frenck/drop-pycountry branch October 4, 2026 08:41
@frenck frenck mentioned this pull request Oct 4, 2026
4 of 11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change A breaking change for existing users. refactor Improvement of existing code, not introducing new features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants