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

compare-method added to Vector class in lib.py #12448

Merged
merged 5 commits into from
Dec 28, 2024

Conversation

Andrwaa
Copy link
Contributor

@Andrwaa Andrwaa commented Dec 22, 2024

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@@ -183,6 +182,16 @@ def angle(self, other: Vector, deg: bool = False) -> float:
else:
return math.acos(num / den)

def __eq__(self, vector: object) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move the method higher up in the file, before all the public methods? Having this one dunder method within all the public methods, rather than having it alongside all the other dunder methods, makes it harder to find.

Comment on lines 185 to 190
def __eq__(self, vector: object) -> bool:
"""
performs the comparison between two vectors
"""
if not isinstance(vector, Vector):
return NotImplemented
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __eq__(self, vector: object) -> bool:
"""
performs the comparison between two vectors
"""
if not isinstance(vector, Vector):
return NotImplemented
def __eq__(self, vector: Vector) -> bool:
"""
performs the comparison between two vectors
"""

I don't think it makes sense to allow comparison with any object, only with other Vectors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried writing Vector insted of object but it gave me an error that saying: Argument 1 of "eq" is incompatible with supertype "object"; supertype defines the argument type as "object" [override]
This violates the Liskov substitution principle

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Dec 28, 2024
@tianyizheng02
Copy link
Contributor

linear_algebra/src/lib.py:98: note: This violates the Liskov substitution principle
linear_algebra/src/lib.py:98: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
linear_algebra/src/lib.py:98: note: It is recommended for "__eq__" to work with arbitrary objects, for example:
linear_algebra/src/lib.py:98: note:     def __eq__(self, other: object) -> bool:
linear_algebra/src/lib.py:98: note:         if not isinstance(other, Vector):
linear_algebra/src/lib.py:98: note:             return NotImplemented
linear_algebra/src/lib.py:98: note:         return <logic to compare two Vector instances>

Sorry for the inconvenience, I hadn't realized that mypy treated this as an error. Would you mind changing the code back to what you had originally, with the NotImplemented check? (The method's new location is fine.)

@Andrwaa
Copy link
Contributor Author

Andrwaa commented Dec 28, 2024

ok, i went back to the previous version

Copy link
Contributor

@tianyizheng02 tianyizheng02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for your changes!

@tianyizheng02 tianyizheng02 merged commit 2b58ab0 into TheAlgorithms:master Dec 28, 2024
5 checks passed
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Dec 28, 2024
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
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

Successfully merging this pull request may close these issues.

2 participants