-
-
Notifications
You must be signed in to change notification settings - Fork 179
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
Conversation
Makes code easier by using `MeshTagsMetaClass.find` instead of numpy version
Perhaps you need to define an explicit setter for name? |
To do that I had to rename the C++ property |
@@ -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" |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
Close this for now. Issue made: #2348 |
To make it possible to use
mypy
with external libraries.Otherwise, one would get errors like:
Also add the correct ignores to make sure we catch other API changes.