Add type hints and strict mypy#18
Merged
Merged
Conversation
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.
- 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
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.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
strict = truefrom __future__ import annotationsthroughout for postponed evaluationpy.typedmarker file for PEP 561 complianceTYPE_CHECKINGguards to resolve circular imports (Atom/Site, Lattice/LookupTable)Lattice.update()to avoid shadowing theatommodulecn_energiestype annotations to correctly reflect the 3-level nested dict structure at the lattice level vs 2-level at the site levelCluster.__init__to accept any iterable of sites