-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add a hashcode implementation to SchemaField #3601
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
Conversation
|
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
|
I signed! |
|
CLAs look good, thanks! |
|
I was a bit confused by what is needed, but this example clarifies for me: >>> class A(object):
... def __init__(self, val):
... self.val = val
... def __repr__(self):
... return 'A({!r})'.format(self.val)
... def __eq__(self, other):
... if not isinstance(other, A):
... return NotImplemented
... return other.val == self.val
... def __ne__(self, other):
... if not isinstance(other, A):
... return NotImplemented
... return other.val != self.val
...
>>> a1 = A(10)
>>> a2 = A(11)
>>> a3 = A(11)
>>> a1 == a2
False
>>> a1 == a3
False
>>> a2 == a3
True
>>> set([a1, a2, a3])
set([A(10), A(11), A(11)]) |
| self.fields = fields | ||
|
|
||
| def __eq__(self, other): | ||
| def __key(self): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| self.field_type.lower(), | ||
| self.mode, | ||
| self.description, | ||
| self.fields) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| self.mode = mode | ||
| self.description = description | ||
| self.fields = fields | ||
| self.fields = None if(fields is None) else tuple(fields) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
@dhermes @tseaver i modified the default arg for FYI: this broke a few tests and I'm wondering if it will break any of the actual api calls. |
| info['description'] = field.description | ||
| if field.fields is not None: | ||
| info['fields'] = _build_schema_resource(field.fields) | ||
| info['fields'] = tuple(_build_schema_resource(field.fields)) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| """ | ||
| if 'fields' not in info: | ||
| return None | ||
| return () |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| def __hash__(self): | ||
| return hash(self._key()) | ||
|
|
||
| def __repr__(self): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
Hmm looks like I broke some tests in circleci, let me go fix that |
5dc1f46 to
66a3d66
Compare
|
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the |
|
@azymnis We (@jonparrott, @lukesneeringer and I) just had a discussion about this and determined Rather than put the burden on you, I have sent a commit into this PR to make the class immutable. |
lukesneeringer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One style nitpick.
bigquery/tests/unit/test_job.py
Outdated
| 'schema': {'fields': [ | ||
| {'name': 'full_name', 'type': 'STRING', 'mode': 'REQUIRED'}, | ||
| {'name': 'age', 'type': 'INTEGER', 'mode': 'REQUIRED'}, | ||
| {'name': |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| self._fields, | ||
| ) | ||
|
|
||
| def __eq__(self, other): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
66a3d66 to
8882df3
Compare
|
@dhermes makes sense, thanks for the update |
|
Thank you all! |
* Add a __hash__ implementation to SchemaField * Modify default list of subfields to be the empty tuple * Making SchemaField immutable. * Adding SchemaField.__ne__.
* Add a __hash__ implementation to SchemaField * Modify default list of subfields to be the empty tuple * Making SchemaField immutable. * Adding SchemaField.__ne__.
* Add a __hash__ implementation to SchemaField * Modify default list of subfields to be the empty tuple * Making SchemaField immutable. * Adding SchemaField.__ne__.
Problem: I need to compare a new schema with an existing table schema. Easiest way would be to use set comparison operators. However this is not possible with
SchemaFieldbecause there is no implementation of__hash__.Solution: Add a simple implementation of
__hash__using tuple keys. This is not the most efficient implementation of a hashcode but should probably be ok for most purposes.