-
-
Notifications
You must be signed in to change notification settings - Fork 46
Include python demos in mypy check #1003
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
Open
risa-hirsh
wants to merge
2
commits into
FEniCS:main
Choose a base branch
from
risa-hirsh:include-python-demos-in-mypy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,20 @@ | |
| # | ||
| # First, we import Basix and Numpy. | ||
|
|
||
| import typing # For type checking | ||
|
|
||
| import numpy as np | ||
| import numpy.typing as npt | ||
|
|
||
| import basix | ||
| from basix import CellType, LatticeType, MapType, PolynomialType, PolysetType, SobolevSpace | ||
|
|
||
| # Aliases for type casting to maintain readability | ||
|
|
||
| FloatArray = npt.NDArray[np.float64] | ||
| FloatingArray = npt.NDArray[np.floating] | ||
| QuadratureRule = tuple[FloatArray, FloatArray] | ||
|
|
||
| # Lagrange element with bubble | ||
| # ============================ | ||
| # | ||
|
|
@@ -76,11 +85,14 @@ | |
| # the largest degree that the integrand will be, so these integrals will | ||
| # be exact). | ||
|
|
||
| pts, wts = basix.make_quadrature(CellType.quadrilateral, 4) | ||
| poly = basix.tabulate_polynomials(PolynomialType.legendre, CellType.quadrilateral, 2, pts) | ||
| x = pts[:, 0] | ||
| y = pts[:, 1] | ||
| f = x * (1 - x) * y * (1 - y) | ||
| pts, wts = typing.cast(QuadratureRule, basix.make_quadrature(CellType.quadrilateral, 4)) | ||
| poly = typing.cast( | ||
| FloatArray, | ||
| basix.tabulate_polynomials(PolynomialType.legendre, CellType.quadrilateral, 2, pts), | ||
| ) | ||
| x_coord = pts[:, 0] | ||
| y_coord = pts[:, 1] | ||
| f = x_coord * (1 - x_coord) * y_coord * (1 - y_coord) | ||
| for i in range(9): | ||
| wcoeffs[4, i] = sum(f * poly[i, :] * wts) | ||
|
|
||
|
|
@@ -100,7 +112,7 @@ | |
| # | ||
| # The shape of each of the point lists is (number of points, dimension). | ||
|
|
||
| x = [[], [], [], []] | ||
| x: list[list[FloatingArray]] = [[], [], [], []] | ||
| x[0].append(np.array([[0.0, 0.0]])) | ||
| x[0].append(np.array([[1.0, 0.0]])) | ||
| x[0].append(np.array([[0.0, 1.0]])) | ||
|
|
@@ -121,7 +133,7 @@ | |
| # The shape of each matrix is (number of DOFs, value size, number of | ||
| # points, number of derivatives). | ||
|
|
||
| M = [[], [], [], []] | ||
| M: list[list[FloatingArray]] = [[], [], [], []] | ||
| for _ in range(4): | ||
| M[0].append(np.array([[[[1.0]]]])) | ||
| M[2].append(np.array([[[[1.0]]]])) | ||
|
|
@@ -156,7 +168,7 @@ | |
|
|
||
| element = basix.create_custom_element( | ||
| CellType.quadrilateral, | ||
| [], | ||
| (), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a correction we should make. Related: it would be good to update the basix functions to accept any |
||
| wcoeffs, | ||
| x, | ||
| M, | ||
|
|
@@ -213,13 +225,16 @@ | |
| wcoeffs[0, 0] = 1 | ||
| wcoeffs[1, 3] = 1 | ||
|
|
||
| pts, wts = basix.make_quadrature(CellType.triangle, 2) | ||
| poly = basix.tabulate_polynomials(PolynomialType.legendre, CellType.triangle, 1, pts) | ||
| x = pts[:, 0] | ||
| y = pts[:, 1] | ||
| pts, wts = typing.cast(QuadratureRule, basix.make_quadrature(CellType.triangle, 2)) | ||
| poly = typing.cast( | ||
| FloatArray, | ||
| basix.tabulate_polynomials(PolynomialType.legendre, CellType.triangle, 1, pts), | ||
| ) | ||
| x_coord = pts[:, 0] | ||
| y_coord = pts[:, 1] | ||
| for i in range(3): | ||
| wcoeffs[2, i] = sum(x * poly[i, :] * wts) | ||
| wcoeffs[2, 3 + i] = sum(y * poly[i, :] * wts) | ||
| wcoeffs[2, i] = sum(x_coord * poly[i, :] * wts) | ||
| wcoeffs[2, 3 + i] = sum(y_coord * poly[i, :] * wts) | ||
|
|
||
| # Interpolation | ||
| # ------------- | ||
|
|
@@ -228,11 +243,11 @@ | |
| # the element are integrals. We begin by defining a degree 1 quadrature rule on an interval. | ||
| # This quadrature rule will be used to integrate on the edges of the triangle. | ||
|
|
||
| pts, wts = basix.make_quadrature(CellType.interval, 1) | ||
| pts, wts = typing.cast(QuadratureRule, basix.make_quadrature(CellType.interval, 1)) | ||
|
|
||
| # The points associated with each edge are calculated by mapping the quadrature points to each edge. | ||
|
|
||
| x = [[], [], [], []] | ||
| x = typing.cast(list[list[FloatingArray]], [[], [], [], []]) | ||
| for _ in range(3): | ||
| x[0].append(np.zeros((0, 2))) | ||
| x[1].append(np.array([[1 - p[0], p[0]] for p in pts])) | ||
|
|
@@ -245,7 +260,7 @@ | |
| # edge, and no extra derivatives are used. The entries of these matrices are the quadrature weights | ||
| # multiplied by the normal directions. | ||
|
|
||
| M = [[], [], [], []] | ||
| M = typing.cast(list[list[FloatingArray]], [[], [], [], []]) | ||
| for _ in range(3): | ||
| M[0].append(np.zeros((0, 2, 0, 1))) | ||
| for normal in [[-1, -1], [-1, 0], [0, 1]]: | ||
|
|
@@ -260,7 +275,7 @@ | |
|
|
||
| element = basix.create_custom_element( | ||
| CellType.triangle, | ||
| [2], | ||
| (2,), | ||
| wcoeffs, | ||
| x, | ||
| M, | ||
|
|
@@ -278,5 +293,8 @@ | |
|
|
||
| rt = basix.create_element(basix.ElementFamily.RT, CellType.triangle, 1) | ||
|
|
||
| points = basix.create_lattice(CellType.triangle, 1, LatticeType.equispaced, True) | ||
| points = typing.cast( | ||
| FloatArray, | ||
| basix.create_lattice(CellType.triangle, 1, LatticeType.equispaced, True), | ||
| ) | ||
| assert np.allclose(rt.tabulate(0, points), element.tabulate(0, points)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These are quite verbose changes to every demo for the sake of typing. What is the issue raised without them?
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.
Thanks for the feedback! The demos had 305 errors with the mypy check which I've attached in a text file: demo_mypy_errors.txt
The majority of errors across all of the demos come from Basix functions returning broad
ArrayLiketypes (these functions includemake_quadrature,create_lattice,tabulate,tabulate_polynomials). Mypy is treating these values as large unions (buffers, strings, nested sequences, etc.), which leads to errors when the demos use them as NumPy arrays, for example:pts[:, 0],poly[i, :])f * poly * wts)NDArraydemo_custom_element.py had the most errors (199) which also included:
x = [[], [], [], []]andM = [[], [], [], []]are inferred aslist[list[Never]], so mypy rejects later.append(np.array(...))calls and the latercreate_custom_element(..., x, M, ...)callsThe lines you commented on specifically are not necessary, I added them thinking it would keep things more readable but inline type casting would work just fine!
Uh oh!
There was an error while loading. Please reload this page.
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.
Right thanks. These are however exactly the errors we would like to resolve with this extended check, so ignoring them by casting to the expected types is not the desired resolution strategy. They show that the current type hints are insufficient. Rather we want to ensure that the type hints provided align with the use cases in the demos. For an example to resolve the errors related to
tabulate_polynomialsandmake_quadraturesee https://github.com/FEniCS/basix/tree/schnellerhase/fix-some-types (feel free to incorporate).