-
Notifications
You must be signed in to change notification settings - Fork 871
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.inputs/optics
#3740
Conversation
Warning Rate Limit Exceeded@DanielYang59 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 27 minutes and 11 seconds before requesting another review. How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. WalkthroughThis update primarily focuses on enhancing code clarity and reliability across the pymatgen library and associated scripts. Key changes include the introduction of explicit type annotations for variables and attributes, the addition of timeouts to HTTP requests to prevent hanging processes, and minor adjustments such as renaming variables and updating method signatures. These modifications aim to improve the maintainability and performance of the codebase, ensuring more robust and clear code structure. Changes
This table groups files with similar changes, focusing on type annotations and the addition of timeouts, which are prevalent throughout this update. Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
@janosh I re-visited this PR and realized it might not be a good idea to work on a single method globally like this, we should push the type annotation effort module by module. Though I agree My reason being: by working like this, we're just adding types for a single (or very few) function among many other un-typed functions. But as the types within a module heavily depend on others, it would either dig out huge amount of extra work, or make the type very loose. I would prefer close this and push our type-annotation effort module by module as discussed previously (starting for |
going folder by folder sounds good. no need to close this PR though, we can just start with i just reverted the initial ruff check . --select ANN204 --unsafe-fixes --fix and force pushed ruff check pymatgen/io/vasp --select ANN204 --unsafe-fixes --fix instead and keeping your manual fixes. you just need to run |
Good point. Let's do it.
There aren't many valuable fixes though. Thanks for the tip! |
ruff
auto typing of "ANN204"io.vasp
|
||
# If default_names is specified (usually coming from a POTCAR), use | ||
# them. This is in line with VASP's parsing order that the POTCAR | ||
# specified is the default used. | ||
if default_names: | ||
if default_names is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default value of default_names
seems inconsistent in:
pymatgen/pymatgen/core/structure.py
Lines 2860 to 2863 in 7f96ef2
elif fmt_low == "poscar": | |
from pymatgen.io.vasp import Poscar | |
struct = Poscar.from_str(input_string, default_names=False, read_velocities=False, **kwargs).structure |
Changed to None
in 1a1752a.
And
pymatgen/pymatgen/io/vasp/inputs.py
Lines 273 to 306 in 7f96ef2
@classmethod | |
def from_str(cls, data, default_names=None, read_velocities=True) -> Self: | |
""" | |
Reads a Poscar from a string. | |
The code will try its best to determine the elements in the POSCAR in | |
the following order: | |
1. If default_names are supplied and valid, it will use those. Usually, | |
default names comes from an external source, such as a POTCAR in the | |
same directory. | |
2. If there are no valid default names but the input file is VASP5-like | |
and contains element symbols in the 6th line, the code will use that. | |
3. Failing (2), the code will check if a symbol is provided at the end | |
of each coordinate. | |
If all else fails, the code will just assign the first n elements in | |
increasing atomic number, where n is the number of species, to the | |
Poscar. For example, H, He, Li, .... This will ensure at least a | |
unique element is assigned to each site and any analysis that does not | |
require specific elemental properties should work fine. | |
Args: | |
data (str): String containing Poscar data. | |
default_names ([str]): Default symbols for the POSCAR file, | |
usually coming from a POTCAR in the same directory. | |
read_velocities (bool): Whether to read or not velocities if they | |
are present in the POSCAR. Default is True. | |
Returns: | |
Poscar object. | |
""" |
I'm trying to relocate the class methods such that dunder methods and properties come first. But this seems to pollute git history. Wondering if there is a way to relocate codes without such side effect? |
io.vasp
io.vasp.inputs/optics
Can you please review this @janosh? I decided to separate the work for different modules as there are too many changes. Some return types are removed for completely un-typed There is one minor fix for the default value of UPDATE: Other than these two, I don't expect further functional changes (the unit test failure is related to the requested default value change, I would need your confirmation before I could change the unit test :) ). Thanks! |
not really. have a look at this video which doesn't solve your problem but is slightly relevant |
Thanks. It's always great to know more tips :) Should be ready for reviewing then. I reverted the default values to |
@@ -908,7 +908,7 @@ def __init__(self, permutations_safe_override=False, only_symbols=None): | |||
|
|||
self.minpoints = {} | |||
self.maxpoints = {} | |||
self.separations_cg = {} | |||
self.separations_cg: dict = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make the types more specific? list
/dict
/set
/... alone don't add any information for the reader. dict[str, CoordinationGeometry]
, list[Structure]
, etc. is more helpful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make the types more specific?
list
/dict
/set
/... alone don't add any information for the reader.dict[str, CoordinationGeometry]
,list[Structure]
, etc. is more helpful
Yes this is very true. But in this PR I was working on adding types for io.vasp
, sometimes mypy
would complain about other unrelated modules (with need annotation for some line
) which I haven't yet got time to look closer into. So in these case I just put a general type to stop mypy
from complaining (if I look closely into every module, then the work can never be done).
But I would keep this in mind and try to more specific types :)
pymatgen/analysis/structure_prediction/substitution_probability.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks so much @DanielYang59! 🙏
given the generic type annotations like var_name: list = []
are easy to find and fix later, we can merge this now
Thanks for reviewing!
Yes I don't know how |
Summary
io.vasp.inputs/optics
str
as path type withPathLike
Originally
Proposed by @janosh in #3739 (comment).
ruff check . --select ANN204 --unsafe-fixes
to auto-fix "ANN204", to experiment on auto typing fix withruff
.mypy
errors were dug out and need to be fixed.ANN204
refers to:Summary by CodeRabbit
New Features
Refactor
Bug Fixes