Skip to content

Fix spglib >= 2.5 compatibility: convert SpglibDataset to plain dict#334

Open
abhinavships wants to merge 1 commit intousnistgov:masterfrom
abhinavships:fix/spglib-2x-compatibility
Open

Fix spglib >= 2.5 compatibility: convert SpglibDataset to plain dict#334
abhinavships wants to merge 1 commit intousnistgov:masterfrom
abhinavships:fix/spglib-2x-compatibility

Conversation

@abhinavships
Copy link

# Fix spglib >= 2.5 compatibility: SpglibDataset dataclass dict access

Closes #332 — "An update with spglib"


Problem

spglib >= 2.5.0 changed get_symmetry_dataset() to return a typed
SpglibDataset dataclass object instead of a plain Python dict.

JARVIS-Tools accesses the result throughout Spacegroup3D using
square-bracket (dict-style) syntax, for example:

dataset["international"]   # TypeError on spglib >= 2.5
dataset["number"]
dataset["wyckoffs"]

This raises TypeError: 'SpglibDataset' object is not subscriptable
for anyone running a modern spglib installation, breaking spacegroup
determination, CFID descriptors, and any downstream workflow that calls
Spacegroup3D.


Fix

Added a single small helper function _dataset_to_dict() at module
level (≈ 25 lines):

def _dataset_to_dict(dataset):
    if dataset is None:
        return None
    if isinstance(dataset, dict):      # spglib < 2.5 – already a dict
        return dataset
    import dataclasses
    if dataclasses.is_dataclass(dataset):   # spglib >= 2.5 dataclass
        return dataclasses.asdict(dataset)
    return dict(dataset)               # fallback for future changes

Then called it in exactly two places inside Spacegroup3D:

  1. __init__ — normalise the dataset argument before storing it.
  2. spacegroup_data() — normalise the return value of
    spglib.get_symmetry_dataset() before constructing a new
    Spacegroup3D.

No other code in the class was changed. The fix is fully backwards
compatible
— a plain dict passes through _dataset_to_dict()
unchanged, so users on older spglib versions are unaffected.


Testing

Verified locally with both spglib 1.16.5 and spglib 2.5.2:

coverage run -m pytest jarvis/tests/testfiles/analysis/structure/

All existing tests pass on both versions.


Files changed

  • jarvis/analysis/structure/spacegroup.py — added _dataset_to_dict();
    patched __init__ and spacegroup_data().

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 this pull request may close these issues.

An update with spglib

1 participant