diff --git a/.basedpyright/baseline.json b/.basedpyright/baseline.json index 269d96fe..ec19ef31 100644 --- a/.basedpyright/baseline.json +++ b/.basedpyright/baseline.json @@ -1886,32 +1886,40 @@ { "code": "reportUnknownVariableType", "range": { - "startColumn": 8, - "endColumn": 12, + "startColumn": 4, + "endColumn": 22, "lineCount": 1 } }, { - "code": "reportUnknownMemberType", + "code": "reportUnknownVariableType", "range": { - "startColumn": 8, - "endColumn": 33, + "startColumn": 4, + "endColumn": 22, "lineCount": 1 } }, { "code": "reportUnknownArgumentType", "range": { - "startColumn": 61, - "endColumn": 65, + "startColumn": 35, + "endColumn": 39, "lineCount": 1 } }, { "code": "reportUnknownArgumentType", "range": { - "startColumn": 67, - "endColumn": 71, + "startColumn": 41, + "endColumn": 45, + "lineCount": 1 + } + }, + { + "code": "reportUnknownVariableType", + "range": { + "startColumn": 51, + "endColumn": 55, "lineCount": 1 } }, @@ -8037,43 +8045,35 @@ "lineCount": 1 } }, - { - "code": "reportUnknownVariableType", - "range": { - "startColumn": 16, - "endColumn": 19, - "lineCount": 1 - } - }, { "code": "reportUnknownArgumentType", "range": { - "startColumn": 32, - "endColumn": 43, + "startColumn": 20, + "endColumn": 23, "lineCount": 1 } }, { - "code": "reportUnknownMemberType", + "code": "reportUnknownArgumentType", "range": { - "startColumn": 16, - "endColumn": 28, + "startColumn": 20, + "endColumn": 23, "lineCount": 1 } }, { - "code": "reportUnknownArgumentType", + "code": "reportUnknownVariableType", "range": { - "startColumn": 33, - "endColumn": 36, + "startColumn": 20, + "endColumn": 23, "lineCount": 1 } }, { "code": "reportUnknownArgumentType", "range": { - "startColumn": 51, - "endColumn": 56, + "startColumn": 36, + "endColumn": 47, "lineCount": 1 } }, @@ -8560,56 +8560,48 @@ { "code": "reportUnknownVariableType", "range": { - "startColumn": 20, - "endColumn": 28, + "startColumn": 16, + "endColumn": 27, "lineCount": 1 } }, { "code": "reportUnknownMemberType", "range": { - "startColumn": 32, - "endColumn": 46, + "startColumn": 31, + "endColumn": 72, "lineCount": 1 } }, { "code": "reportUnknownMemberType", "range": { - "startColumn": 32, - "endColumn": 58, + "startColumn": 40, + "endColumn": 63, "lineCount": 1 } }, { - "code": "reportUnknownMemberType", + "code": "reportUnknownVariableType", "range": { - "startColumn": 20, - "endColumn": 38, + "startColumn": 24, + "endColumn": 32, "lineCount": 1 } }, { "code": "reportUnknownMemberType", "range": { - "startColumn": 39, - "endColumn": 80, - "lineCount": 1 - } - }, - { - "code": "reportUnknownArgumentType", - "range": { - "startColumn": 39, - "endColumn": 82, + "startColumn": 36, + "endColumn": 50, "lineCount": 1 } }, { "code": "reportUnknownMemberType", "range": { - "startColumn": 48, - "endColumn": 71, + "startColumn": 36, + "endColumn": 62, "lineCount": 1 } }, @@ -10301,6 +10293,14 @@ "lineCount": 1 } }, + { + "code": "reportUnknownVariableType", + "range": { + "startColumn": 27, + "endColumn": 31, + "lineCount": 1 + } + }, { "code": "reportUnknownVariableType", "range": { @@ -11680,8 +11680,8 @@ { "code": "reportMissingTypeStubs", "range": { - "startColumn": 15, - "endColumn": 41, + "startColumn": 13, + "endColumn": 33, "lineCount": 1 } }, @@ -17967,6 +17967,14 @@ "lineCount": 1 } }, + { + "code": "reportUnknownVariableType", + "range": { + "startColumn": 33, + "endColumn": 41, + "lineCount": 1 + } + }, { "code": "reportUnknownVariableType", "range": { @@ -25680,24 +25688,24 @@ { "code": "reportUnknownMemberType", "range": { - "startColumn": 30, - "endColumn": 43, + "startColumn": 12, + "endColumn": 27, "lineCount": 1 } }, { - "code": "reportUnknownArgumentType", + "code": "reportUnknownMemberType", "range": { - "startColumn": 30, - "endColumn": 46, + "startColumn": 34, + "endColumn": 47, "lineCount": 1 } }, { - "code": "reportUnknownMemberType", + "code": "reportUnknownArgumentType", "range": { - "startColumn": 16, - "endColumn": 31, + "startColumn": 34, + "endColumn": 50, "lineCount": 1 } }, diff --git a/examples/from_firedrake.py b/examples/from_firedrake.py index 2ac23281..36a69572 100644 --- a/examples/from_firedrake.py +++ b/examples/from_firedrake.py @@ -107,7 +107,7 @@ def main(visualize=True): ax2.set_title("cos(x+y) in\nFiredrakeConnection 'on_boundary'") bdy_vis.show_scalar_in_matplotlib_3d(bdy_field, do_show=False) - import matplotlib.cm as cm + from matplotlib import cm fig.colorbar(cm.ScalarMappable(), ax=ax2) plt.show() diff --git a/examples/moving-geometry.py b/examples/moving-geometry.py index a2b6ebe4..645a794f 100644 --- a/examples/moving-geometry.py +++ b/examples/moving-geometry.py @@ -25,9 +25,8 @@ import numpy as np import pyopencl as cl -import pytools.obj_array as obj_array from arraycontext import Array, ArrayContext -from pytools import keyed_memoize_in +from pytools import keyed_memoize_in, obj_array from meshmode.array_context import PyOpenCLArrayContext from meshmode.discretization import ( diff --git a/examples/parallel-vtkhdf.py b/examples/parallel-vtkhdf.py index 73aef6c3..ed04ff20 100644 --- a/examples/parallel-vtkhdf.py +++ b/examples/parallel-vtkhdf.py @@ -5,7 +5,7 @@ from meshmode.mesh import Mesh -logger = logging.getLogger(__file__) +logger = logging.getLogger(__name__) def make_example_mesh(ambient_dim: int, nelements: int, order: int) -> Mesh: diff --git a/examples/simple-dg.py b/examples/simple-dg.py index 174820e5..eece661e 100644 --- a/examples/simple-dg.py +++ b/examples/simple-dg.py @@ -31,7 +31,6 @@ import pyopencl as cl import pyopencl.array as cla # noqa -import pytools.obj_array as obj_array from arraycontext import ( ArrayContainer, ArrayContext, @@ -39,7 +38,7 @@ map_array_container, with_container_arithmetic, ) -from pytools import log_process, memoize_method +from pytools import log_process, memoize_method, obj_array from pytools.obj_array import ObjectArray1D, ObjectArray2D from meshmode.array_context import PyOpenCLArrayContext, PytatoPyOpenCLArrayContext @@ -156,9 +155,7 @@ def get_connection(self, src, tgt): return self.all_faces_connection() elif src_tgt == ("vol", BTAG_ALL): return self.boundary_connection(tgt) - elif src_tgt == ("int_faces", "all_faces"): - return self.get_to_all_face_embedding(src) - elif src_tgt == (BTAG_ALL, "all_faces"): + elif src_tgt in (("int_faces", "all_faces"), (BTAG_ALL, "all_faces")): return self.get_to_all_face_embedding(src) else: raise ValueError(f"locations '{src}'->'{tgt}' not understood") diff --git a/meshmode/array_context.py b/meshmode/array_context.py index e27b9a05..9ac45932 100644 --- a/meshmode/array_context.py +++ b/meshmode/array_context.py @@ -277,12 +277,10 @@ def untag_loopy_call_results( else: return expr - dag = pt.transform.map_and_copy(dag, untag_loopy_call_results) + return pt.transform.map_and_copy(dag, untag_loopy_call_results) # }}} - return dag - @override def transform_loopy_program(self, t_unit: TranslationUnit): # FIXME: Do not parallelize for now. diff --git a/meshmode/discretization/__init__.py b/meshmode/discretization/__init__.py index 5b985ed2..56f2b059 100644 --- a/meshmode/discretization/__init__.py +++ b/meshmode/discretization/__init__.py @@ -35,9 +35,8 @@ import loopy as lp import modepy as mp -import pytools.obj_array as obj_array from arraycontext import Array, ArrayContext, make_loopy_program, tag_axes -from pytools import keyed_memoize_in, memoize_in, memoize_method +from pytools import keyed_memoize_in, memoize_in, memoize_method, obj_array # underscored because it shouldn't be imported from here. from meshmode.dof_array import DOFArray as _DOFArray diff --git a/meshmode/discretization/connection/chained.py b/meshmode/discretization/connection/chained.py index 1384c4d1..3dbfabd2 100644 --- a/meshmode/discretization/connection/chained.py +++ b/meshmode/discretization/connection/chained.py @@ -215,9 +215,8 @@ def flatten_chained_connection(actx, connection): # recursively build direct connections connections = connection.connections - direct_connections = [] - for conn in connections: - direct_connections.append(flatten_chained_connection(actx, conn)) + direct_connections = [ + flatten_chained_connection(actx, conn) for conn in connections] direct_connections = [conn for conn in direct_connections if not isinstance(conn, IdentityDiscretizationConnection)] diff --git a/meshmode/discretization/connection/projection.py b/meshmode/discretization/connection/projection.py index 46422f17..b6d6c8cd 100644 --- a/meshmode/discretization/connection/projection.py +++ b/meshmode/discretization/connection/projection.py @@ -67,9 +67,9 @@ def __new__(cls, connections, is_surjective=False): return cls(connections.connections, is_surjective=is_surjective) else: - conns = [] - for cnx in reversed(connections): - conns.append(cls(cnx, is_surjective=is_surjective)) + conns = [ + cls(cnx, is_surjective=is_surjective) + for cnx in reversed(connections)] return ChainedDiscretizationConnection(conns) @@ -217,9 +217,8 @@ def kproj(): sgrp = self.from_discr.groups[batch.from_group_index] # Generate the basis tabulation matrix - tabulations = [] - for basis_fn in sgrp.basis_obj().functions: - tabulations.append(basis_fn(batch.result_unit_nodes).flatten()) + tabulations = [basis_fn(batch.result_unit_nodes).flatten() + for basis_fn in sgrp.basis_obj().functions] tabulations = actx.from_numpy(np.asarray(tabulations)) # NOTE: batch.*_element_indices are reversed here because diff --git a/meshmode/discretization/visualization.py b/meshmode/discretization/visualization.py index bc2f72eb..ccd49c04 100644 --- a/meshmode/discretization/visualization.py +++ b/meshmode/discretization/visualization.py @@ -34,7 +34,6 @@ import numpy as np from typing_extensions import TypeIs, override -import pytools.obj_array as obj_array from arraycontext import ( Array, ArrayContext, @@ -43,7 +42,7 @@ flatten, ) from modepy.shapes import Hypercube, Shape, Simplex -from pytools import memoize_method +from pytools import memoize_method, obj_array from pytools.obj_array import ObjectArray, ObjectArray1D from meshmode.discretization.connection.direct import ( @@ -256,13 +255,10 @@ def _check_discr_same_connectivity( if len(discr.groups) != len(other.groups): return False - if not all( - sg.discretization_key() == og.discretization_key() - and sg.nelements == og.nelements - for sg, og in zip(discr.groups, other.groups, strict=True)): - return False - - return True + return all( + sg.discretization_key() == og.discretization_key() + and sg.nelements == og.nelements + for sg, og in zip(discr.groups, other.groups, strict=True)) # }}} @@ -345,7 +341,7 @@ def version(self) -> str: @property def simplex_cell_types(self) -> dict[int, int]: - import pyvisfile.vtk as vtk + from pyvisfile import vtk return { 1: vtk.VTK_LINE, 2: vtk.VTK_TRIANGLE, @@ -354,7 +350,7 @@ def simplex_cell_types(self) -> dict[int, int]: @property def tensor_cell_types(self) -> dict[int, int]: - import pyvisfile.vtk as vtk + from pyvisfile import vtk return { 1: vtk.VTK_LINE, 2: vtk.VTK_QUAD, @@ -462,7 +458,7 @@ def version(self) -> str: @property @override def simplex_cell_types(self) -> dict[int, int]: - import pyvisfile.vtk as vtk + from pyvisfile import vtk return { 1: vtk.VTK_LAGRANGE_CURVE, 2: vtk.VTK_LAGRANGE_TRIANGLE, @@ -472,7 +468,7 @@ def simplex_cell_types(self) -> dict[int, int]: @property @override def tensor_cell_types(self) -> dict[int, int]: - import pyvisfile.vtk as vtk + from pyvisfile import vtk return { 1: vtk.VTK_LAGRANGE_CURVE, 2: vtk.VTK_LAGRANGE_QUADRILATERAL, @@ -645,7 +641,7 @@ def _vis_nodes_numpy(self): def show_scalar_in_mayavi(self, field, **kwargs): # pylint: disable=import-error - import mayavi.mlab as mlab + from mayavi import mlab do_show = kwargs.pop("do_show", True) @@ -1125,7 +1121,7 @@ def _xdmf_nodes_numpy(self) -> ObjectArray1D[onp.Array2D[np.floating]]: stack=True, by_group=True) def _vtk_to_xdmf_cell_type(self, cell_type: int): - import pyvisfile.vtk as vtk + from pyvisfile import vtk from pyvisfile.xdmf import TopologyType return { vtk.VTK_LINE: TopologyType.Polyline, @@ -1301,7 +1297,7 @@ def show_scalar_in_matplotlib_3d(self, field, **kwargs): import matplotlib.pyplot as plt # This import also registers the 3D projection. - import mpl_toolkits.mplot3d.art3d as art3d + from mpl_toolkits.mplot3d import art3d do_show = kwargs.pop("do_show", True) vmin = kwargs.pop("vmin", None) diff --git a/meshmode/interop/firedrake/mesh.py b/meshmode/interop/firedrake/mesh.py index 5a24648a..ac9f1295 100644 --- a/meshmode/interop/firedrake/mesh.py +++ b/meshmode/interop/firedrake/mesh.py @@ -209,7 +209,7 @@ def _get_firedrake_boundary_tags(fdrake_mesh, tag_induced_boundary=False): def _get_facet_markers(dm, facets): # based on code removed in # https://github.com/firedrakeproject/firedrake/commit/9125a65c0cb5bb671c62c33f05a0d42b983e06ed - import firedrake.cython.dmcommon as dmcommon + from firedrake.cython import dmcommon ids = np.empty_like(facets) ids.fill(-1) diff --git a/meshmode/mesh/generation.py b/meshmode/mesh/generation.py index 085191a4..ff6bb085 100644 --- a/meshmode/mesh/generation.py +++ b/meshmode/mesh/generation.py @@ -1631,9 +1631,8 @@ def generate_box_mesh( AffineMap(offset=offset)), 1e-12*offset[idim])) - periodic_mesh = glue_mesh_boundaries(mesh, bdry_pair_mappings_and_tols) + return glue_mesh_boundaries(mesh, bdry_pair_mappings_and_tols) - return periodic_mesh else: return mesh @@ -1828,11 +1827,10 @@ def transform( aff_map = AffineMap(matrix, center - matrix @ center) from meshmode.mesh.processing import BoundaryPairMapping, glue_mesh_boundaries - periodic_mesh = glue_mesh_boundaries( + return glue_mesh_boundaries( mesh, bdry_pair_mappings_and_tols=[ (BoundaryPairMapping("-theta", "+theta", aff_map), 1e-12)]) - return periodic_mesh else: return mesh diff --git a/meshmode/mesh/processing.py b/meshmode/mesh/processing.py index ad7a3062..ffe531d6 100644 --- a/meshmode/mesh/processing.py +++ b/meshmode/mesh/processing.py @@ -119,7 +119,7 @@ def _compute_global_elem_to_part_elem( ``global_elem_to_part_elem[ielement, 1]`` gives its part-wide element index. """ global_elem_to_part_elem = np.empty((nelements, 2), dtype=element_id_dtype) - for part_id in part_id_to_elements.keys(): + for part_id in part_id_to_elements: elements = part_id_to_elements[part_id] global_elem_to_part_elem[elements, 0] = part_id_to_part_index[part_id] global_elem_to_part_elem[elements, 1] = np.indices( diff --git a/meshmode/mesh/tools.py b/meshmode/mesh/tools.py index 911f79ab..22eb075a 100644 --- a/meshmode/mesh/tools.py +++ b/meshmode/mesh/tools.py @@ -128,8 +128,7 @@ def rand_rotation_matrix(ambient_dim, deflection=1.0, randnums=None, rng=None): # Construct the rotation matrix ( V Transpose(V) - I ) R. - M = (np.outer(V, V) - np.eye(3)).dot(R) - return M + return (np.outer(V, V) - np.eye(3)).dot(R) # }}} @@ -316,13 +315,11 @@ def find_point_to_point_mapping( min_distance_sq_indices = np.argmin(distances_sq, axis=1) min_distances_sq = distances_sq[src_indices, min_distance_sq_indices] - src_idx_to_tgt_idx = np.where( + return np.where( min_distances_sq < tol**2, min_distance_sq_indices, -1) - return src_idx_to_tgt_idx - else: both_points = np.concatenate((tgt_points, src_points), axis=1) diff --git a/meshmode/mesh/visualization.py b/meshmode/mesh/visualization.py index b34691c8..f4e03c1a 100644 --- a/meshmode/mesh/visualization.py +++ b/meshmode/mesh/visualization.py @@ -84,10 +84,8 @@ def draw_2d_mesh( pathdata = [ (Path.MOVETO, (elverts[0, 0], elverts[1, 0])), ] - for i in range(1, elverts.shape[1]): - pathdata.append( - (Path.LINETO, (elverts[0, i], elverts[1, i])) - ) + pathdata.extend((Path.LINETO, (elverts[0, i], elverts[1, i])) + for i in range(1, elverts.shape[1])) pathdata.append( (Path.CLOSEPOLY, (elverts[0, 0], elverts[1, 0]))) diff --git a/pyproject.toml b/pyproject.toml index ba4320dd..78afbcce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -100,6 +100,8 @@ extend-ignore = [ "N818", # error suffix in exception names "UP032", # use f-strings instead of .format "RUF067", # no code in __init__ *shrug* + "TRY004", + "SIM102", ] [tool.ruff.lint.flake8-quotes] @@ -130,7 +132,8 @@ required-imports = ["from __future__ import annotations"] [tool.ruff.lint.per-file-ignores] "examples/*.py" = ["I002"] -"doc/conf.py" = ["I002"] +"doc/conf.py" = ["I002", "S102"] +"test/test_*.py" = ["S102"] [tool.pytest.ini_options] markers = [ diff --git a/test/test_array.py b/test/test_array.py index 45d3806c..9756226a 100644 --- a/test/test_array.py +++ b/test/test_array.py @@ -29,7 +29,6 @@ import numpy as np import pytest -import pytools.obj_array as obj_array from arraycontext import ( ArrayContextFactory, dataclass_array_container, @@ -37,6 +36,7 @@ pytest_generate_tests_for_array_contexts, with_container_arithmetic, ) +from pytools import obj_array from pytools.tag import Tag from meshmode import _acf # noqa: F401 diff --git a/test/test_chained.py b/test/test_chained.py index 5c42ba2a..6628e4b0 100644 --- a/test/test_chained.py +++ b/test/test_chained.py @@ -86,11 +86,9 @@ def create_discretization(actx, ndim, from meshmode.discretization.poly_element import ( InterpolatoryQuadratureSimplexGroupFactory, ) - discr = Discretization(actx, mesh, + return Discretization(actx, mesh, InterpolatoryQuadratureSimplexGroupFactory(order)) - return discr - def create_refined_connection(actx, discr, threshold=0.3): from meshmode.discretization.connection import make_refinement_connection @@ -105,11 +103,9 @@ def create_refined_connection(actx, discr, threshold=0.3): refiner.refine(flags) discr_order = discr.groups[0].order - connection = make_refinement_connection(actx, refiner, discr, + return make_refinement_connection(actx, refiner, discr, InterpolatoryQuadratureSimplexGroupFactory(discr_order)) - return connection - def create_face_connection(actx, discr): from meshmode.discretization.connection import FACE_RESTR_ALL, make_face_restriction @@ -118,13 +114,11 @@ def create_face_connection(actx, discr): ) discr_order = discr.groups[0].order - connection = make_face_restriction(actx, discr, + return make_face_restriction(actx, discr, InterpolatoryQuadratureSimplexGroupFactory(discr_order), FACE_RESTR_ALL, per_face_groups=True) - return connection - @pytest.mark.skip(reason="implementation detail") @pytest.mark.parametrize("ndim", [2, 3]) diff --git a/test/test_meshmode.py b/test/test_meshmode.py index c6112255..ec34c84f 100644 --- a/test/test_meshmode.py +++ b/test/test_meshmode.py @@ -31,12 +31,12 @@ import numpy.linalg as la import pytest -import pytools.obj_array as obj_array from arraycontext import ( ArrayContextFactory, flatten, pytest_generate_tests_for_array_contexts, ) +from pytools import obj_array import meshmode.mesh.generation as mgen from meshmode import _acf # noqa: F401 @@ -430,7 +430,7 @@ def f(x): h = 1/mesh_par elif mesh_name == "periodic": - assert dim == 2 or dim == 3 + assert dim in {2, 3} if dim == 2: mesh = mgen.generate_regular_rect_mesh( diff --git a/test/test_refinement.py b/test/test_refinement.py index 7627e91f..51253dff 100644 --- a/test/test_refinement.py +++ b/test/test_refinement.py @@ -216,8 +216,8 @@ def f(x): f_true = f(x_fine) if visualize == "dots": - import matplotlib.cm as cm import matplotlib.pyplot as plt + from matplotlib import cm x = x.get(actx.queue) err = np.array(np.log10( diff --git a/test/test_visualization.py b/test/test_visualization.py index 2d0fcc25..dcb7167e 100644 --- a/test/test_visualization.py +++ b/test/test_visualization.py @@ -31,8 +31,8 @@ import numpy as np import pytest -import pytools.obj_array as obj_array from arraycontext import ArrayContextFactory, pytest_generate_tests_for_array_contexts +from pytools import obj_array import meshmode.mesh.generation as mgen from meshmode import _acf # noqa: F401