Skip to content

Commit

Permalink
Remove '/' from signature of Scenario.add_horizon()
Browse files Browse the repository at this point in the history
This syntax is only available from Python 3.8.
  • Loading branch information
khaeru committed Aug 13, 2020
1 parent cd468e8 commit 2fe3273
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
27 changes: 16 additions & 11 deletions message_ix/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def recurse(k, v, parent='World'):
self.add_set("lvl_spatial", levels)
self.add_set("map_spatial_hierarchy", hierarchy)

def add_horizon(self, data=None, /, year=[], firstmodelyear=None):
def add_horizon(self, year=[], firstmodelyear=None, data=None):
"""Set the scenario time horizon via ``year`` and related categories.
:meth:`add_horizon` acts like ``add_set("year", ...)``, except with
Expand Down Expand Up @@ -332,11 +332,20 @@ def add_horizon(self, data=None, /, year=[], firstmodelyear=None):
>>> s.add_horizon([2020, 2030, 2040])
"""
# Check arguments
# NB once the deprecated signature is removed, this entire block can be
# deleted, and the signature changed to:
# def add_horizon(self, year=[], firstmodelyear=None):
if isinstance(data, dict):
warn("add_horizon() with a dict() argument", DeprecationWarning)
# NB once the deprecated signature is removed, these two 'if' blocks
# and the data= argument can be deleted.
if isinstance(year, dict):
# Move a dict argument to `data` to trigger the next block
if data:
raise ValueError("both year= and data= arguments")
data = year

if data:
warn(
"dict() argument to add_horizon(); use year= and "
"firstmodelyear=",
DeprecationWarning
)

try:
year = data.pop("year")
Expand All @@ -350,11 +359,7 @@ def add_horizon(self, data=None, /, year=[], firstmodelyear=None):
firstmodelyear = data.pop("firstmodelyear", None)

if len(data):
raise ValueError(f"Unknown keys: {sorted(data.keys())}")
elif isinstance(data, list):
# Copy the `data` positional argument to `year`
assert len(year) == 0, "both data= and year= supplied"
year = data
raise ValueError(f"unknown keys: {sorted(data.keys())}")

# Check for existing years
existing = self.set("year").tolist()
Expand Down
5 changes: 4 additions & 1 deletion message_ix/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ def test_add_horizon(test_mp, args, kwargs, exp):
if isinstance(args[0], dict):
with pytest.warns(
DeprecationWarning,
match=r"add_horizon\(\) with a dict\(\) argument",
match=(
r"dict\(\) argument to add_horizon\(\); use year= and "
"firstmodelyear="
)
):
scen.add_horizon(*args, **kwargs)
else:
Expand Down

0 comments on commit 2fe3273

Please sign in to comment.