Skip to content

Commit

Permalink
using Union instead of pipe, for python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
igormcoelho committed Dec 7, 2023
1 parent f4cf3cc commit 61e58e7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions demo/03_QuickstartTSP_VNS_BRKGA/dev-mainTSP-fcore-brkga.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def generateRK(problemCtx: ProblemContextTSP, ptr_array_double : LibArrayDouble)


from optframe.core import LibArrayDouble
from typing import Tuple
from typing import Tuple, Union

import ctypes

Expand Down Expand Up @@ -217,7 +217,7 @@ def decodeSolution(pTSP: ProblemContextTSP, array_double : LibArrayDouble) -> So
return sol

@staticmethod
def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[SolutionTSP|None, float]:
def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[Union[SolutionTSP,None], float]:
#
# print("decodeMinimize! needsSolution="+str(needsSolution), flush=True)
sol = DecoderTSP.decodeSolution(pTSP, array_double)
Expand Down
4 changes: 2 additions & 2 deletions demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga-part3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from optframe.core import LibArrayDouble
from typing import Tuple
from typing import Tuple, Union

import ctypes

Expand Down Expand Up @@ -30,7 +30,7 @@ def decodeSolution(pTSP: ProblemContextTSP, array_double : LibArrayDouble) -> So
return sol

@staticmethod
def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[SolutionTSP|None, float]:
def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[Union[SolutionTSP,None], float]:
#
# print("decodeMinimize! needsSolution="+str(needsSolution), flush=True)
sol = DecoderTSP.decodeSolution(pTSP, array_double)
Expand Down
4 changes: 2 additions & 2 deletions demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def generateRK(problemCtx: ProblemContextTSP, ptr_array_double : LibArrayDouble)


from optframe.core import LibArrayDouble
from typing import Tuple
from typing import Tuple, Union

import ctypes

Expand Down Expand Up @@ -211,7 +211,7 @@ def decodeSolution(pTSP: ProblemContextTSP, array_double : LibArrayDouble) -> So
return sol

@staticmethod
def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[SolutionTSP|None, float]:
def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[Union[SolutionTSP,None], float]:
#
# print("decodeMinimize! needsSolution="+str(needsSolution), flush=True)
sol = DecoderTSP.decodeSolution(pTSP, array_double)
Expand Down
4 changes: 3 additions & 1 deletion optframe/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,9 @@ def add_decoder_rk(self, problemCtx, decoder_rk_callback):
self.callback_utils_decref_ptr)
return IdDecoderRandomKeysNoEvaluation(idx_dec)

def add_edecoder_op_rk_class(self, p : XProblem, decoder_rk: Type[XDecoderRandomKeysMinimize|XDecoderRandomKeysMaximize]):
# decoder_rk: Type[XDecoderRandomKeysMinimize|XDecoderRandomKeysMaximize]
# cannot make Union here... think about python 3.9 (pipe only after 3.10)
def add_edecoder_op_rk_class(self, p : XProblem, decoder_rk: Union[XDecoderRandomKeysMinimize,XDecoderRandomKeysMaximize]):
"""
Add a XDecoderRandomKeys class to the engine.
Expand Down
6 changes: 3 additions & 3 deletions optframe/protocols.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3

from typing import Protocol, runtime_checkable, Tuple
from typing import Protocol, runtime_checkable, Tuple, Union

# protocols should only depend on 'core'
from optframe.core import SearchOutput
Expand Down Expand Up @@ -95,13 +95,13 @@ def decodeSolution(p: XProblem, rk : LibArrayDouble) -> XSolution:
@runtime_checkable
class XDecoderRandomKeysMinimize(Protocol):
@staticmethod
def decodeMinimize(p: XProblem, rk : LibArrayDouble, needsSolution: bool) -> Tuple[XSolution|None, float]:
def decodeMinimize(p: XProblem, rk : LibArrayDouble, needsSolution: bool) -> Tuple[Union[XSolution,None], float]:
...

@runtime_checkable
class XDecoderRandomKeysMaximize(Protocol):
@staticmethod
def decodeMaximize(p: XProblem, rk : LibArrayDouble, needsSolution: bool) -> Tuple[XSolution|None, float]:
def decodeMaximize(p: XProblem, rk : LibArrayDouble, needsSolution: bool) -> Tuple[Union[XSolution,None], float]:
...

@runtime_checkable
Expand Down

0 comments on commit 61e58e7

Please sign in to comment.