-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Python: Modernize 3 quality queries for comparison methods #20038
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
Open
joefarebrother
wants to merge
15
commits into
github:main
Choose a base branch
from
joefarebrother:python-qual-comparison
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fccdc30
Modernize incomplete ordering query
joefarebrother e71af8f
Move to subfolder
joefarebrother 4c5c4e0
Move inconsistentEquality and equals-hash-mismatch to subfolder
joefarebrother eb1b5a3
Modernize inconsistent equality
joefarebrother a687b60
Modernise equals-hash-mismatch
joefarebrother 8fb9bdd
move equals attr test to equals attr folder
joefarebrother 083d258
Add/update unit tests
joefarebrother 843a6c8
Remove total order check from equals not equals (doesn't make sense t…
joefarebrother 58f503d
Update docs for incomplete ordering + inconsistent hashing
joefarebrother ea48fcc
Update doc for equalsNotEquals
joefarebrother 61af4e4
Add changenote and update integraion test output
joefarebrother f784bb0
Fix qldoc errors + typos
joefarebrother 0f04a8b
Update integration test output
joefarebrother 15115f5
Remove old tests
joefarebrother 3a27758
Remove old py2-specific tests
joefarebrother File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update docs for incomplete ordering + inconsistent hashing
- Loading branch information
commit 58f503de38cbd8e2cd9dc07a209a6fdfb4fb4376
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 8 additions & 52 deletions
60
python/ql/src/Classes/Comparisons/examples/EqualsOrHash.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,8 @@ | ||
# Incorrect: equality method defined but class contains no hash method | ||
class Point(object): | ||
|
||
def __init__(self, x, y): | ||
self._x = x | ||
self._y = y | ||
|
||
def __repr__(self): | ||
return 'Point(%r, %r)' % (self._x, self._y) | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, Point): | ||
return False | ||
return self._x == other._x and self._y == other._y | ||
|
||
|
||
# Improved: equality and hash method defined (inequality method still missing) | ||
class PointUpdated(object): | ||
|
||
def __init__(self, x, y): | ||
self._x = x | ||
self._y = y | ||
|
||
def __repr__(self): | ||
return 'Point(%r, %r)' % (self._x, self._y) | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, Point): | ||
return False | ||
return self._x == other._x and self._y == other._y | ||
|
||
def __hash__(self): | ||
return hash(self._x) ^ hash(self._y) | ||
|
||
# Improved: equality method defined and class instances made unhashable | ||
class UnhashablePoint(object): | ||
|
||
def __init__(self, x, y): | ||
self._x = x | ||
self._y = y | ||
|
||
def __repr__(self): | ||
return 'Point(%r, %r)' % (self._x, self._y) | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, Point): | ||
return False | ||
return self._x == other._x and self._y == other._y | ||
|
||
#Tell the interpreter that instances of this class cannot be hashed | ||
__hash__ = None | ||
|
||
class A: | ||
def __init__(self, a, b): | ||
self.a = a | ||
self.b = b | ||
|
||
# No equality method is defined | ||
def __hash__(self): | ||
return hash((self.a, self.b)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
python/ql/src/Classes/Comparisons/examples/IncompleteOrdering.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
class IncompleteOrdering(object): | ||
class A: | ||
def __init__(self, i): | ||
self.i = i | ||
|
||
# BAD: le is not defined, so `A(1) <= A(2) would result in an error.` | ||
def __lt__(self, other): | ||
return self.i < other.i | ||
return self.i < other.i | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.