Skip to content

Commit 8d821cb

Browse files
authored
Merge pull request #126 from PolicyEngine/feat/api-version-in-output
Add country package version to economy comparisons
2 parents 317eac4 + 17b7ad8 commit 8d821cb

File tree

9 files changed

+105
-2
lines changed

9 files changed

+105
-2
lines changed

policyengine/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from policyengine.utils.data_download import download
55

66
# Datasets
7-
87
ENHANCED_FRS = "hf://policyengine/policyengine-uk-data/enhanced_frs_2022_23.h5"
98
FRS = "hf://policyengine/policyengine-uk-data/frs_2022_23.h5"
109
ENHANCED_CPS = "hf://policyengine/policyengine-us-data/enhanced_cps_2024.h5"

policyengine/outputs/macro/comparison/calculate_economy_comparison.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from policyengine.outputs.macro.single.calculate_single_economy import (
1111
SingleEconomy,
1212
)
13+
from policyengine.utils.packages import get_country_package_version
1314
from typing import List, Dict
1415

1516

@@ -775,6 +776,7 @@ def uk_constituency_breakdown(
775776

776777

777778
class EconomyComparison(BaseModel):
779+
country_package_version: str
778780
budget: BudgetaryImpact
779781
detailed_budget: DetailedBudgetaryImpact
780782
decile: DecileImpact
@@ -823,6 +825,7 @@ def calculate_economy_comparison(
823825
)
824826

825827
return EconomyComparison(
828+
country_package_version=get_country_package_version(country_id),
826829
budget=budgetary_impact_data,
827830
detailed_budget=detailed_budgetary_impact_data,
828831
decile=decile_impact_data,

policyengine/simulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _initialise_simulation(
181181
time_period: TimePeriodType,
182182
region: RegionType,
183183
subsample: SubsampleType,
184-
):
184+
) -> CountrySimulation:
185185
macro = scope == "macro"
186186
_simulation_type: Type[CountrySimulation] = {
187187
"uk": {

policyengine/utils/packages.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from importlib.metadata import version
2+
3+
4+
def get_country_package_name(country_id: str) -> str:
5+
if country_id in NON_STANDARD_COUNTRY_CODES:
6+
return f"policyengine_{NON_STANDARD_COUNTRY_CODES[country_id]}"
7+
if country_id in COUNTRY_IDS:
8+
return f"policyengine_{country_id}"
9+
raise ValueError(
10+
f"Unsupported country ID: {country_id}. Supported IDs are: {COUNTRY_IDS}"
11+
)
12+
13+
14+
def get_country_package_version(country_id: str) -> str:
15+
16+
package_name = get_country_package_name(country_id)
17+
return version(package_name)
18+
19+
20+
COUNTRY_IDS = ["us", "uk", "ca", "il", "ng"]
21+
22+
NON_STANDARD_COUNTRY_CODES = {
23+
"ca": "canada",
24+
}
25+
26+
COUNTRY_PACKAGES = [
27+
get_country_package_name(country) for country in COUNTRY_IDS
28+
]

tests/__init__.py

Whitespace-only changes.

tests/fixtures/utils/__init__.py

Whitespace-only changes.

tests/fixtures/utils/packages.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from policyengine.utils.packages import COUNTRY_PACKAGES
2+
import pytest
3+
from unittest.mock import patch
4+
5+
MOCK_VERSION = "MOCK_VERSION"
6+
7+
8+
@pytest.fixture
9+
def patch_importlib_version():
10+
def mock_version(package_name):
11+
if package_name in COUNTRY_PACKAGES:
12+
return MOCK_VERSION
13+
else:
14+
raise Exception(f"Package {package_name} not found")
15+
16+
with patch(
17+
"policyengine.utils.packages.version", side_effect=mock_version
18+
) as mock:
19+
yield mock

tests/utils/__init__.py

Whitespace-only changes.

tests/utils/test_packages.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import pytest
2+
from policyengine.utils.packages import (
3+
get_country_package_name,
4+
get_country_package_version,
5+
)
6+
from tests.fixtures.utils.packages import (
7+
patch_importlib_version,
8+
MOCK_VERSION,
9+
)
10+
11+
12+
class TestGetCountryPackageName:
13+
def test__given_country_id__then_return_package_name(self):
14+
test_country = "us"
15+
16+
test_package_name = get_country_package_name(test_country)
17+
18+
assert test_package_name == "policyengine_us"
19+
20+
def test__given_non_standard_country_id__then_return_package_name(self):
21+
test_country = "ca"
22+
23+
test_package_name = get_country_package_name(test_country)
24+
25+
assert test_package_name == "policyengine_canada"
26+
27+
def test__given_unsupported_country_id__then_return_package_name(self):
28+
test_country = "zz"
29+
30+
with pytest.raises(Exception, match="Unsupported country ID: zz"):
31+
get_country_package_name(test_country)
32+
33+
34+
class TestGetCountryPackageVersion:
35+
def test__given_package_exists__then_return_version(
36+
self, patch_importlib_version
37+
):
38+
test_country = "us"
39+
40+
test_version = get_country_package_version(test_country)
41+
42+
# Version number defined by mock
43+
assert test_version == MOCK_VERSION
44+
45+
def test__given_package_does_not_exist__then_raise_exception(
46+
self, patch_importlib_version
47+
):
48+
test_country = "zz"
49+
50+
with pytest.raises(
51+
Exception,
52+
match=f"Unsupported country ID: {test_country}. Supported IDs are: ",
53+
):
54+
get_country_package_version(test_country)

0 commit comments

Comments
 (0)