Skip to content

Add type hints and strict mypy#18

Merged
bjmorgan merged 12 commits into
masterfrom
modernise/add-type-hints
Mar 4, 2026
Merged

Add type hints and strict mypy#18
bjmorgan merged 12 commits into
masterfrom
modernise/add-type-hints

Conversation

@bjmorgan

@bjmorgan bjmorgan commented Mar 4, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add comprehensive type hints to all 14 source files
  • Tighten mypy configuration from basic warnings to strict = true
  • Add from __future__ import annotations throughout for postponed evaluation
  • Add py.typed marker file for PEP 561 compliance
  • Use TYPE_CHECKING guards to resolve circular imports (Atom/Site, Lattice/LookupTable)
  • Rename local variable in Lattice.update() to avoid shadowing the atom module
  • Fix cn_energies type annotations to correctly reflect the 3-level nested dict structure at the lattice level vs 2-level at the site level
  • Widen Cluster.__init__ to accept any iterable of sites

bjmorgan added 9 commits March 4, 2026 15:09
Prepare for comprehensive type hints by adding postponed annotation
evaluation to all source files. Add PEP 561 py.typed marker and
configure setuptools to include it in package data.
Type the core data classes with full annotations. Use TYPE_CHECKING
guards to resolve the circular Atom/Site reference. Add assert guards
for p_neighbours in methods that iterate it.
Move all attribute initialisations into __init__ for clearer typing.
Rename local variable in update() to avoid shadowing the atom module.
Use Iterator[Site] for generator-returning methods.
Fix cn_energies nesting depth (3-level dict at lattice level, 2-level
at site level). Add assert guards for nullable attributes accessed in
hot paths. Widen Cluster constructor to accept any iterable.
Iterable and Iterator are available from collections.abc since
Python 3.9, and the project targets 3.11+. This is the more modern
import location.

This comment was marked as resolved.

- Fix BlockedLatticeError not calling super().__init__, so str(exc)
  now includes the error message
- Fix sites_at_edges() variable names swapped relative to their
  values (x_max was selecting sites at min_x, etc.) and return
  order now matches docstring [+x, -x, +y, -y, +z, -z]
- Fix truthiness checks in Simulation.set_nn_energy,
  set_cn_energies, set_site_energies to use 'is not None' so that
  0.0 and {} values are not silently ignored
- Improve ValueError message in LookupTable to include the invalid
  value and allowed values
- Fix typos: "lattce" -> "lattice", "equiibration" -> "equilibration"
- Fix copy-pasted docstring on Options.set_number_of_jumps
- Fix invalid dict syntax in Options.set_site_energies docstring

This comment was marked as resolved.

The previous code would raise an unhelpful TypeError when called
with an empty atoms list, because sum([]) returns 0 and
np.square(0) produces a scalar that sum() cannot iterate over.
@bjmorgan bjmorgan merged commit 1da421d into master Mar 4, 2026
5 checks passed
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.

2 participants