Skip to content

Conversation

@moe-ad
Copy link
Contributor

@moe-ad moe-ad commented Oct 23, 2025

Closes #2288.

The following changes have been made:

  • Making the fact that Collection is also a generic type more explicit. From my investigation, this helps older type checker versions (especially on older python versions) infer types more accurately.
  • Make the CollectionFactory function a collection_factory class method of Collection. This is done for three main reasons:
    • It is makes more sense and it is more intuitive to bind it to the class, making it more obvious that it returns class objects deriving from Collection.
    • Eliminating the need to pass an explicit BaseClass argument. I couldn't think of a reason why a different argument other than Collection will be passed. @PProfizi what do you think?
    • To accurately type-hint the factory function, access to the type parameter of Collection is needed.
  • Corrections and improvements to the type hints.

With these changes, IDEs are now better able to suggest accurate autocompletion. Example in PyCharm:
image

The type inference is also much more accurate. For example, IDEs are able to infer that a StringFieldsCollection contains StringFields:
image

And because of that, the type of the object returned by a call to get_entry will be inferred correctly as StringField:
image

And only available methods of that object will be suggested through autocompletion:
image

@moe-ad moe-ad self-assigned this Oct 23, 2025
@codecov
Copy link

codecov bot commented Oct 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.36%. Comparing base (3323383) to head (0ae8d19).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2695   +/-   ##
=======================================
  Coverage   84.36%   84.36%           
=======================================
  Files          92       92           
  Lines       10890    10890           
=======================================
  Hits         9187     9187           
  Misses       1703     1703           

Copy link
Member

@jorgepiloto jorgepiloto left a comment

Choose a reason for hiding this comment

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

Just left some suggestions. Thanks for adding this, @moe-ad.

from ansys.dpf.core.collection_base import CollectionBase
from ansys.dpf.core.common import create_dpf_instance

TYPE = TypeVar("TYPE")
Copy link
Member

Choose a reason for hiding this comment

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

Let's use T as per the original PEP 484:

Suggested change
TYPE = TypeVar("TYPE")
T = TypeVar("T")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would prefer leaving this as is. Just to conform with the naming convention adopted for type parameters in other modules.

@PProfizi
Copy link
Contributor

@moe-ad could we maybe add a comment about the presence of Generic[TYPE], and a docstring example for the collection_factory static method?

@moe-ad
Copy link
Contributor Author

moe-ad commented Oct 23, 2025

@moe-ad could we maybe add a comment about the presence of Generic[TYPE], and a docstring example for the collection_factory static method?

@PProfizi I added dc4c274. Is that ok?

@PProfizi
Copy link
Contributor

@moe-ad could we maybe add a comment about the presence of Generic[TYPE], and a docstring example for the collection_factory static method?

@PProfizi I added dc4c274. Is that ok?

Yes that's perfect, thanks @moe-ad !

@github-actions
Copy link
Contributor

Some tests with 'continue-on-error: true' have failed:

  • PyDPF-Post API tests on ubuntu-latest

Created by continue-on-error-comment

@moe-ad moe-ad merged commit 09885a7 into main Oct 23, 2025
127 of 129 checks passed
@moe-ad moe-ad deleted the fix/design-and-type-hint-of-custom-collections branch October 23, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom collections do not show any available methods in IDE

3 participants