Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stubs for MeshTagsMetaClass #2219

Closed
wants to merge 5 commits into from
Closed

Add stubs for MeshTagsMetaClass #2219

wants to merge 5 commits into from

Conversation

jorgensd
Copy link
Sponsor Member

To make it possible to use mypy with external libraries.

Otherwise, one would get errors like:

library/file.py:74: error: "MeshTagsMetaClass" has no attribute "dim"
library/file.py:114: error: "MeshTagsMetaClass" has no attribute "indices"
library/file.py:114: error: "MeshTagsMetaClass" has no attribute "values"

Also add the correct ignores to make sure we catch other API changes.

jorgensd added a commit to jorgensd/asimov-contact that referenced this pull request May 31, 2022
Makes code easier by using `MeshTagsMetaClass.find` instead of numpy version
@jhale
Copy link
Member

jhale commented May 31, 2022

Perhaps you need to define an explicit setter for name?

@jorgensd
Copy link
Sponsor Member Author

Perhaps you need to define an explicit setter for name?

To do that I had to rename the C++ property name, otherwise I got a recursion error.. However, then the signature of MeshTagsMetaClass and cpp.MeshTags<T> are different, and dolfinx.mesh.meshtags_from_entities returns a different object than

@@ -121,7 +121,7 @@

msh.topology.create_connectivity(2, 0)
mt = meshtags_from_entities(msh, 2, create_adjacencylist(entities), values)
mt.name = "ball_d1_surface"
mt._name = "ball_d1_surface"
Copy link
Member

Choose a reason for hiding this comment

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

The underscore looks wrong in a demo - it's not idiomatic.

@@ -107,7 +107,7 @@ void declare_meshtags(py::module& m, std::string type)
}))
.def_property_readonly("dtype", [](const dolfinx::mesh::MeshTags<T>& self)
{ return py::dtype::of<T>(); })
.def_readwrite("name", &dolfinx::mesh::MeshTags<T>::name)
.def_readwrite("_name", &dolfinx::mesh::MeshTags<T>::name)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't look good. We should seek a proper solution before merging.

Copy link
Sponsor Member Author

@jorgensd jorgensd May 31, 2022

Choose a reason for hiding this comment

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

You cannot have the same name for a property exposed to Python that has a getter and setter (causes infinite recursion). This would be the natural way to expose variables in a pythonic way. The main problem is that the MeshTagMetaClass is not used for all constructors of meshtags in the Python layer, thus making it very hard to use typing and mypy for external libraries.

Copy link
Member

@jhale jhale Jun 3, 2022

Choose a reason for hiding this comment

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

One option instead of retyping the entire C++ module in python would be to automatically generate stubs (pyi files) for our pybind11 module:

https://mypy.readthedocs.io/en/stable/stubgen.html

or

https://github.com/sizmailov/pybind11-stubgen

However, I couldn't get it to work, admittedly only a five minute try.

Copy link
Contributor

Choose a reason for hiding this comment

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

stubgen -p cpp in the same location of cpp.so should do it.

Copy link
Sponsor Member Author

@jorgensd jorgensd Jun 6, 2022

Choose a reason for hiding this comment

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

The main issue is that the directory of cpp.so depends on the installation process (and should preferably be executed when installing the pybind layer in setup.py). I do not think we want users to have to generate stubs manually.

Copy link
Contributor

Choose a reason for hiding this comment

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

Usually those tools produce draft stubs which often require some manual updates, I thought those would be committed. Creating them on-the-fly in setup.py is also possible, but it would introduce dependencies. Additionally, if the package is already installed stubgen -p dolfinx.cpp will find it.

Copy link
Member

@jhale jhale Jun 30, 2022

Choose a reason for hiding this comment

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

@jorgensd Further to your comment #2219 (comment), do you not think that the 'fix' then would be to use the Pythonic constructors for MeshTags throughout?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I think that would be the correct way of fixing it. It would mean that we need to wrap up xdmf.read_meshtags
and meshtags_from_entities. I guess we would need to use the C++ copy constructor to transfer the data from C++ to Python, or do you have any other suggestion as to how we could do this in a nice way?

@jorgensd
Copy link
Sponsor Member Author

jorgensd commented Sep 6, 2022

Close this for now. Issue made: #2348

@jorgensd jorgensd closed this Sep 6, 2022
@jorgensd jorgensd deleted the dokken/mypy-meshtags branch May 22, 2024 06:06
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.

4 participants