[modular] Allow method with the same name in case of @property decorator (#39308)

* fix

* add example

* fix

* Update modular_model_converter.py
This commit is contained in:
Cyril Vallez
2025-07-09 15:46:53 +02:00
committed by GitHub
parent 4798c05c64
commit 1cefb5d788
7 changed files with 264 additions and 8 deletions

View File

@@ -972,12 +972,37 @@ def replace_class_node(
# Use all original modeling attributes, and potentially override some with values in the modular
new_class_attributes = list({**original_modeling_class_attributes, **modular_class_attributes}.values())
original_modeling_methods = {
node.name.value: node for node in original_modeling_node.body.body if m.matches(node, m.FunctionDef())
}
modular_methods = {
node.name.value: node for node in modular_class_node.body.body if m.matches(node, m.FunctionDef())
}
# Check class methods defined in the modular and associated modeling
original_modeling_methods = {}
for node in original_modeling_node.body.body:
if m.matches(node, m.FunctionDef()):
# Due to the @property and @name.setter decorators, methods can sometimes have the same name, so we need a way
# to separate them
if node.name.value in original_modeling_methods:
# If it's already present, and the decorator is @property, it means the node already added was the setter
if node.decorators[0].decorator.value == "property":
original_modeling_methods[f"{node.name.value}_setter"] = original_modeling_methods[node.name.value]
original_modeling_methods[node.name.value] = node
# In this case current node is the setter
else:
original_modeling_methods[f"{node.name.value}_setter"] = node
else:
original_modeling_methods[node.name.value] = node
modular_methods = {}
for node in modular_class_node.body.body:
if m.matches(node, m.FunctionDef()):
# Due to the @property and @name.setter decorators, methods can sometimes have the same name, so we need a way
# to separate them
if node.name.value in modular_methods:
# If it's already present, and the decorator is @property, it means the node already added was the setter
if node.decorators[0].decorator.value == "property":
modular_methods[f"{node.name.value}_setter"] = modular_methods[node.name.value]
modular_methods[node.name.value] = node
# In this case current node is the setter
else:
modular_methods[f"{node.name.value}_setter"] = node
else:
modular_methods[node.name.value] = node
new_class_methods = []
# Iterate over the methods of the original modeling code, and add them to the list of methods to add