Skip to content

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

Merged
tianyizheng02 merged 5 commits into
TheAlgorithms:masterfrom
Andrwaa:master
Dec 28, 2024
Merged

compare-method added to Vector class in lib.py#12448
tianyizheng02 merged 5 commits into
TheAlgorithms:masterfrom
Andrwaa:master

Conversation

@Andrwaa

@Andrwaa Andrwaa commented Dec 22, 2024

Copy link
Copy Markdown
Contributor

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".

Comment thread linear_algebra/src/lib.py Outdated
else:
return math.acos(num / den)

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

Copy link
Copy Markdown
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 thread linear_algebra/src/lib.py Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Andrwaa commented Dec 28, 2024

Copy link
Copy Markdown
Contributor Author

ok, i went back to the previous version

@tianyizheng02 tianyizheng02 left a comment

Copy link
Copy Markdown
Contributor

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
@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