pymatgen icon indicating copy to clipboard operation
pymatgen copied to clipboard

Typing for pymatgen

Open shyuep opened this issue 3 years ago • 1 comments

I think we should try to get at least the core aspects of pymatgen typed. Here are some thoughts:

  1. Numpy 1.20 has introduced the numpy.typing.ArrayLike type, which we can rely on for many instances. I created one in pymatgen.util.typing that uses ArrayLike where available and falls back to a default creation where it is not. One disadvantage of using the numpy ArrayLike is that it is extremely broad and does not distinguish between vectors or matrices. But the advantage is that we do not need to maintain anything. Not sure what is the best course here.
  2. I added the SpeciesLike and CompositionLike types. These are basically things that can be passed into get_el_sp or Composition() to return an Element/Species/DummySpecies or a Composition object respectively. CompositionLike is a superset of SpeciesLike since Species can be converted to just {sp: 1} compositions.
  3. We should minimize the types we create. The above + the core python types should cover 90% of use cases. But I welcome any suggestions of addition types we want to create.

shyuep avatar Feb 02 '21 16:02 shyuep

  1. I've been looking for solutions for this problem too. I found nptyping which defines a set of types for Numpy arrays of specific shapes. I'd rather not rely on a third-party here though. We can simply alias Vector and Matrix to ArrayLike for now, so at least it's easier to replace with an appropriate type later.
  2. Ok, sounds good.
  3. Agreed -- I think mixing of species/element in different places already causes some confusion. Avoiding custom types will make things simpler. The type hint I am using a lot however is Literal (as a substitute for str when only one of a few specific strings are valid), but this is in the standard library.

mkhorton avatar Feb 03 '21 05:02 mkhorton

I've been slowly adding types to the most frequently used parts of pymatgen over the last 2 years. Still far from complete coverage but core is mostly there which is what this issue was aiming for. Closing this as completed.

janosh avatar May 30 '23 16:05 janosh