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

Raise Composition ValueError when passing float("NaN") #3518

Closed
janosh opened this issue Dec 15, 2023 · 0 comments · Fixed by #3519
Closed

Raise Composition ValueError when passing float("NaN") #3518

janosh opened this issue Dec 15, 2023 · 0 comments · Fixed by #3519
Labels
core Pymatgen core ux User experience

Comments

@janosh
Copy link
Member

janosh commented Dec 15, 2023

#3517 doesn't catch the "NaN" -> float("NaN") edge case

>>> Composition(float("NaN"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pymatgen/pymatgen/core/composition.py", line 127, in __init__
    elem_map = dict(*args, **kwargs)  # type: ignore
TypeError: 'float' object is not iterable
>>> float("NaN")
nan

I was going to add another check:

from math import isnan
...
        if len(args) == 1 and isinstance(args[0], Composition):
            elem_map = args[0]
        elif len(args) == 1 and isinstance(args[0], str):
            elem_map = self._parse_formula(args[0])  # type: ignore
        elif len(args) == 1 and isnan(args[0]):
            raise ValueError("float('NaN') is not a valid Composition, did you mean str('NaN')?")
        else:
            elem_map = dict(*args, **kwargs)  # type: ignore

Originally posted by @CompRhys in #3517 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Pymatgen core ux User experience
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant