Skip to content

UK constituency country filter uses substring match instead of startswith #3453

@MaxGhenis

Description

@MaxGhenis

Summary

The UK constituency-breakdown filter uses substring containment ("E" not in code) instead of prefix matching. Any code containing the letter anywhere — not just at the start — is treated as a match, so the "England / Scotland / Wales / Northern Ireland" toggle leaks rows across countries. The sibling uk_local_authority_breakdown function in the same file gets this right with code.startswith(...).

Location

  • Broken: policyengine_api/endpoints/economy/compare.py:601-607
  • Correct reference: policyengine_api/endpoints/economy/compare.py:718-724

What goes wrong

Broken constituency filter:

# Filter to specific country if requested
if selected_country is not None:
    if selected_country == "ENGLAND" and "E" not in code:
        continue
    elif selected_country == "SCOTLAND" and "S" not in code:
        continue
    elif selected_country == "WALES" and "W" not in code:
        continue
    elif selected_country == "NORTHERN_IRELAND" and "N" not in code:
        continue

Because these are ONS GSS codes (e.g. E14000001 for England, W07000049 for Wales, N05000017 for Northern Ireland), any code that merely contains the letter passes. "E" lives inside virtually every constituency code, so ENGLAND never filters anything out; and a Welsh code like W07... passes the "N" not in code test only coincidentally.

Correct version from the LA function right below:

if selected_country == "england" and not code.startswith("E"):
    continue
elif selected_country == "scotland" and not code.startswith("S"):
    continue
elif selected_country == "wales" and not code.startswith("W"):
    continue
elif selected_country == "northern_ireland" and not code.startswith("N"):
    continue

Suggested fix

Switch the constituency filter to code.startswith(...) and, ideally, factor the two copies into one helper so they can't drift again. (Note the case mismatch too: constituency code uses ENGLAND, LA code uses england.)

Severity

Low.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions