Skip to content
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/4866.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add get and create methods and **kwarg support for create
Comment thread
Gobot1234 marked this conversation as resolved.
Comment thread
Gobot1234 marked this conversation as resolved.
2 changes: 2 additions & 0 deletions src/ansys/fluent/core/codegen/allapigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def generate(version: str, static_infos: dict, verbose: bool = False):
pickle.dump(api_tree, f)
if not config.codegen_skip_builtin_settings:
builtin_settingsgen.generate(version)
# Generate main .pyi file for the latest version processed
builtin_settingsgen.generate_main_pyi(version)


if __name__ == "__main__":
Expand Down
125 changes: 105 additions & 20 deletions src/ansys/fluent/core/codegen/builtin_settingsgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from ansys.fluent.core.utils.fluent_version import all_versions

_PY_FILE = config.codegen_outdir / "solver" / "settings_builtin.py"
_PYI_FILE = config.codegen_outdir / "solver" / "settings_builtin.pyi"


def _get_settings_root(version: str):
Expand Down Expand Up @@ -74,6 +73,29 @@ def _get_named_objects_in_path(root, path, kind):
return named_objects, final_type


def _has_create_method(root, path):
"""Check if a setting object has a create method."""
try:
cls = root
comps = path.split(".")
for comp in comps:
cls = cls._child_classes[comp]
# Check if the class has 'create' in its child classes or command names
return "create" in getattr(cls, "_child_classes", {}) or "create" in getattr(
cls, "command_names", []
Comment on lines +93 to +94
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can we comment why we check both of these. Otherwise, it looks speculative.

)
except (KeyError, AttributeError):
return False


def _get_reciprocal_name(name: str) -> str | None:
"""Get the reciprocal name (singular/plural counterpart) from DATA."""
try:
return DATA[name][2]
except KeyError:
return None


def generate(version: str):
"""Generate builtin setting classes."""
print("Generating builtin settings...")
Expand All @@ -87,14 +109,14 @@ def generate(version: str):
"from ansys.fluent.core.solver.flobject import SettingsBase\n\n\n"
)
f.write("__all__ = [\n")
for name, (kind, _) in DATA.items():
for name, (kind, _, _) in DATA.items():
f.write(f' "{name}",\n')
if kind == "Command":
command_name = _convert_camel_case_to_snake_case(name)
f.write(f' "{command_name}",\n')
f.write("]\n\n")
for name, v in DATA.items():
kind, path = v
kind, path, _ = v
if isinstance(path, dict):
version_supported = False
for version_set, p in path.items():
Expand Down Expand Up @@ -142,25 +164,88 @@ def generate(version: str):
)
f.write(" return instance(**kwargs)\n\n")

with open(_PYI_FILE, "w") as f:
for version in FluentVersion:
f.write(
f"from ansys.fluent.core.generated.solver.settings_{version.number} import root as settings_root_{version.number}\n"
)
# Generate version-specific .pyi files
pyi_file = (
config.codegen_outdir / "solver" / f"settings_builtin_{version.number}.pyi"
)
with open(pyi_file, "w") as f:
# Import base classes and deprecated decorator
f.write(
"from typing_extensions import deprecated\n"
"from ansys.fluent.core.solver.settings_builtin_bases import _SingletonSetting, _CreatableNamedObjectSetting, _NonCreatableNamedObjectSetting, _CommandSetting\n"
)
# Import version-specific root for type hints
f.write(
f"from ansys.fluent.core.generated.solver.settings_{version.number} import root as settings_root_{version.number}\n"
)
f.write("\n\n")
for name, v in DATA.items():
kind, path = v
f.write(f"class {name}(\n")
if isinstance(path, str):
path = {all_versions(): path}
for version_set, p in path.items():
if kind == "NamedObject":
p = f"{p}.child_object_type"
for v in reversed(list(version_set)):
f.write(f" type(settings_root_{v.number}.{p}),\n")
f.write("): ...\n\n")
kind, path, recip = v
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not easy to see what recip is.

if isinstance(path, dict):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not easy to see what the significance is of path being a dict, or what should be done if it is.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path here is a dict[FluentVersionSet, str]. It's only used by a small handful of things

        {
            since(FluentVersion.v251): "setup.dynamic_mesh",
        },

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it expected to be a dict always? IOW is it an error if a different type is encountered there?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's just a dict when there's a version constraint on the path and the name changes between versions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, please could you add that as a code comment.

version_supported = False
for version_set, p in path.items():
if version in version_set:
path = p
version_supported = True
break
if not version_supported:
continue
named_objects, final_type = _get_named_objects_in_path(root, path, kind)
if kind == "NamedObject":
kind = f"{final_type}NamedObject"
path_with_child = f"{path}.child_object_type"
f.write(f"class {name}(\n")
f.write(f" _{kind}Setting,\n")
f.write(
f" type(settings_root_{version.number}.{path_with_child}),\n"
)
f.write("):\n")
if final_type == "Creatable":
f.write(
f" create = settings_root_{version.number}.{path}.create\n"
)
else:
f.write(" ...\n")
f.write("\n")
else:
# For Singleton and Command types
# Check if this is a plural class by looking at its reciprocal
if kind == "Singleton" and recip:
# Add deprecated decorator for plural container classes
f.write(f'@deprecated("Use {recip}.all() instead")\n')

f.write(f"class {name}(\n")
f.write(f" _{kind}Setting,\n")
f.write(f" type(settings_root_{version.number}.{path}),\n")
f.write("):\n")
# Check if singleton has create method
if kind == "Singleton" and _has_create_method(root, path):
f.write(
f" create = settings_root_{version.number}.{path}.create\n"
)
else:
f.write(" ...\n")
f.write("\n")


def generate_main_pyi(version_str: str):
"""Generate main settings_builtin.pyi that imports from a specific version."""
_MAIN_PYI_FILE = config.codegen_outdir / "solver" / "settings_builtin.pyi"
version_obj = FluentVersion(version_str)
with open(_MAIN_PYI_FILE, "w") as f:
Comment thread
Gobot1234 marked this conversation as resolved.
f.write(f"# Re-export from version {version_str}\n")
f.write(
f"from ansys.fluent.core.generated.solver.settings_builtin_{version_obj.number} import *\n"
)


if __name__ == "__main__":
version = "261" # for development
generate(version)
# Generate for all available versions
versions = sorted([v.number for v in all_versions()])
Comment on lines +335 to +336
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions 'all available versions' but the actual code in if __name__ == '__main__' generates for all versions. This might cause unexpected behavior if run directly vs imported, as the main generation flow uses a single version. Consider clarifying or consolidating the version selection logic.

Copilot uses AI. Check for mistakes.
for version in versions:
try:
generate(str(version))
except Exception as e:
print(f"Failed to generate for version {version}: {e}")
# Generate main .pyi that imports from the latest version
generate_main_pyi(str(versions[-1]))
23 changes: 22 additions & 1 deletion src/ansys/fluent/core/codegen/settingsgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
ListObject,
NamedObject,
get_cls,
settings_logger,
to_constant_name,
to_python_name,
)
Expand Down Expand Up @@ -200,10 +201,19 @@ def _get_unique_name(name):

def _write_function_stub(name, data, s_stub):
s_stub.write(f" def {name}(self")
if name == "create":
if not data["argument_names"] or "name" not in data["argument_names"]:
settings_logger.warning("Create method with no arguments %s", data)
else:
s_stub.write(", *") # allow only keyword arguments as best practice
for arg_name in data["argument_names"]:
arg_type = data["child_classes"][arg_name]["bases"][0]
py_arg_type = _arg_type_strings.get(arg_type, "Any")
s_stub.write(f", {arg_name}: {py_arg_type}")
if name == "create" and py_arg_type == "Any":
continue # don't write the object arguments for create as they shouldn't be used
Comment thread
Gobot1234 marked this conversation as resolved.
s_stub.write(
f", {arg_name}: {py_arg_type}{' = ...' if name == 'create' else ''}"
)
s_stub.write("):\n")
# TODO: add return type
doc = data["doc"]
Expand Down Expand Up @@ -283,6 +293,17 @@ def _write_data(cls_name: str, python_name: str, data: dict, f: IO, f_stub: IO |
# to write only if it is not found in the _NAME_BY_HASH dict and avoid
# the _CLASS_WRITTEN set.
if k in command_names + query_names:

Comment thread
Gobot1234 marked this conversation as resolved.
# Special handling for create commands that only expose a "name" child.
# In this case, the actual arguments of the create operation are
# described by the associated child_object_type, not by the "name"
# placeholder itself. We therefore replace the argument_names and
# child_classes with those from child_object_type so that the
# generated stubs show the full set of creation parameters.
if k == "create" and v["child_classes"].keys() == {"name"}:
child_object_type = data["child_object_type"]
v["argument_names"] = child_object_type["child_names"]
v["child_classes"] = child_object_type["child_classes"]
_write_function_stub(k, v, s_stub)
classes_to_write[unique_name] = (child_python_name, v, hash_, False)
else:
Expand Down
16 changes: 11 additions & 5 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ def to_python_keys(cls, value):
query_names = []
_child_aliases = {}

def _create_child_object(self, cname: str):
def _create_child_object(self, cname: str, **kwargs: Any):
ret = self._objects.get(cname)
if not ret:
cls = self.__class__.child_object_type
Expand All @@ -1399,6 +1399,7 @@ def _create_child_object(self, cname: str):
"rename",
types.MethodType(lambda obj, name: _rename(self, name, cname), ret),
)
ret.set_state(kwargs)
return ret

def _update_objects(self):
Expand Down Expand Up @@ -1451,7 +1452,7 @@ def get_object_names(self):
obj_names_list = obj_names if isinstance(obj_names, list) else list(obj_names)
return obj_names_list

def __getitem__(self, name: str) -> ChildTypeT:
def __getitem__(self, name: str, **kwargs: Any) -> ChildTypeT:
Comment thread
Gobot1234 marked this conversation as resolved.
Comment thread
Gobot1234 marked this conversation as resolved.
if name not in self.get_object_names():
if self.flproxy.has_wildcard(name):
child_cls = self.__class__.child_object_type
Expand All @@ -1473,7 +1474,7 @@ def __getitem__(self, name: str) -> ChildTypeT:

obj = self._objects.get(name)
if not obj:
obj = self._create_child_object(name)
obj = self._create_child_object(name, **kwargs)
return obj

def get(self, name: str) -> ChildTypeT:
Expand All @@ -1494,6 +1495,10 @@ def get(self, name: str) -> ChildTypeT:
except Exception:
return

def all(self) -> list[ChildTypeT]:
"""Return all child objects."""
return list(self.values())

def __getattr__(self, name: str):
alias = self._child_aliases.get(name)
if alias:
Expand Down Expand Up @@ -1727,7 +1732,7 @@ def _get_new_keywords(obj, *args, **kwds):
newkwds[alias] = v
elif k in obj.argument_names:
newkwds[k] = v
else:
elif k not in obj.get_active_child_names():
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition may not correctly filter out child object parameters passed to create(). If a child name exists but is not 'active', valid kwargs might still be filtered out. Consider validating this logic or adding a comment explaining when child names are 'active'.

Copilot uses AI. Check for mistakes.
unknown_keywords.add(k)
for k in unknown_keywords:
# Noisily ignore unknown keywords
Expand Down Expand Up @@ -1843,7 +1848,8 @@ def execute_command(self, *args, **kwds):
and isinstance(self._parent, NamedObject)
and ret in self._parent
):
Comment thread
Gobot1234 marked this conversation as resolved.
return self._parent[ret]
child_attributes = {k: v for k, v in kwds.items() if k != "name"}
return self._parent.__getitem__(ret, **child_attributes)
return_t = getattr(self, "return_type", None)
if return_t:
base_t = _baseTypes.get(return_t)
Expand Down
Loading
Loading