-
Notifications
You must be signed in to change notification settings - Fork 474
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
Bug in compound unit dimensionality/unit reduction after caching? #876
Comments
I'm looking into it |
Huh, what the... After my change, >>> hash(1)
1
>>> hash(2)
2
>>> hash(-1)
-2
>>> hash(-2)
-2 |
So the fact that hash(-1) returns -2 is an implementation detail of CPython, because they use -1 for error codes: However, this is apparently working as intended. From https://docs.python.org/3/reference/datamodel.html#object.__hash__: > The only required property is that objects which compare equal have the same hash value and it turns out that, on dict and set insertion, when two objects have the same hash, their class C:
def __init__(self, x):
self.x = x
def __eq__(self, other):
print(f"{self}.__eq__({other})")
return self.x == other.x
def __hash__(self):
print(f"{self}.__hash__")
return hash(self.x)
def __repr__(self):
return f"C({self.x})"
set([C(-2), C(-1), C(0)]) output:
|
Related: https://bugs.python.org/issue38105 |
This is very weird bug. Thanks for being to fast to report it, track it and fix it. |
I came across a bizarre bug that popped up on the master branch recently:
gives the following traceback
but it works fine after removing the
joule
line.After digging through the commit history, it seems to lead back to a9a97ba, since any point in the history I checked including that commit fails with this error, and any without it returns the expected result of
1.0 meter
for the second line. Glancing through the changes made there, it makes sense that it is tied to this seemingly cache-related issue. That being said, I have no clue what is particularly going wrong here or what exactly in a9a97ba could have caused this.In case it helps with troubleshooting...
...the error no longer arises when any of the compound pieces of the last unit are removed.
...it still arises with the following:
...but does not arise with the following:
Another oddity is that, despite what the last non-failing example may suggest, this first arose in MetPy through a function that does not use
.to_base_units
, but rather just.to
. However, after spending a decent amount of time on it, I can't seem to come up with a short example that replicates the failure with.to
.ping @crusaderky, in case you may have any insight here with the seemingly problematic commit from #864.
The text was updated successfully, but these errors were encountered: