Skip to content
Snippets Groups Projects
Commit 57ecedf6 authored by Michael Kuron's avatar Michael Kuron :mortar_board:
Browse files

Fix deepcopying on Python 3.11

Tested with Python 3.10 and 3.11 and Sympy 1.9 and 1.11
parent e356d3d7
No related branches found
No related tags found
No related merge requests found
Pipeline #53305 failed
from collections import defaultdict from collections import defaultdict
from functools import partial from functools import partial
import sys
from typing import Tuple, Union, Sequence from typing import Tuple, Union, Sequence
import numpy as np import numpy as np
...@@ -187,19 +188,16 @@ def get_type_of_expression(expr, ...@@ -187,19 +188,16 @@ def get_type_of_expression(expr,
# Fix for sympy versions from 1.9 # Fix for sympy versions from 1.9
sympy_version = sp.__version__.split('.') sympy_version = sp.__version__.split('.')
if int(sympy_version[0]) * 100 + int(sympy_version[1]) >= 109: sympy_version_int = int(sympy_version[0]) * 100 + int(sympy_version[1])
if sympy_version_int >= 109:
# __setstate__ would bypass the contructor, so we remove it # __setstate__ would bypass the contructor, so we remove it
if sympy_version_int >= 111:
del sp.Basic.__setstate__
del sp.Symbol.__setstate__
else:
sp.Number.__getstate__ = sp.Basic.__getstate__ sp.Number.__getstate__ = sp.Basic.__getstate__
del sp.Basic.__getstate__ del sp.Basic.__getstate__
class FunctorWithStoredKwargs:
def __init__(self, func, **kwargs):
self.func = func
self.kwargs = kwargs
def __call__(self, *args):
return self.func(*args, **self.kwargs)
# __reduce_ex__ would strip kwargs, so we override it # __reduce_ex__ would strip kwargs, so we override it
def basic_reduce_ex(self, protocol): def basic_reduce_ex(self, protocol):
if hasattr(self, '__getnewargs_ex__'): if hasattr(self, '__getnewargs_ex__'):
...@@ -210,9 +208,7 @@ if int(sympy_version[0]) * 100 + int(sympy_version[1]) >= 109: ...@@ -210,9 +208,7 @@ if int(sympy_version[0]) * 100 + int(sympy_version[1]) >= 109:
state = self.__getstate__() state = self.__getstate__()
else: else:
state = None state = None
return FunctorWithStoredKwargs(type(self), **kwargs), args, state return partial(type(self), **kwargs), args, state
sp.Number.__reduce_ex__ = sp.Basic.__reduce_ex__
sp.Basic.__reduce_ex__ = basic_reduce_ex sp.Basic.__reduce_ex__ = basic_reduce_ex
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment