Skip to content
1 change: 1 addition & 0 deletions doc/changelog.d/4932.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Call .name if it's a SettingsBase + Path support
Comment thread
Copilot marked this conversation as resolved.
Outdated
5 changes: 3 additions & 2 deletions src/ansys/fluent/core/codegen/settingsgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ def _get_unique_name(name):
"Integer": "int",
"Real": "float | str",
"String": "str",
"Filename": "str",
"Filename": "PathType",
"BooleanList": "list[bool]",
"IntegerList": "list[int]",
"RealVector": "tuple[float | str, float | str, float | str]",
"RealList": "list[float | str]",
"StringList": "list[str]",
"FilenameList": "list[str]",
"FilenameList": "list[PathType]",
}


Expand Down Expand Up @@ -381,6 +381,7 @@ def generate(version: str, static_infos: dict, verbose: bool = False) -> None:
header.write("# This is an auto-generated file. DO NOT EDIT!\n")
header.write("#\n")
header.write("\n")
header.write("from ansys.fluent.core._types import PathType\n")
header.write("from ansys.fluent.core.solver.flobject import *\n\n")
header.write("from ansys.fluent.core.solver.flobject import (\n")
header.write(" _ChildNamedObjectAccessorMixin,\n")
Expand Down
44 changes: 39 additions & 5 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@

from __future__ import annotations

import collections
import collections.abc
from collections.abc import Sequence
from contextlib import contextmanager, nullcontext
import fnmatch
import hashlib
Expand All @@ -54,13 +55,15 @@
import sys
import types
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
ForwardRef,
Generic,
List,
NewType,
Protocol,
Tuple,
TypeVar,
Union,
Expand All @@ -71,6 +74,9 @@
import warnings
import weakref

from typing_extensions import override

from ansys.fluent.core._types import PathType
from ansys.fluent.core.pyfluent_warnings import (
PyFluentDeprecationWarning,
PyFluentUserWarning,
Expand Down Expand Up @@ -673,24 +679,36 @@ def units(self) -> str | None:
return get_si_unit_for_fluent_quantity(quantity)


class SettingsBaseWithName(Protocol): # pylint: disable=missing-class-docstring
__class__: type["SettingsBase"] # pyright: ignore[reportIncompatibleMethodOverride]
name: Callable[[], str]


class Textual(Property):
"""Exposes attribute accessor on settings object - specific to string objects."""

def set_state(self, state: StateT | None = None, **kwargs):
def set_state(
self,
state: str | VariableDescriptor | SettingsBaseWithName | None = None,
**kwargs,
):
"""Set the state of the object.

Parameters
----------
state
Either str or VariableDescriptor.
Either str, settings object with a ``name()`` method or VariableDescriptor.
kwargs : Any
Keyword arguments.

Raises
------
TypeError
If state is not a string.
If state is not an appropriate type.
"""
Comment thread
Gobot1234 marked this conversation as resolved.
if isinstance(state, SettingsBase) and hasattr(state, "name"):
state = state.name()
Comment thread
Gobot1234 marked this conversation as resolved.
Comment thread
Gobot1234 marked this conversation as resolved.
Comment thread
Gobot1234 marked this conversation as resolved.

allowed_types = (str, VariableDescriptor)

if not isinstance(state, allowed_types):
Expand Down Expand Up @@ -943,11 +961,17 @@ class String(SettingsBase[str], Textual):
set_state = Textual.set_state


class Filename(SettingsBase[str], Textual):
class Filename(SettingsBase[PathType], Textual):
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
"""A ``Filename`` object representing a file name."""

_state_type = str

@override
def set_state(self, state: PathType | None = None, **kwargs):
if state is not None:
state = os.fspath(state)
return super().set_state(state, **kwargs)
Comment thread
Gobot1234 marked this conversation as resolved.
Comment on lines +1108 to +1111

def file_purpose(self):
"""Specifies whether this file is used as input or output by Fluent."""
return self.get_attr(_InlineConstants.file_purpose)
Expand All @@ -958,6 +982,16 @@ class FilenameList(SettingsBase[StringListType], Textual):

_state_type = StringListType

if TYPE_CHECKING:

@override
def set_state(self, state: Sequence[PathType] | None = None, **kwargs): ...

Comment thread
Gobot1234 marked this conversation as resolved.
@override
def set_state(self, state: Sequence[PathType] | None = None, **kwargs):
if state is not None:
state = [os.fspath(path) for path in state]
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
return super().set_state(state, **kwargs)
Comment thread
Gobot1234 marked this conversation as resolved.
Comment thread
Gobot1234 marked this conversation as resolved.
def file_purpose(self):
"""Specifies whether this file is used as input or output by Fluent."""
return self.get_attr(_InlineConstants.file_purpose)
Expand Down
Loading