Skip to content

wrote magic methods#2856

Open
DanZhul wants to merge 2 commits into
mate-academy:masterfrom
DanZhul:develop
Open

wrote magic methods#2856
DanZhul wants to merge 2 commits into
mate-academy:masterfrom
DanZhul:develop

Conversation

@DanZhul

@DanZhul DanZhul commented Jul 4, 2026

Copy link
Copy Markdown

No description provided.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review Summary

I am requesting changes because several behaviors of your Distance class still conflict with the task’s functional requirements and checklist. On app/main.py, your __str__ currently rounds the value; it must instead output exactly "Distance: {km} kilometers." using the raw self.km (see examples like Distance(20.123)), while rounding is only required for division results. In __mul__, around the lines where you set self.km and return self, you need to stop mutating the current instance and instead return a new Distance with km equal to the product (so distance2 = distance1 * 5 leaves distance1 unchanged and makes distance2 a separate instance). Your comparison methods (__lt__, __gt__, __eq__, __le__, __ge__) currently assume other is numeric only; they must also support Distance operands, e.g. distance < Distance(60) or distance == Distance(100), by checking whether other is a Distance and then comparing self.km to other.km. In __iadd__, there is an unnecessary else branch after handling the Distance case; per the checklist [CHECKLIST ITEM #X], you should return or continue directly after the if and remove the else, letting the numeric-handling code run in the fall-through path. Once you adjust the string formatting, multiplication semantics, comparison handling, and the else style issue, your solution will align well with the specification—your use of type annotations and most arithmetic logic is solid, so you’re very close to a fully correct implementation.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread app/main.py Outdated
self.km = km

def __str__(self) -> str:
return f"Distance: {round(self.km, 2)} kilometers."

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

__str__ is required to output exactly "Distance: {km} kilometers." using the stored km value. Rounding in __str__ is not mentioned in the description and will change outputs like Distance(20.123) vs the expected examples.

Comment thread app/main.py Outdated
def __iadd__(self, other: int | Distance) -> Distance:
if isinstance(other, Distance):
self.km += other.km
else:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This __iadd__ implementation uses an else that isn’t needed. Checklist item "Avoid unnecessary else statements" shows the pattern of returning / continuing directly after the if instead of using else. You can remove the else and adapt the flow accordingly.

Comment thread app/main.py
self.km += other
return self

def __mul__(self, other: int) -> Distance:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

__mul__ must return a new Distance instance with km set to the product, not mutate self. The description example uses distance2 = distance1 * 5 and then checks isinstance(distance2, Distance) and distance2.km == 100, which implies distance1 should stay unchanged.

Comment thread app/main.py Outdated
return self

def __mul__(self, other: int) -> Distance:
self.km *= other

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Inside __mul__, you currently modify self.km and return self. This violates the requirement that distance2 is a separate Distance instance resulting from the multiplication.

Comment thread app/main.py Outdated
def __truediv__(self, other: float | int) -> Distance:
return Distance(round((self.km / other), 2))

def __lt__(self, other: int) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comparison methods must support comparing to another Distance instance as well as to numbers. The description includes examples like distance < Distance(60) and distance > Distance(120). With the current annotation other: int and direct self.km < other, Distance operands will not be handled correctly. You need to accept both Distance and numeric types and compare using other.km when other is a Distance.

Comment thread app/main.py Outdated
def __lt__(self, other: int) -> bool:
return self.km < other

def __gt__(self, other: int) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similarly to __lt__, __gt__ must also support Distance operands (e.g. distance > Distance(120) from the description), not only int. Adjust the type annotation and logic to handle both Distance and numeric values.

Comment thread app/main.py Outdated
def __gt__(self, other: int) -> bool:
return self.km > other

def __eq__(self, other: int) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

__eq__ must support comparing to another Distance instance (distance == Distance(100)) as well as numbers. Currently it only accepts int and compares directly, which will fail when other is a Distance.

Comment thread app/main.py Outdated
def __eq__(self, other: int) -> bool:
return self.km == other

def __le__(self, other: int) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

__le__ must allow Distance vs Distance and Distance vs number comparisons (per the description examples), but it only supports int here. Consider mirroring the handling you add to __lt__.

Comment thread app/main.py Outdated
def __le__(self, other: int) -> bool:
return self.km <= other

def __ge__(self, other: int) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

__ge__ also needs to handle comparisons with another Distance instance in addition to numbers, like distance >= Distance(50) from the task description.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review Summary

Your Distance class implementation meets the functional and stylistic requirements for this task, so your solution is approved. The constructor correctly accepts a single km argument and stores it on self.km, and both __str__ and __repr__ produce the exact required string formats. Arithmetic methods (__add__, __iadd__, __mul__, __truediv__) behave as expected: they work with both other Distance instances and numeric values where required, return new Distance objects (except for the in-place add), and handle division with rounding to two decimal places. Your comparison methods properly support comparing Distance to Distance and Distance to numeric types by branching on isinstance(other, Distance) and falling back to numeric comparison. Type annotations and method signatures follow the constraints given, including excluding Distance as other in __mul__ and __truediv__, and avoiding unnecessary else blocks after returns. Overall, this is a clean, correct implementation that should pass the described tests and satisfy the checklist. Nice work on aligning the code closely with the requirements and handling the edge cases around arithmetic and comparisons.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

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