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

Default fmt of IStructure.to doesn't agree with docstring #4305

Closed
DanielYang59 opened this issue Feb 27, 2025 · 0 comments · Fixed by #4306
Closed

Default fmt of IStructure.to doesn't agree with docstring #4305

DanielYang59 opened this issue Feb 27, 2025 · 0 comments · Fixed by #4306

Comments

@DanielYang59
Copy link
Contributor

DanielYang59 commented Feb 27, 2025

As per the IStructure.to docstring:

def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
"""Output the structure to a file or string.
Args:
filename (PathLike): If provided, output will be written to a file. If
fmt is not specified, the format is determined from the
filename. Defaults is None, i.e. string output.
fmt (str): Format to output to. Defaults to JSON unless filename
is provided. If fmt is specifies, it overrides whatever the
filename is. Options include "cif", "poscar", "cssr", "json",
"xsf", "mcsqs", "prismatic", "yaml", "yml", "fleur-inpgen", "pwmat",
"aims".
Non-case sensitive.

The default fmt should be json when filename is not provided.

However:

from pymatgen.core import Structure, Lattice


structure = Structure(
    lattice=Lattice.cubic(3),
    species=("Fe", "Fe"),
    coords=((0, 0, 0), (0.5, 0.5, 0.5)),
)

structure.to()

Would throw an error as the fmt is empty string instead of JSON:

Traceback (most recent call last):
  File "/Users/yang/developer/pymatgen/debug/test_structure_to.py", line 10, in <module>
    structure.to()
    ~~~~~~~~~~~~^^
  File "/Users/yang/developer/pymatgen/src/pymatgen/core/structure.py", line 3028, in to
    raise ValueError(f"Format not specified and could not infer from {filename=}")
ValueError: Format not specified and could not infer from filename=''

else:
if fmt == "":
raise ValueError(f"Format not specified and could not infer from {filename=}")
raise ValueError(f"Invalid {fmt=}, valid options are {get_args(FileFormats)}")

@DanielYang59 DanielYang59 changed the title Default format of IStructure.to doesn't agree with docstring Default fmt of IStructure.to doesn't agree with docstring Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant