Skip to content

Conversation

@marcorudolphflex
Copy link
Contributor

@marcorudolphflex marcorudolphflex commented Oct 22, 2025

This PR is a start towards static type checks with mypy.

It includes:

  • integration of mypy in pre-commit-hook
    • level 1: only checks on no-untyped-def
    • only in tidy3d/web
  • Adding "Any" to *arg and **kwargs arguments
  • Adding None return types if nothing or just None is returned
  • Adding types to all functions in tidy3d.web package

Notes
Review should be focussed on tidy3d/web files as the others were automatically annotated (Any args and None returns, see above).

The PR was made from here #2871 as there were already several merge conflicts with develop.

@greptile-apps
Copy link

greptile-apps bot commented Oct 22, 2025

Skipped: This PR changes more files than the configured file change limit: (100)

@github-actions
Copy link
Contributor

github-actions bot commented Oct 22, 2025

Diff Coverage

Diff: origin/develop...HEAD, staged and unstaged changes

  • tidy3d/main.py (100%)
  • tidy3d/components/autograd/derivative_utils.py (100%)
  • tidy3d/components/autograd/functions.py (100%)
  • tidy3d/components/base.py (100%)
  • tidy3d/components/base_sim/simulation.py (100%)
  • tidy3d/components/boundary.py (100%)
  • tidy3d/components/data/data_array.py (100%)
  • tidy3d/components/data/dataset.py (100%)
  • tidy3d/components/data/monitor_data.py (100%)
  • tidy3d/components/data/sim_data.py (100%)
  • tidy3d/components/data/unstructured/base.py (100%)
  • tidy3d/components/data/unstructured/tetrahedral.py (100%)
  • tidy3d/components/data/unstructured/triangular.py (100%)
  • tidy3d/components/eme/grid.py (100%)
  • tidy3d/components/eme/simulation.py (100%)
  • tidy3d/components/frequencies.py (100%)
  • tidy3d/components/geometry/base.py (100%)
  • tidy3d/components/geometry/mesh.py (100%)
  • tidy3d/components/geometry/primitives.py (100%)
  • tidy3d/components/geometry/utils.py (100%)
  • tidy3d/components/material/tcad/charge.py (100%)
  • tidy3d/components/material/tcad/heat.py (100%)
  • tidy3d/components/medium.py (100%)
  • tidy3d/components/microwave/path_integrals/integrals/base.py (100%)
  • tidy3d/components/microwave/path_integrals/specs/current.py (100%)
  • tidy3d/components/microwave/path_integrals/specs/voltage.py (100%)
  • tidy3d/components/mode/data/sim_data.py (100%)
  • tidy3d/components/mode/mode_solver.py (100%)
  • tidy3d/components/mode/simulation.py (100%)
  • tidy3d/components/mode/solver.py (100%)
  • tidy3d/components/monitor.py (100%)
  • tidy3d/components/scene.py (100%)
  • tidy3d/components/simulation.py (100%)
  • tidy3d/components/source/base.py (100%)
  • tidy3d/components/source/current.py (100%)
  • tidy3d/components/source/field.py (100%)
  • tidy3d/components/source/time.py (100%)
  • tidy3d/components/structure.py (100%)
  • tidy3d/components/tcad/data/monitor_data/abstract.py (100%)
  • tidy3d/components/tcad/data/monitor_data/charge.py (100%)
  • tidy3d/components/tcad/data/sim_data.py (100%)
  • tidy3d/components/tcad/doping.py (100%)
  • tidy3d/components/tcad/effective_DOS.py (100%)
  • tidy3d/components/tcad/simulation/heat_charge.py (100%)
  • tidy3d/components/types/base.py (100%)
  • tidy3d/components/validators.py (100%)
  • tidy3d/components/viz/axes_utils.py (100%)
  • tidy3d/components/viz/descartes.py (100%)
  • tidy3d/components/viz/flex_style.py (100%)
  • tidy3d/components/viz/plot_params.py (100%)
  • tidy3d/components/viz/plot_sim_3d.py (0.0%): Missing lines 26
  • tidy3d/exceptions.py (100%)
  • tidy3d/log.py (100%)
  • tidy3d/material_library/util.py (100%)
  • tidy3d/packaging.py (100%)
  • tidy3d/plugins/autograd/invdes/filters.py (100%)
  • tidy3d/plugins/autograd/invdes/parametrizations.py (100%)
  • tidy3d/plugins/design/design.py (100%)
  • tidy3d/plugins/design/method.py (100%)
  • tidy3d/plugins/design/result.py (100%)
  • tidy3d/plugins/dispersion/fit.py (100%)
  • tidy3d/plugins/invdes/design.py (100%)
  • tidy3d/plugins/invdes/penalty.py (100%)
  • tidy3d/plugins/invdes/region.py (100%)
  • tidy3d/plugins/invdes/result.py (100%)
  • tidy3d/plugins/invdes/transformation.py (100%)
  • tidy3d/plugins/invdes/utils.py (100%)
  • tidy3d/plugins/invdes/validators.py (0.0%): Missing lines 19
  • tidy3d/plugins/klayout/drc/drc.py (100%)
  • tidy3d/plugins/microwave/array_factor.py (100%)
  • tidy3d/plugins/pytorch/wrapper.py (100%)
  • tidy3d/plugins/smatrix/component_modelers/modal.py (100%)
  • tidy3d/plugins/smatrix/component_modelers/terminal.py (100%)
  • tidy3d/plugins/smatrix/ports/base_lumped.py (100%)
  • tidy3d/plugins/smatrix/ports/base_terminal.py (100%)
  • tidy3d/plugins/smatrix/ports/coaxial_lumped.py (100%)
  • tidy3d/plugins/smatrix/ports/rectangular_lumped.py (100%)
  • tidy3d/plugins/smatrix/run.py (100%)
  • tidy3d/plugins/smatrix/utils.py (100%)
  • tidy3d/updater.py (25.0%): Missing lines 299,304,313
  • tidy3d/web/api/autograd/autograd.py (100%)
  • tidy3d/web/api/autograd/backward.py (100%)
  • tidy3d/web/api/autograd/engine.py (100%)
  • tidy3d/web/api/autograd/io_utils.py (100%)
  • tidy3d/web/api/connect_util.py (100%)
  • tidy3d/web/api/container.py (90.9%): Missing lines 1336
  • tidy3d/web/api/material_libray.py (100%)
  • tidy3d/web/api/mode.py (100%)
  • tidy3d/web/api/tidy3d_stub.py (100%)
  • tidy3d/web/api/webapi.py (100%)
  • tidy3d/web/cache.py (100%)
  • tidy3d/web/cli/app.py (85.7%): Missing lines 79
  • tidy3d/web/cli/develop/documentation.py (80.0%): Missing lines 144
  • tidy3d/web/cli/develop/index.py (100%)
  • tidy3d/web/cli/develop/install.py (100%)
  • tidy3d/web/cli/develop/tests.py (100%)
  • tidy3d/web/cli/develop/utils.py (100%)
  • tidy3d/web/core/account.py (100%)
  • tidy3d/web/core/core_config.py (100%)
  • tidy3d/web/core/exceptions.py (100%)
  • tidy3d/web/core/http_util.py (100%)
  • tidy3d/web/core/s3utils.py (80.0%): Missing lines 279,367
  • tidy3d/web/core/stub.py (100%)
  • tidy3d/web/core/task_core.py (100%)
  • tidy3d/web/core/task_info.py (100%)
  • tidy3d/web/core/types.py (100%)

Summary

  • Total: 360 lines
  • Missing: 10 lines
  • Coverage: 97%

tidy3d/components/viz/plot_sim_3d.py

  22     with h5py.File(buffer, "r") as src:
  23         buffer2 = BytesIO()
  24         with h5py.File(buffer2, "w") as dst:
  25 
! 26             def copy_item(name, obj) -> None:
  27                 if isinstance(obj, h5py.Group):
  28                     dst.create_group(name)
  29                     for k, v in obj.attrs.items():
  30                         dst[name].attrs[k] = v

tidy3d/plugins/invdes/validators.py

  15 def ignore_inherited_field(field_name: str) -> typing.Callable:
  16     """Create validator that ignores a field inherited but not set by user."""
  17 
  18     @pd.validator(field_name, always=True)
! 19     def _ignore_field(cls, val) -> None:
  20         """Ignore supplied field value and warn."""
  21         if val is not None:
  22             td.log.warning(
  23                 f"Field '{field_name}' was supplied but the 'invdes' plugin will automatically "

tidy3d/updater.py

  295 @updates_from_version("1.4")
  296 def update_1_4(sim_dict: dict) -> dict:
  297     """Updates version 1.4."""
  298 
! 299     def fix_polyslab(geo_dict) -> None:
  300         """Fix a PolySlab dictionary."""
  301         geo_dict.pop("length", None)
  302         geo_dict.pop("center", None)
  303 
! 304     def fix_modespec(ms_dict) -> None:
  305         """Fix a ModeSpec dictionary."""
  306         sort_by = ms_dict.pop("sort_by", None)
  307         if sort_by and sort_by != "largest_neff":
  308             log.warning(

  309                 "ModeSpec.sort_by was removed in Tidy3D 1.5.0, reverting to sorting by "
  310                 "largest effective index. Use ModeSpec.filter_pol to select polarization instead."
  311             )
  312 
! 313     def fix_geometry_group(geo_dict) -> None:
  314         """Fix a GeometryGroup dictionary."""
  315         geo_dict.pop("center", None)
  316 
  317     update_types = {

tidy3d/web/api/container.py

  1332             if job.load_if_cached:
  1333                 job._materialize_from_stash(job_path)
  1334                 continue
  1335 
! 1336             def fn(job: Job = job, job_path: PathLike = job_path) -> None:
  1337                 job.download(path=job_path)
  1338 
  1339             fns.append(fn)

tidy3d/web/cli/app.py

  75     apikey : str
  76         User input api key.
  77     """
  78 
! 79     def auth(req: requests.Request) -> requests.Request:
  80         """Enrich auth information to request.
  81         Parameters
  82         ----------
  83         req : requests.Request

tidy3d/web/cli/develop/documentation.py

  140     submodule_path : str
  141         The relative path to the submodule.
  142     """
  143 
! 144     def commit_repository(repository_path: str, commit_message: str) -> None:
  145         """
  146         Commit changes in the specified Git repository.
  147 
  148         Args:

tidy3d/web/core/s3utils.py

  275                 task_id = progress.add_task(
  276                     "upload", filename=str(remote_filename), total=total_size
  277                 )
  278 
! 279                 def _callback(bytes_in_chunk: int) -> None:
  280                     progress.update(task_id, advance=bytes_in_chunk)
  281 
  282                 _upload(_callback)

  363                 total_size = meta_data.get("ContentLength", 0)
  364                 progress.start()
  365                 task_id = progress.add_task("download", filename=remote_basename, total=total_size)
  366 
! 367                 def _callback(bytes_in_chunk: int) -> None:
  368                     progress.update(task_id, advance=bytes_in_chunk)
  369 
  370                 _download(_callback)

@daquinteroflex
Copy link
Collaborator

Nice this is pretty exciting

@marcorudolphflex marcorudolphflex force-pushed the FXC-3573-static-type-checking-in-python-client branch 2 times, most recently from 764dded to 26ac0b6 Compare October 29, 2025 08:43
@marcorudolphflex marcorudolphflex force-pushed the FXC-3573-static-type-checking-in-python-client branch from 26ac0b6 to 46dbe02 Compare October 29, 2025 09:13
@marcorudolphflex marcorudolphflex force-pushed the FXC-3573-static-type-checking-in-python-client branch from 46dbe02 to d470e08 Compare October 29, 2025 10:46
Copy link
Collaborator

@yaugenst-flex yaugenst-flex left a comment

Choose a reason for hiding this comment

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

mypy tidy3d/web/
--> Success: no issues found in 47 source files

Niice! This is great. Lots of Any, but I guess that's for another stage to figure out 😬

@marcorudolphflex marcorudolphflex added this pull request to the merge queue Oct 29, 2025
Merged via the queue into develop with commit 31d3717 Oct 29, 2025
35 checks passed
@marcorudolphflex marcorudolphflex deleted the FXC-3573-static-type-checking-in-python-client branch October 29, 2025 12:10
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