Skip to content
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

Improve type annotations for core.(trajectory/units) #3832

Merged

Conversation

DanielYang59
Copy link
Contributor

@DanielYang59 DanielYang59 commented May 16, 2024

Summary

  • Improve type annotations for core.trajectory and core.units
  • Nest _get_si_unit and _check_mappings inside the only methods that call them in Unit

Extend the following to support argument "unit" as both str and Unit

  • __init/new__ of FloatWithUnit and ArrayWithUnit
  • get_conversion_factor of Unit, and to methods of FloatWithUnit and ArrayWithUnit

The type hint suggests str, docstring suggest Unit and implementation seems to handle partially of both.

def __init__(self, val: float | Number, unit: str, unit_type: str | None = None) -> None:
"""Initialize a float with unit.
Args:
val (float): Value
unit (Unit): A unit. e.g. "C".
unit_type (str): A type of unit. e.g. "charge"
"""
if unit_type is not None and str(unit) not in ALL_UNITS[unit_type]:
raise UnitError(f"{unit} is not a supported unit for {unit_type}")
self._unit = Unit(unit)

Might be good to support both for more flexibility, without sacrificing backwards compatibility.

fix `core.operations`

add types for site, mypy errors to fix

remove no_type_check decorator

move dunder methods to the top

fix mypy errors

remove debug tag

improve `spectrum`

finish `core.tensors`

finish `xcfunc`

fix hash of `SymmOp`

finish `core.trajectory`

Add types for `core.molecular_orbitals/operations/sites/spectrum/tensor/xcfunc`  (materialsproject#3829)

* fix `core.molecular_orbitals`

* fix `core.operations`

* add types for site, mypy errors to fix

* remove no_type_check decorator

* move dunder methods to the top

* fix mypy errors

* remove debug tag

* improve `spectrum`

* finish `core.tensors`

* finish `xcfunc`

* fix hash of `SymmOp`

* add a missing type

* Revert "fix hash of `SymmOp`"

This reverts commit bf2a953.

* fix some types in operations

* "TEST": remove one unused var, relocate another

* final tweak types

* avoid hard-code class name

* format tweaks

* type tweak

* fix unit test

* replace all `[0:x]` with `[:x]`

Guard `TYPE_CHECKING` only imports (materialsproject#3827)

* flake8 --select=TYP001

* fix type import

* format docstring to google style

* fix more unguarded typecheck imports

* format more docs to Google format

* trigger CI (better var names)

---------

Co-authored-by: Janosh Riebesell <janosh.riebesell@gmail.com>

move structures out of util dir (materialsproject#3831)

Add matgenb in the aux link section. Because apparently it is not
"discoverable". Fixes materialsproject#3811.

More updates.

Move AIMS notebooks to matgenb.

Add README in examples folder that point to proper resources.

Improve type annotations and comments for `io.cif` (materialsproject#3820)

* remove duplicate warning

* type and docstring tweaks

* add some types for io.cif

* add comments and types

* temp save of some formatting

* revert functional change

* revert unrelated changes

* fix unit test

* remove update that does nothing

* relocate `sub_space_group` and `space_groups` to their usage

* add more types

* pre-commit auto-fixes

* breaking: make parse_magmom/oxi_stats private

* remove merge header

* fix unit test

* add more types

* fix mypy errors

* add a few spaces

* remove if name ==main

* simplify "check to see if" in comments

* final tweaks

* revise docstring

* replace deprecated abc.abstractproperty

* add missing doc strings and standardize existing

* breaking: fix typo in method name orthongonalize_structure

* revert accidental change in test_composition.py

---------

Co-authored-by: Janosh Riebesell <janosh.riebesell@gmail.com>
@DanielYang59 DanielYang59 force-pushed the type-core-remain2-struct-unit-tracj branch from 43096cf to 1683c18 Compare May 16, 2024 12:36
pymatgen/core/units.py Outdated Show resolved Hide resolved
@DanielYang59 DanielYang59 changed the title Improve type annotations for core.(trajectory/units/structure) Improve type annotations for core.(trajectory/units) May 17, 2024
@DanielYang59 DanielYang59 changed the title Improve type annotations for core.(trajectory/units) Improve type annotations for core.(trajectory/units) May 17, 2024
@DanielYang59 DanielYang59 marked this pull request as ready for review May 17, 2024 12:33
@DanielYang59
Copy link
Contributor Author

Can I have this one reviewed please? @janosh

Copy link
Member

@janosh janosh left a comment

Choose a reason for hiding this comment

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

thanks @DanielYang59! 👍

@janosh janosh enabled auto-merge (squash) May 17, 2024 13:10
@janosh janosh added the tests Issues with or changes to the pymatgen test suite label May 17, 2024
@janosh janosh added linting Linting and quality assurance types Type all the things labels May 17, 2024
@janosh janosh merged commit 6a84d8a into materialsproject:master May 17, 2024
23 checks passed
@DanielYang59
Copy link
Contributor Author

Thanks for reviewing! Appreciate that.

@DanielYang59 DanielYang59 deleted the type-core-remain2-struct-unit-tracj branch May 17, 2024 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linting Linting and quality assurance tests Issues with or changes to the pymatgen test suite types Type all the things
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants