adds numpy.NaN => np.nan conversion to the strategy updater since numpy2.0 changed that, now it throws errors.

To adjust more of those we can now easily expand on it.
This includes aliases for imports as well.
This commit is contained in:
hippocritical
2025-07-26 11:22:07 +02:00
parent 3ecc9c30cb
commit 52374b39f2
2 changed files with 32 additions and 4 deletions
+29 -4
View File
@@ -39,6 +39,17 @@ class StrategyUpdater:
"sell": "exit", "sell": "exit",
} }
# Update function names.
# example: `np.NaN` was removed in the NumPy 2.0 release. Use `np.nan` instead.
module_replacements = {
"numpy": {
"aliases": set(),
"replacements": [
("NaN", "nan"),
],
}
}
# create a dictionary that maps the old column names to the new ones # create a dictionary that maps the old column names to the new ones
rename_dict = {"buy": "enter_long", "sell": "exit_long", "buy_tag": "enter_tag"} rename_dict = {"buy": "enter_long", "sell": "exit_long", "buy_tag": "enter_tag"}
@@ -153,16 +164,24 @@ class NameUpdater(ast_comments.NodeTransformer):
def visit_Name(self, node): def visit_Name(self, node):
# if the name is in the mapping, update it # if the name is in the mapping, update it
node.id = self.check_dict(StrategyUpdater.name_mapping, node.id) node.id = self.check_dict(StrategyUpdater.name_mapping, node.id)
for mod, info in StrategyUpdater.module_replacements.items():
for old_attr, new_attr in info["replacements"]:
if node.id == old_attr:
node.id = new_attr
return node return node
def visit_Import(self, node): def visit_Import(self, node):
# do not update the names in import statements for alias in node.names:
if alias.name in StrategyUpdater.module_replacements:
as_name = alias.asname or alias.name
StrategyUpdater.module_replacements[alias.name]["aliases"].add(as_name)
return node return node
def visit_ImportFrom(self, node): def visit_ImportFrom(self, node):
# if hasattr(node, "module"): if node.module in StrategyUpdater.module_replacements:
# if node.module == "freqtrade.strategy.hyper": mod = node.module
# node.module = "freqtrade.strategy" StrategyUpdater.module_replacements[node.module]["aliases"].add(mod)
return node return node
def visit_If(self, node: ast_comments.If): def visit_If(self, node: ast_comments.If):
@@ -182,6 +201,12 @@ class NameUpdater(ast_comments.NodeTransformer):
and node.attr == "nr_of_successful_buys" and node.attr == "nr_of_successful_buys"
): ):
node.attr = "nr_of_successful_entries" node.attr = "nr_of_successful_entries"
if isinstance(node.value, ast_comments.Name):
for mod, info in StrategyUpdater.module_replacements.items():
if node.value.id in info["aliases"]:
for old_attr, new_attr in info["replacements"]:
if node.attr == old_attr:
node.attr = new_attr
return node return node
def visit_ClassDef(self, node): def visit_ClassDef(self, node):
+3
View File
@@ -42,8 +42,10 @@ def test_strategy_updater_methods(default_conf, caplog) -> None:
instance_strategy_updater = StrategyUpdater() instance_strategy_updater = StrategyUpdater()
modified_code1 = instance_strategy_updater.update_code( modified_code1 = instance_strategy_updater.update_code(
""" """
import numpy as np
class testClass(IStrategy): class testClass(IStrategy):
def populate_buy_trend(): def populate_buy_trend():
some_variable = np.NaN
pass pass
def populate_sell_trend(): def populate_sell_trend():
pass pass
@@ -62,6 +64,7 @@ class testClass(IStrategy):
assert "check_exit_timeout" in modified_code1 assert "check_exit_timeout" in modified_code1
assert "custom_exit" in modified_code1 assert "custom_exit" in modified_code1
assert "INTERFACE_VERSION = 3" in modified_code1 assert "INTERFACE_VERSION = 3" in modified_code1
assert "np.nan" in modified_code1
def test_strategy_updater_params(default_conf, caplog) -> None: def test_strategy_updater_params(default_conf, caplog) -> None: