Skip to content

Count countries with a lowercase code only once - #1574

Merged
frenck merged 1 commit into
mainfrom
frenck/merge-country-codes
Oct 1, 2026
Merged

frenck merged 1 commit into
mainfrom
frenck/merge-country-codes

Conversation

@frenck

@frenck frenck commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Breaking change

None in practice. Country.code is now always uppercase, and a country no longer shows up twice. Sorting by station count is now done by the client instead of the API, with the same result, plus a stable alphabetical order for countries with the same count.

Proposed change

Found while fixing the country sorting in #1568. A few stations carry their country code in lowercase. The API lists those codes as countries of their own, so after resolving the names, countries() returned the same country twice:

{'name': 'DE', 'stationcount': 6470}
{'name': 'de', 'stationcount': 1}

The set changes over time (right now bg, de, it, tr and us; earlier today also nl), and filtering on a code is case-insensitive in the API: bycountrycodeexact/DE already includes the de station. So these belong to the same country.

countries() now merges codes by their uppercase form and adds up the station counts. Doing that correctly needs the whole list, since a lowercase code could land on a different page than its uppercase sibling. The list is short (around 245 entries), so it is now always fetched in full, and sorted and paged by the client, for both supported orders.

country_name() is also case-insensitive now (including the Kosovo special case), so a station with country code de gets "Germany" too.

Live, after this change: 241 countries, no duplicate names, Germany with 6471 stations.

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.

A few stations carry their country code in lowercase, like "de". The API
lists those as a country of their own, so Germany showed up twice. Merge
them into the uppercase code and add up their station counts; filtering
on "DE" already includes those stations.

Merging needs the whole list, so countries are now always fetched in
full, and sorted and paged by the client. Country names also resolve for
lowercase codes now.
@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Oct 1, 2026
@frenck
frenck merged commit 42a88cd into main Oct 1, 2026
31 checks passed
@frenck
frenck deleted the frenck/merge-country-codes branch October 1, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant