11import logging
22import os
33from collections .abc import Iterable , Mapping , Sequence
4- from functools import lru_cache , partial
4+ from functools import lru_cache
55from itertools import chain , product , zip_longest
66from typing import Optional , TypeVar , Union
77from warnings import warn
1212from ixmp .backend import ItemType
1313from ixmp .backend .jdbc import JDBCBackend
1414from ixmp .util import as_str_list , maybe_check_out , maybe_commit
15-
16- from message_ix .util .ixmp4 import on_ixmp4backend
15+ from ixmp .util .ixmp4 import is_ixmp4backend
1716
1817# from message_ix.util.scenario_data import PARAMETERS
1918
@@ -210,7 +209,7 @@ def cat_list(self, name: str) -> list[str]:
210209 -------
211210 list of str
212211 """
213- return self ._backend ( " cat_list" , name )
212+ return self .platform . _backend . cat_list ( self , name )
214213
215214 def add_cat (self , name , cat , keys , is_unique = False ):
216215 """Map elements from *keys* to category *cat* within set *name*.
@@ -227,7 +226,9 @@ def add_cat(self, name, cat, keys, is_unique=False):
227226 If `True`, then *cat* must have only one element. An exception is
228227 raised if *cat* already has an element, or if ``len(keys) > 1``.
229228 """
230- self ._backend ("cat_set_elements" , name , str (cat ), as_str_list (keys ), is_unique )
229+ self .platform ._backend .cat_set_elements (
230+ self , name , str (cat ), as_str_list (keys ), is_unique
231+ )
231232
232233 def cat (self , name , cat ):
233234 """Return a list of all set elements mapped to a category.
@@ -247,7 +248,7 @@ def cat(self, name, cat):
247248 return list (
248249 map (
249250 int if name == "year" else lambda v : v ,
250- self ._backend ( " cat_get_elements" , name , cat ),
251+ self .platform . _backend . cat_get_elements ( self , name , cat ),
251252 )
252253 )
253254
@@ -257,16 +258,16 @@ def add_par(
257258 key_or_data : Optional [
258259 Union [int , str , Sequence [Union [int , str ]], dict , pd .DataFrame ]
259260 ] = None ,
260- value = None ,
261- unit : Optional [str ] = None ,
262- comment : Optional [str ] = None ,
261+ value : Union [ float , Iterable [ float ], None ] = None ,
262+ unit : Union [str , Iterable [ str ], None ] = None ,
263+ comment : Union [str , Iterable [ str ], None ] = None ,
263264 ) -> None :
264265 # ixmp.Scenario.add_par() is typed as accepting only str, but this method also
265266 # accepts int for "year"-like dimensions. Proxy the call to avoid type check
266267 # failures.
267268 # TODO Move this upstream, to ixmp
268269
269- if on_ixmp4backend (self ):
270+ if is_ixmp4backend (self . platform . _backend ):
270271 from message_ix .util .scenario_setup import check_existence_of_units
271272
272273 # Check for existence of required units
@@ -294,7 +295,13 @@ def add_par(
294295 def add_set (
295296 self ,
296297 name : str ,
297- key : Union [int , str , Sequence [Union [str , int ]], dict , pd .DataFrame ],
298+ key : Union [
299+ int ,
300+ str ,
301+ Iterable [object ],
302+ dict [str , Union [Sequence [int ], Sequence [str ]]],
303+ pd .DataFrame ,
304+ ],
298305 comment : Union [str , Sequence [str ], None ] = None ,
299306 ) -> None :
300307 # ixmp.Scenario.add_par() is typed as accepting only str, but this method also
@@ -841,10 +848,9 @@ def rename(self, name: str, mapping: Mapping[str, str], keep: bool = False) -> N
841848
842849 # - Iterate over tuples of (item_name, ix_type); only those indexed by `name`.
843850 # - First all sets indexed sets; then all parameters.
844- items = partial (self .items , indexed_by = name , par_data = False )
845851 for item_name , ix_type in chain (
846- zip_longest (items (ItemType .SET ), [], fillvalue = "set" ),
847- zip_longest (items (ItemType .PAR ), [], fillvalue = "par" ),
852+ zip_longest (self . items (ItemType .SET , indexed_by = name ), [], fillvalue = "set" ),
853+ zip_longest (self . items (ItemType .PAR , indexed_by = name ), [], fillvalue = "par" ),
848854 ):
849855 # Identify some index names of this set; only those where the corresponding
850856 # index set is `name`
0 commit comments