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
Add type annotations for io.vasp.outputs
#3776
base: master
Are you sure you want to change the base?
Add type annotations for io.vasp.outputs
#3776
Conversation
b82ab1c
to
db9729e
Compare
@janosh While you are working on improving dcostrings, can you please skip this |
I'm now facing significant resistance from the "default value as Default values for
|
@property | |
def net_magnetization(self): | |
"""Net magnetization from Chgcar""" | |
return np.sum(self.data["diff"]) if self.is_spin_polarized else None |
Such default values as None
would expand the type from float
to float | None
, meaning all downstream operations (or child objects) need to explicitly check for None
.
Default values for functions/methods
For example
pymatgen/pymatgen/io/vasp/outputs.py
Lines 223 to 235 in a16ac39
def __init__( | |
self, | |
filename: str | Path, | |
ionic_step_skip: int | None = None, | |
ionic_step_offset: int = 0, | |
parse_dos: bool = True, | |
parse_eigen: bool = True, | |
parse_projected_eigen: bool = False, | |
parse_potcar_file: bool = True, | |
occu_tol: float = 1e-8, | |
separate_spins: bool = False, | |
exception_on_bad_xml: bool = True, | |
) -> None: |
The default ionic_step_skip
is None
, which would require all downstream methods to handle this case. Using a default int
which will NOT normally happen during runtime could bypass this issue (for number of steps, -1
might be a good choice).
Some preliminary thoughts
- For sequence types, use an empty sequence might be quite straightforward (
""
forstr
,[]/()/{}
forlist/tuple/dict
). - For numbers, maybe use a number that would NOT happen during runtime (for example,
-1
for number of CPU cores).
Summary
Such cases are happening everywhere across the code base (for all kinds of types) and need to insert huge amount of checks if var is None
. I'm not sure how to resolve this gracefully (I cannot even describe this issue properly because it's happening everywhere and in various forms). Any suggestion would be appreciated @janosh.
definitely feel free to homogenize types where it make sense and simplifies the code. there are cases where it's important to be explicit about accepting |
fe42fa1
to
b83361c
Compare
Summary
io.vasp.outputs
, a follow-up PR of Add type annotations forio.vasp.inputs/optics
#3740.PathLike
as type overstr
for all paths.Vasprun/Xdatcar
inTrajectory
to be consistent with other part of the class.