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 INCAR tag check #3621

Merged
merged 7 commits into from
Feb 24, 2024

Conversation

DanielYang59
Copy link
Contributor

@DanielYang59 DanielYang59 commented Feb 13, 2024

Summary

Some legacy from #3539.

  • Clean up INCAR parameters recording file incar_parameters.json to separate types and values.
  • Clean up INCAR checking mechanism accordingly.

TODOs

Enable checking of more complex type annotations like LATTICE_CONSTRAINTS : Sequence[bool, bool, bool]) suggested by @esoteric-ephemera in #3539 (comment).

@DanielYang59
Copy link
Contributor Author

I did some research and found typeguard seems to be what we're looking for.
For example:

from typeguard import check_type, CollectionCheckStrategy


check_type(
    value=[1, "hi"], expected_type=list[int],
    collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS
)

check_type(
    value=[1, "hi"], expected_type=list[int, int],
    collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS
)

check_type(
    value=[1, "hi"], expected_type=eval("list[int, int]"),
    collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS
)

However there are still some concerns to address:

  1. Is it essentially necessary to add an extra dependency typeguard to pymatgen
  2. I currently don't know any better approach to cast string to object ("list" to list) than eval, but in our case the source is internal (from file incar_parameters.json), so it should not lead to any securify issues
  3. type check each item might incur a performance penalty (Type checking on generic only checks first value entry in data structure  agronholm/typeguard#418 (comment)), thus we would need to discuss the necessity of this and have a switch for strict mode (check every item or just check the first item, where check first item of a collection is the default for typeguard).

@DanielYang59
Copy link
Contributor Author

DanielYang59 commented Feb 16, 2024

Can you give me some advice on this @janosh? Do you think it's necessary to have an item-wise type check for INCAR tags (and might need to include an extra dependency typeguard)? Or should we stick to a more general type check (list instead of list[int, int, int] for instance). Thanks for any input in advance.

@janosh
Copy link
Member

janosh commented Feb 16, 2024

Do you think it's necessary to have an item-wise type check for INCAR tags (and might need to include an extra dependency typeguard)?

i think adding typeguard as a dependency just for this is a tall ask. i'm guessing @shyuep will be strongly against.
unless there's a strong selling point i would also lean against

@DanielYang59
Copy link
Contributor Author

i think adding typeguard as a dependency just for this is a tall ask. i'm guessing @shyuep will be strongly against. unless there's a strong selling point i would also lean against

Yes I was thinking the same, adding a dependency to be used only once would be too much overhead. Actually this should be questions:

  • Do you think it would be necessary to perform item-wise check for INCAR tags (or just keep current more general type checking)? @janosh I'm pretty unsure and worried about the potential performance penalty here.
  • If so, including an extra dependency should certainly be discouraged. I would see if I could find another way around.

@janosh
Copy link
Member

janosh commented Feb 16, 2024

Do you think it would be necessary to perform item-wise check for INCAR tags

i don't think so. i don't remember a lot of GitHub issues about this.

@@ -2570,6 +2571,10 @@ def set_symbols(
self.extend(PotcarSingle.from_symbol_and_functional(el, functional) for el in symbols)


class UnknownPotcarWarning(UserWarning):
"""Warning raised when POTCAR hashes do not pass validation."""
Copy link
Contributor Author

@DanielYang59 DanielYang59 Feb 16, 2024

Choose a reason for hiding this comment

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

Relocated Warning class after its corresponding class for consistency.

@DanielYang59 DanielYang59 marked this pull request as ready for review February 16, 2024 08:35
@DanielYang59
Copy link
Contributor Author

i don't think so. i don't remember a lot of GitHub issues about this.

Let's keep the type checking unchanged then, and implement more careful checking when necessary. This PR should be good for review now @janosh . Thanks a lot!

@DanielYang59
Copy link
Contributor Author

Guess this PR get forgotten... Can you please review this @janosh ? Thanks.

@janosh janosh added io Input/output functionality vasp Vienna Ab initio Simulation Package types Type all the things labels Feb 24, 2024
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! lgtm :shipit:

@janosh janosh merged commit dc60d35 into materialsproject:master Feb 24, 2024
22 checks passed
@DanielYang59
Copy link
Contributor Author

Thanks for reviewing @janosh

@DanielYang59 DanielYang59 deleted the improve-incar-check branch February 24, 2024 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
io Input/output functionality types Type all the things vasp Vienna Ab initio Simulation Package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants