stac-utils / pystac

Python library for working with any SpatioTemporal Asset Catalog (STAC)

Home Page:https://pystac.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[RFC] Using `attrs` in pystac object definitions

jsignell opened this issue · comments

tl;dr the "subclass from dict" approach is more promising.

I just took a crack at using attrs to create pystac objects and wanted to write up some initial thoughts comparing that approach to the "subclass from dict" approach.

There are 3 main types of things stored on pystac objects:

  1. data in pre-defined fields
  2. data in arbitrary fields
  3. other objects that aren't part of the spec, but are useful to have around (validator, reader)

The "subclass from dict" approach is very good at 2 and 3, but is less good at 1
The attrs approach is very good at 1, not good at 2 and ok at 3.

So I was originally thinking maybe we can do both! Subclass from dict and extend it using attrs. But that doesn't really work because one of the main things that attrs wants to do is write the __init__.

My main finding is that attrs feels like a nice way to implement a data model, but if we are trying to store other non-spec attributes on the class (I'm looking at things like _stac_io) it starts to be less nice.

Pros:

  • data model is the primary feature of the class
  • equality, repr, serialization are all included

Cons:

  • any attribute on the class that should not be serialized needs to be explicitly excluded (or be private)
  • there are some extra hoops to jump through for generic fields