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

Should more classes have __eq__ and/or __repr__ #222

Open
schwehr opened this issue Oct 28, 2020 · 1 comment
Open

Should more classes have __eq__ and/or __repr__ #222

schwehr opened this issue Oct 28, 2020 · 1 comment
Labels
best practice Issues that should be addressed for compliance with best practices, but that aren't technically bugs
Milestone

Comments

@schwehr
Copy link
Collaborator

schwehr commented Oct 28, 2020

Not all classes have __eq__ and __repr__. This was originally mentioned in #218 when I was writing tests for the Link class. It's okay to not have one or both of these in a class, but it's worth a quick discussion. __eq__ is mostly useful for testing. Can/should the __repr__ be something that can be pasted to create an instance of that thing? That seems tough with the structure of pystac.

find pystac -name \*.py | xargs grep '__eq__'
pystac/serialization/identify.py:    def __eq__(self, other):
pystac/serialization/identify.py:        return not self.__eq__(other)

and

find pystac -name \*.py | wc -l
28

find pystac -name \*.py | xargs grep --files-without-match '__repr__' | wc -l
20

find pystac -name \*.py | xargs grep '__repr__'
pystac/collection.py:    def __repr__(self):
pystac/extensions/pointcloud.py:    def __repr__(self):
pystac/extensions/pointcloud.py:    def __repr__(self):
pystac/extensions/label.py:    def __repr__(self):
pystac/extensions/label.py:    def __repr__(self):
pystac/extensions/eo.py:    def __repr__(self):
pystac/extensions/eo.py:    def __repr__(self):
pystac/link.py:    def __repr__(self):
pystac/catalog.py:    def __repr__(self):
pystac/item.py:    def __repr__(self):
pystac/item.py:    def __repr__(self):
pystac/serialization/identify.py:    def __repr__(self):
pystac/serialization/identify.py:    def __repr__(self):
@lossyrob lossyrob added the best practice Issues that should be addressed for compliance with best practices, but that aren't technically bugs label Oct 30, 2020
@duckontheweb
Copy link
Contributor

__eq__ is mostly useful for testing.

Related to #380.

This is also useful for checking whether some kind of collection contains an object. Right now, the only case of this that I know if is in ItemCollection, but it's possible that in the future we would want to define __contains__ on Collection and Catalog instances as well so we can do tests like:

item: Item = ...
collection: Collection = ...

if item in collection:
    # Do something interesting...
    ...

Can/should the __repr__ be something that can be pasted to create an instance of that thing? That seems tough with the structure of pystac.

Agreed, I think this would end up in a __repr__ that is too overwhelming. See the discussion in this comment in #331 for some options. Aligning the outputs of __repr__ with our definition of equality seems like a good way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
best practice Issues that should be addressed for compliance with best practices, but that aren't technically bugs
Projects
None yet
Development

No branches or pull requests

4 participants