Skip to content

chore: Replace Named Tuples with dataclass#5055

Draft
prmukherj wants to merge 3 commits intomainfrom
maint/replace_named_tuples_with_dataclass
Draft

chore: Replace Named Tuples with dataclass#5055
prmukherj wants to merge 3 commits intomainfrom
maint/replace_named_tuples_with_dataclass

Conversation

@prmukherj
Copy link
Copy Markdown
Collaborator

@prmukherj prmukherj commented Apr 6, 2026

Context

SurfaceFieldDataRequest, ScalarFieldDataRequest, VectorFieldDataRequest, and PathlinesFieldDataRequest were NamedTuple subclasses. NamedTuple property descriptors carry __doc__ directly, so _set_namedtuple_field_docs() could patch per-field Sphinx docs at runtime. After migration to @dataclass, docs were stored in cls.__dataclass_field_docs__ but nothing consumed that attribute, silently dropping per-field API docs.

Change Summary

  • field_data_interfaces.py: Converted the four request containers from NamedTuple to @dataclass with a _NamedTupleCompat mixin preserving _asdict(), _replace(), iteration, and indexing. Updated _set_namedtuple_field_docs() to store docs in cls.__dataclass_field_docs__ (since dataclass fields have no property descriptor to patch __doc__ on). Fixed PathlinesFieldDataRequest.zones to use default_factory=list.
  • field_data.py: Minor internal call-site updates to pass zones from kwargs directly.
  • doc/source/conf.py: Added _inject_dataclass_field_docs — an autodoc-process-docstring Sphinx hook that reads __dataclass_field_docs__ on any dataclass and injects per-field docs into empty attribute docstrings, restoring the Sphinx member-table output that previously worked via NamedTuple descriptors.

Rationale

The Sphinx hook is the minimal fix: it bridges the gap between dataclass fields (no descriptor __doc__) and Sphinx autodoc without requiring verbose dataclasses.field(metadata={"doc": ...}) rewrites across every field definition.

Impact

  • Sphinx/autodoc output for the four request dataclasses now renders per-field member documentation correctly.
  • Downstream code using _asdict(), _replace(), tuple unpacking, or index access continues to work unchanged via the _NamedTupleCompat mixin.

@prmukherj prmukherj requested a review from Copilot April 6, 2026 07:31
@github-actions github-actions bot added the enhancement Improve any current implemented feature label Apr 6, 2026
@codacy-production
Copy link
Copy Markdown

codacy-production bot commented Apr 6, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR migrates the field-data request “parameter container” types from NamedTuple to @dataclass, while attempting to preserve the legacy NamedTuple-style helper APIs (_asdict(), _replace(), iteration/indexing) for downstream compatibility.

Changes:

  • Replaced SurfaceFieldDataRequest, ScalarFieldDataRequest, VectorFieldDataRequest, and PathlinesFieldDataRequest from NamedTuple to @dataclass with a _NamedTupleCompat mixin.
  • Adjusted PathlinesFieldDataRequest.zones to use default_factory=list and updated a couple of internal call sites to pass zones directly from kwargs.
  • Updated the field-documentation helper to store docs in a dataclass-specific attribute.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/ansys/fluent/core/services/field_data.py Removes local zones temporary variables and forwards zones directly from kwargs in pathlines request construction.
src/ansys/fluent/core/field_data_interfaces.py Converts request containers to dataclasses and adds a compatibility layer to emulate prior NamedTuple conveniences.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +129 to +134
For dataclass fields, this stores documentation in a class attribute
that Sphinx and documentation tools can use to render field documentation
in attribute/member tables.
"""
for field_name, field_doc in field_docs.items():
getattr(cls, field_name).__doc__ = field_doc
# Store field documentation in a class attribute for Sphinx discovery
cls.__dataclass_field_docs__ = field_docs
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

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

_set_namedtuple_field_docs() now stores docs on cls.__dataclass_field_docs__, but that attribute doesn’t appear to be consumed anywhere else in the codebase (no references found). As-is, this likely won’t affect Sphinx/autodoc output and may regress the per-field documentation that previously worked via NamedTuple descriptors. Consider attaching docs via dataclasses.field(metadata={...}) (and wiring Sphinx to read it), or adding an explicit Sphinx hook/extension that reads __dataclass_field_docs__ and emits member docs.

Copilot uses AI. Check for mistakes.
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.

@copilot apply changes based on this feedback

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Added a Sphinx autodoc-process-docstring hook in doc/source/conf.py (commit 3122002) that reads the __dataclass_field_docs__ mapping stored on each dataclass and injects the per-field documentation into attribute docstrings during Sphinx processing. This restores the per-field member documentation that previously worked via NamedTuple property descriptors (which carried __doc__ directly on the descriptor object).

…_field_docs__

Agent-Logs-Url: https://github.com/ansys/pyfluent/sessions/465fae24-3285-4e8b-989d-30018468c47f

Co-authored-by: prmukherj <109645853+prmukherj@users.noreply.github.com>
Copilot AI changed the title chore: Replace Named Tuples with dataclass. chore: Replace Named Tuples with dataclass Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improve any current implemented feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants