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

NummedObj.__eq__ breaks assumptions about object comparison #5

Open
exarkun opened this issue Mar 18, 2022 · 0 comments
Open

NummedObj.__eq__ breaks assumptions about object comparison #5

exarkun opened this issue Mar 18, 2022 · 0 comments

Comments

@exarkun
Copy link

exarkun commented Mar 18, 2022

A NummedObj instance cannot be compared for equality or non-equality against arbitrary objects:

>>> from pyutil.nummedobj import NummedObj
>>> class X(NummedObj): pass
... 
>>> x = X()
>>> x == 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nix/store/9ms6vnn4iakl4aix85ygc7ana5kshhj1-python3-3.9.6-env/lib/python3.9/site-packages/pyutil/nummedobj.py", line 41, in __eq__
    return (self._objid, self._classname,) == (other._objid, other._classname,)
AttributeError: 'int' object has no attribute '_objid'
>>> x != 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nix/store/9ms6vnn4iakl4aix85ygc7ana5kshhj1-python3-3.9.6-env/lib/python3.9/site-packages/pyutil/nummedobj.py", line 44, in __ne__
    return (self._objid, self._classname,) != (other._objid, other._classname,)
AttributeError: 'int' object has no attribute '_objid'
>>> 

But the typical assumption would be that since x is not very similar to 1, x == 1 would evaluate to False and x != 1 would evaluate to True.

This broken assumption causes problems in a number of places since it means NummedObj instances don't really implement the equality protocol, only a subset of it.

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

No branches or pull requests

1 participant