Skip to content

Create dynamic country/NUTS region mapping via Eurostat with robust fallback#42

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/create-country-region-codes-dynamically
Draft

Create dynamic country/NUTS region mapping via Eurostat with robust fallback#42
Copilot wants to merge 4 commits intomainfrom
copilot/create-country-region-codes-dynamically

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 20, 2026

This change removes hard-coded country/NUTS mappings and builds REGION_MAPPING dynamically from Eurostat data (countries + EU-wide NUTS2/NUTS3), while preserving current consumer behavior. It also adds deterministic fallback behavior so mapping remains stable when Eurostat data is unavailable or incomplete.

  • Dynamic mapping pipeline in utils.py

    • Added pure loaders:
      • get_country_mapping()
      • get_nuts_mapping(level, country_prefix=None)
      • create_region_mapping()
    • Added regex-based filtering for valid code shapes:
      • countries: ^[A-Z]{2}$
      • NUTS2: ^[A-Z]{2}[A-Z0-9]{2}$
      • NUTS3: ^[A-Z]{2}[A-Z0-9]{3}$
    • Composed final mapping in deterministic precedence order:
      • countries -> NUTS2 -> NUTS3 -> COUNTRIES_SPECIAL_CASES
  • Compatibility and resilience

    • Kept REGION_MAPPING as a plain dict[str, str] (no downstream interface changes).
    • Preserved pass-through behavior for unknown codes (REGION_MAPPING.get(code, code) remains valid).
    • Added graceful fallback to local static mappings for Eurostat import/network/data-shape failures.
    • Added explicit fallback warning to make source mode observable at runtime.
  • Dependency updates

    • Added eurostat to project dependencies in:
      • pixi.toml
      • pyproject.toml
  • Test suite updates (mapping-focused)

    • Reworked tests/test_utils.py to cover:
      • structure/type guarantees
      • required country coverage
      • NUTS2/NUTS3 presence
      • mocked Eurostat success path
      • mocked failure fallback path
      • partial-data fallback path
      • merge precedence (special cases override)
    • Extended integration assertions in:
      • tests/test_network_processor.py
      • tests/test_aggregation.py
    • Added mapping-flow checks for map_country_codes_to_names=True vs disabled pass-through.
def create_region_mapping() -> dict[str, str]:
    return (
        get_country_mapping()
        | get_nuts_mapping(level=2)
        | get_nuts_mapping(level=3)
        | COUNTRIES_SPECIAL_CASES
    )

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • ec.europa.eu
    • Triggering command: /home/REDACTED/.local/bin/pytest pytest tests/test_utils.py tests/test_network_processor.py tests/test_aggregation.py -v (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pytest pytest tests/ -v (dns block)
    • Triggering command: /usr/bin/python python - (dns block)
  • webgate.ec.europa.eu
    • Triggering command: /home/REDACTED/.local/bin/pytest pytest tests/test_utils.py tests/test_network_processor.py tests/test_aggregation.py -v (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pytest pytest tests/ -v (dns block)
    • Triggering command: /usr/bin/python python - (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Create country-codes and region-codes dynamically Create dynamic country/NUTS region mapping via Eurostat with robust fallback Apr 20, 2026
Copilot AI requested a review from maxnutz April 20, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create country-codes and region-codes dynamically

2 participants