From 5f9ff1e9ebf09e39930fdacdeb7968e63e4c1ecf Mon Sep 17 00:00:00 2001 From: moe-ad Date: Thu, 23 Oct 2025 09:41:27 +0200 Subject: [PATCH 1/5] fix: design and type hints of custom collections --- src/ansys/dpf/core/__init__.py | 11 +++++------ src/ansys/dpf/core/collection.py | 29 ++++++++++++++++------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/ansys/dpf/core/__init__.py b/src/ansys/dpf/core/__init__.py index 08a256525f6..a7196cfa30f 100644 --- a/src/ansys/dpf/core/__init__.py +++ b/src/ansys/dpf/core/__init__.py @@ -109,17 +109,16 @@ from ansys.dpf.core.dpf_operator import available_operator_names -from ansys.dpf.core.collection import CollectionFactory as _CollectionFactory from ansys.dpf.core.collection import Collection as _Collection from ansys.dpf.core.label_space import LabelSpace # register classes for collection types: -CustomTypeFieldsCollection:type = _CollectionFactory(CustomTypeField) -GenericDataContainersCollection:type = _CollectionFactory(GenericDataContainer) -StringFieldsCollection:type = _CollectionFactory(StringField) -OperatorsCollection: type = _CollectionFactory(Operator) -AnyCollection:type = _Collection +CustomTypeFieldsCollection = _Collection.CollectionFactory(CustomTypeField) +GenericDataContainersCollection = _Collection.CollectionFactory(GenericDataContainer) +StringFieldsCollection = _Collection.CollectionFactory(StringField) +OperatorsCollection = _Collection.CollectionFactory(Operator) +AnyCollection = _Collection # for matplotlib # solves "QApplication: invalid style override passed, ignoring it." diff --git a/src/ansys/dpf/core/collection.py b/src/ansys/dpf/core/collection.py index 90405198e12..084edd7287c 100644 --- a/src/ansys/dpf/core/collection.py +++ b/src/ansys/dpf/core/collection.py @@ -25,13 +25,16 @@ from __future__ import annotations +from typing import Generic, Type, TypeVar + from ansys.dpf.core import errors, server as server_module from ansys.dpf.core.any import Any -from ansys.dpf.core.collection_base import TYPE, CollectionBase +from ansys.dpf.core.collection_base import CollectionBase from ansys.dpf.core.common import create_dpf_instance +TYPE = TypeVar('TYPE') -class Collection(CollectionBase[TYPE]): +class Collection(CollectionBase[TYPE], Generic[TYPE]): """Represents a collection of dpf objects organised by label spaces. Parameters @@ -120,16 +123,16 @@ def add_entry(self, label_space, entry): """ return super()._add_entry(label_space, Any.new_from(entry, server=self._server)) + @classmethod + def CollectionFactory(cls, subtype: TYPE) -> Type[Collection[TYPE]]: + """Create classes deriving from Collection at runtime for a given subtype.""" -def CollectionFactory(subtype, BaseClass=Collection): - """Create classes deriving from Collection at runtime for a given subtype.""" - - def __init__(self, **kwargs): - BaseClass.__init__(self, **kwargs) + def __init__(self, **kwargs): + cls.__init__(self, **kwargs) - new_class = type( - str(subtype.__name__) + "sCollection", - (BaseClass,), - {"__init__": __init__, "entries_type": subtype}, - ) - return new_class + new_class = type( + str(subtype.__name__) + "sCollection", + (cls,), + {"__init__": __init__, "entries_type": subtype}, + ) + return new_class From 50d72a3aff84bbc130d4f76a6ba43012a010467b Mon Sep 17 00:00:00 2001 From: moe-ad Date: Thu, 23 Oct 2025 09:43:26 +0200 Subject: [PATCH 2/5] fix: codestyle --- src/ansys/dpf/core/collection.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ansys/dpf/core/collection.py b/src/ansys/dpf/core/collection.py index 084edd7287c..9067e7bb541 100644 --- a/src/ansys/dpf/core/collection.py +++ b/src/ansys/dpf/core/collection.py @@ -32,7 +32,8 @@ from ansys.dpf.core.collection_base import CollectionBase from ansys.dpf.core.common import create_dpf_instance -TYPE = TypeVar('TYPE') +TYPE = TypeVar("TYPE") + class Collection(CollectionBase[TYPE], Generic[TYPE]): """Represents a collection of dpf objects organised by label spaces. From fc9857ed380f7e8aca93da75542bdb1703126ae8 Mon Sep 17 00:00:00 2001 From: moe-ad Date: Thu, 23 Oct 2025 10:08:00 +0200 Subject: [PATCH 3/5] fix: rename class method --- src/ansys/dpf/core/__init__.py | 8 ++++---- src/ansys/dpf/core/collection.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ansys/dpf/core/__init__.py b/src/ansys/dpf/core/__init__.py index a7196cfa30f..2894d2bd113 100644 --- a/src/ansys/dpf/core/__init__.py +++ b/src/ansys/dpf/core/__init__.py @@ -114,10 +114,10 @@ # register classes for collection types: -CustomTypeFieldsCollection = _Collection.CollectionFactory(CustomTypeField) -GenericDataContainersCollection = _Collection.CollectionFactory(GenericDataContainer) -StringFieldsCollection = _Collection.CollectionFactory(StringField) -OperatorsCollection = _Collection.CollectionFactory(Operator) +CustomTypeFieldsCollection = _Collection.collection_factory(CustomTypeField) +GenericDataContainersCollection = _Collection.collection_factory(GenericDataContainer) +StringFieldsCollection = _Collection.collection_factory(StringField) +OperatorsCollection = _Collection.collection_factory(Operator) AnyCollection = _Collection # for matplotlib diff --git a/src/ansys/dpf/core/collection.py b/src/ansys/dpf/core/collection.py index 9067e7bb541..660acfe1c25 100644 --- a/src/ansys/dpf/core/collection.py +++ b/src/ansys/dpf/core/collection.py @@ -125,7 +125,7 @@ def add_entry(self, label_space, entry): return super()._add_entry(label_space, Any.new_from(entry, server=self._server)) @classmethod - def CollectionFactory(cls, subtype: TYPE) -> Type[Collection[TYPE]]: + def collection_factory(cls, subtype: TYPE) -> Type[Collection[TYPE]]: """Create classes deriving from Collection at runtime for a given subtype.""" def __init__(self, **kwargs): From f0bc72c8056a328399eec9b35792e42122d6e6e9 Mon Sep 17 00:00:00 2001 From: moe-ad Date: Thu, 23 Oct 2025 11:06:16 +0200 Subject: [PATCH 4/5] fix: review suggestion --- src/ansys/dpf/core/collection.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ansys/dpf/core/collection.py b/src/ansys/dpf/core/collection.py index 660acfe1c25..45724a34a0a 100644 --- a/src/ansys/dpf/core/collection.py +++ b/src/ansys/dpf/core/collection.py @@ -127,13 +127,9 @@ def add_entry(self, label_space, entry): @classmethod def collection_factory(cls, subtype: TYPE) -> Type[Collection[TYPE]]: """Create classes deriving from Collection at runtime for a given subtype.""" - - def __init__(self, **kwargs): - cls.__init__(self, **kwargs) - new_class = type( str(subtype.__name__) + "sCollection", (cls,), - {"__init__": __init__, "entries_type": subtype}, + {"entries_type": subtype}, ) return new_class From dc4c274d0951b9dd8e6257c83be978c0edeed07d Mon Sep 17 00:00:00 2001 From: moe-ad Date: Thu, 23 Oct 2025 16:24:12 +0200 Subject: [PATCH 5/5] fix: add comment and docstring --- src/ansys/dpf/core/collection.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ansys/dpf/core/collection.py b/src/ansys/dpf/core/collection.py index 45724a34a0a..42af7b052a2 100644 --- a/src/ansys/dpf/core/collection.py +++ b/src/ansys/dpf/core/collection.py @@ -35,6 +35,7 @@ TYPE = TypeVar("TYPE") +# Explicit Generic[TYPE] helps some type checkers Collection as a generic. class Collection(CollectionBase[TYPE], Generic[TYPE]): """Represents a collection of dpf objects organised by label spaces. @@ -126,7 +127,34 @@ def add_entry(self, label_space, entry): @classmethod def collection_factory(cls, subtype: TYPE) -> Type[Collection[TYPE]]: - """Create classes deriving from Collection at runtime for a given subtype.""" + """Create classes deriving from Collection at runtime for a given subtype. + + This factory method dynamically creates a new class that inherits from Collection + and is specialized for storing entries of the specified subtype. + + Parameters + ---------- + subtype : type + Any recognized DPF type. For example, CustomTypeField, GenericDataContainer, + StringField, Operator, etc. This type will be used as the entries_type for + the new collection class. + + Returns + ------- + Type[Collection[TYPE]] + A new class that inherits from Collection and is specialized for the given + subtype. The class name will be "{subtype.__name__}sCollection". + + Examples + -------- + >>> from ansys.dpf.core.string_field import StringField + >>> from ansys.dpf.core.collection import Collection + >>> string_fields_collection = Collection.collection_factory(StringField)() + >>> string_fields_collection.__class__.__name__ + 'StringFieldsCollection' + >>> string_fields_collection.entries_type.__name__ + 'StringField' + """ new_class = type( str(subtype.__name__) + "sCollection", (cls,),