Skip to content
Open
Show file tree
Hide file tree
Changes from 19 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.
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
12 changes: 9 additions & 3 deletions src/ansys/fluent/core/field_data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,12 @@ class _SurfaceNames:
def __init__(self, allowed_surface_names):
self._allowed_surface_names = allowed_surface_names

def allowed_values(self):
def all(self):
"""Lists available surface names."""
return list(self._allowed_surface_names())

allowed_values = all

def validate(self, surfaces: List[str]) -> bool:
"""
Validate that the given surfaces are in the list of allowed surface names.
Expand Down Expand Up @@ -327,10 +329,12 @@ class _SurfaceIds:
def __init__(self, allowed_surface_ids):
self._allowed_surface_ids = allowed_surface_ids

def allowed_values(self):
def all(self):
"""Lists available surface ids."""
return self._allowed_surface_ids()

allowed_values = all

def validate(self, surface_ids: List[int]) -> bool:
"""
Validate that the given surface IDs are in the list of allowed surface IDs.
Expand Down Expand Up @@ -366,10 +370,12 @@ def is_active(self, field_name):
return True
return False

def allowed_values(self):
def all(self):
"""Lists available scalar or vector field names."""
return list(self._available_field_names())

allowed_values = all

def __call__(self):
return self._available_field_names()

Expand Down
4 changes: 3 additions & 1 deletion src/ansys/fluent/core/rpvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __call__(self, var: str | None = None, val: Any | None = None) -> Any:
else (self._get_var(var) if var is not None else self._get_vars())
)

def allowed_values(self) -> List[str]:
def all(self) -> List[str]:
"""Returns list with the allowed rpvars names.

Returns
Expand All @@ -97,6 +97,8 @@ def allowed_values(self) -> List[str]:
)
return RPVars._allowed_values

allowed_values = all

def _get_var(self, var: str):
if var not in self.allowed_values():
raise RuntimeError(
Expand Down
4 changes: 3 additions & 1 deletion src/ansys/fluent/core/services/datamodel_se.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,10 +1523,12 @@ def false_if_none(val: bool | None) -> bool:
class PyTextual(PyParameter):
"""Provides interface for textual parameters."""

def allowed_values(self) -> list[str]:
def all(self) -> list[str]:
"""Get allowed values."""
return self.get_attr(Attribute.ALLOWED_VALUES.value)

allowed_values = all


class PyNumerical(PyParameter):
"""Provides interface for numerical parameters."""
Expand Down
12 changes: 7 additions & 5 deletions src/ansys/fluent/core/services/field_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,34 +361,36 @@ class _Arg:
def __init__(self, accessor):
self._accessor = accessor

def allowed_values(self):
def all(self):
"""Returns set of allowed values."""
if self._accessor.__class__.__name__ == "_AllowedScalarFieldNames":
warnings.warn(
"This usage is deprecated and will be removed in a future release. "
"Please use 'scalar_fields.allowed_values()' instead",
"Please use 'scalar_fields.all()' instead",
PyFluentDeprecationWarning,
)
elif self._accessor.__class__.__name__ == "_AllowedVectorFieldNames":
warnings.warn(
"This usage is deprecated and will be removed in a future release. "
"Please use 'vector_fields.allowed_values()' instead",
"Please use 'vector_fields.all()' instead",
PyFluentDeprecationWarning,
)
elif self._accessor.__class__.__name__ == "_AllowedSurfaceNames":
warnings.warn(
"This usage is deprecated and will be removed in a future release. "
"Please use 'field_data.surfaces.allowed_values()' instead",
"Please use 'field_data.surfaces.all()' instead",
PyFluentDeprecationWarning,
)
elif self._accessor.__class__.__name__ == "_AllowedSurfaceIDs":
warnings.warn(
"This usage is deprecated and will be removed in a future release. "
"Please use 'field_data.surface_ids.allowed_values()' instead",
"Please use 'field_data.surface_ids.all()' instead",
PyFluentDeprecationWarning,
)
return sorted(self._accessor())

allowed_values = all

def __init__(self, field_data_accessor, args_allowed_values_accessors):
self._field_data_accessor = field_data_accessor
for arg_name, accessor in args_allowed_values_accessors.items():
Expand Down
4 changes: 3 additions & 1 deletion src/ansys/fluent/core/services/solution_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,12 @@ class _Arg:
def __init__(self, accessor):
self._accessor = accessor

def allowed_values(self):
def all(self):
"""Get allowed values."""
return sorted(self._accessor())

allowed_values = all

def __init__(self, svar_accessor, args_allowed_values_accessors):
self._svar_accessor = svar_accessor
for arg_name, accessor in args_allowed_values_accessors.items():
Expand Down
Loading
Loading