Skip to content
Open
Changes from all commits
Commits
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
44 changes: 43 additions & 1 deletion src/pyvmcon/problem.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Defines the interface for VMCON to exchange data with external software."""

import warnings
from abc import ABC, abstractmethod
from collections.abc import Callable
from collections.abc import Callable, Iterator
from dataclasses import dataclass, field
from typing import TypeVar, cast

Expand Down Expand Up @@ -37,6 +38,22 @@ class Result:
each input variable.
"""

def __iter__(self) -> Iterator[ScalarType | VectorType | MatrixType]:
"""Convert the dataclass into an iterable.

.. deprecated:: 2.4.2
Result is now a dataclass so should not be iterated over
(including tuple unpacking the object).
"""
warnings.warn(
"Using the Result class as a namedtuple is deprecated (e.g. by tuple "
"unpacking the object). Instead you should access individual attributes of "
"the dataclass.",
DeprecationWarning,
stacklevel=2,
)
return iter((self.f, self.df, self.eq, self.deq, self.ie, self.die))


class AbstractProblem(ABC):
"""A problem defines how VMCON gets data about your specific constrained system.
Expand Down Expand Up @@ -126,3 +143,28 @@ def num_equality(self) -> int:
def num_inequality(self) -> int:
"""The number of inequality constraints this problem has."""
return len(self.inequality_constraints)

def __iter__(
self,
) -> Iterator[_ScalarReturnFunctionAlias | _VectorReturnFunctionAlias]:
"""Convert the dataclass into an iterable.

.. deprecated:: 2.4.2
Problem is now a dataclass so should not be iterated over
(including tuple unpacking the object).
"""
warnings.warn(
"Using the Problem class as a namedtuple is deprecated (e.g. by tuple "
"unpacking the object). Instead you should access individual attributes of "
"the dataclass.",
DeprecationWarning,
stacklevel=2,
)
return iter((
self.f,
self.df,
self.equality_constraints,
self.inequality_constraints,
self.dequality_constraints,
self.dinequality_constraints,
))
Loading