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

Improve clarity of simulation setups with typing.override #1291

Open
pschultzendorff opened this issue Dec 19, 2024 · 1 comment
Open

Improve clarity of simulation setups with typing.override #1291

pschultzendorff opened this issue Dec 19, 2024 · 1 comment
Labels
user group Issue to be worked on in the internal user group.

Comments

@pschultzendorff
Copy link
Contributor

pschultzendorff commented Dec 19, 2024

Python 3.12 introduced the typing.override decorator. This allows static type checkers to check whether a child class method/attribute actually overrides the method/attribute of one of the parent classes.

Setting up simulations in PorePy requires intensive subclassing of various mixins and overriding method/attributes. Wrongly named method/attributes in the subclasses cause the wrong (i.e., the parent's) methods/attributes to be called. This can happen easily and go unnoticed, but cause unwanted behavior and tedious debugging sessions. @override helps by type checking for such errors beforehand.

Example
The spelling error in the first method causes the model to run on the wrong domain size:

class ModifiedGeometry:
    def set_domian(self) -> None:
        size = self.units.convert_units(2, "m")
        self._domain = nd_cube_domain(2, size)

    def meshing_arguments(self) -> dict:
        cell_size = self.units.convert_units(0.25, "m")
        mesh_args: dict[str, float] = {"cell_size": cell_size}
        return mesh_args

class SinglePhaseFlowGeometry(
    ModifiedGeometry,
    SinglePhaseFlow):
    ...

model = SinglePhaseFlowGeometry({})
pp.run_time_dependent_model(model)

With the use of @override, mypy catches the error

from typing import override

class ModifiedGeometry:
    @override
    def set_domian(self) -> None:
.
.
.

>>> Method "set_domian" is marked as an override, but no base method was found with this name.

To make life easier, in particular for new users, the tutorials could make use of @override as a best practice and have some short explanation similar to above.

Would be happy to hear some opinions or ideas on this :) If this was already discussed internally and dismissed, let me know.

@keileg
Copy link
Contributor

keileg commented Dec 20, 2024

This is super interesting, @pschultzendorff, I can almost promise we will implement this. Thanks!

@IvarStefansson IvarStefansson added the user group Issue to be worked on in the internal user group. label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user group Issue to be worked on in the internal user group.
Projects
None yet
Development

No branches or pull requests

3 participants