1- import inspect
21import textwrap
32import types
43from typing import Type
@@ -15,60 +14,12 @@ def patch_pyomo():
1514 if _patched_pyomo :
1615 return
1716
18- # fix Pyomo issue #2019. Once PR #2020 gets released this will no longer be needed
19- from pyomo .core .base .misc import _robust_sort_keyfcn
20- setattr (_robust_sort_keyfcn , "__call__" , fixed_robust_sort_keyfcn )
21-
2217 # Patch Set and Param to allow specifying input file location (via input_file="...")
2318 extend_to_allow_loading (Set )
2419 extend_to_allow_loading (Param )
2520
2621 _patched_pyomo = True
2722
28- def fixed_robust_sort_keyfcn (self , val , use_key = True ):
29- """Generate a tuple ( str(type_name), val ) for sorting the value.
30-
31- `key=` expects a function. We are generating a functor so we
32- have a convenient place to store the _typemap, which converts
33- the type-specific functions for converting a value to the second
34- argument of the sort key.
35-
36- """
37- if use_key and self ._key is not None :
38- val = self ._key (val )
39-
40- try :
41- i , _typename = self ._typemap [val .__class__ ]
42- except KeyError :
43- # If this is not a type we have seen before, determine what
44- # to use for the second value in the tuple.
45- _type = val .__class__
46- _typename = _type .__name__
47- try :
48- # 1: Check if the type is comparable. In Python 3, sorted()
49- # uses "<" to compare objects.
50- val < val
51- i = 1
52- except :
53- try :
54- # 2: try converting the value to string
55- str (val )
56- i = 2
57- except :
58- # 3: fallback on id(). Not deterministic
59- # (run-to-run), but at least is consistent within
60- # this run.
61- i = 3
62- self ._typemap [_type ] = i , _typename
63- if i == 1 :
64- return _typename , val
65- elif i == 3 :
66- return _typename , tuple (self (v , use_key = False ) for v in val )
67- elif i == 2 :
68- return _typename , str (val )
69- else :
70- return _typename , id (val )
71-
7223def extend_to_allow_loading (cls : Type ):
7324 def new_init (self , * args , input_file = None , input_column = None , input_optional = None , ** kwargs ):
7425 self .__old_init__ (* args , ** kwargs )
0 commit comments