added distance class solution#2863
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am requesting changes because there are still critical functional issues that prevent the class from behaving as the task requires. The in-place addition __iadd__ currently only accepts a Distance and then does self.km = other + self.km, which tries to add a Distance instance to a number and does not support += with plain int/float values; it should accept Distance | int | float and always update self.km using either other.km or other directly. All comparison methods (__lt__, __gt__, __eq__, __le__, __ge__) only accept Distance in their annotations and then compare self.km to other instead of other.km, and they don’t support comparing to numbers, so expressions like distance < 60 or distance == other_distance won’t behave correctly and must be fixed to handle both Distance and numeric operands. Your type annotations use Distance before the class is defined without adding from __future__ import annotations, which can cause a NameError and violates the requirement to avoid that issue [CHECKLIST ITEM #2]; either add the future import at the top of app/main.py or adjust the annotations to use string/union types. The constructor currently restricts km to int, but the task describes division results like 2.85, so km (and related methods like __mul__ and __truediv__) should allow float as well to keep behavior consistent with non-integer distances.
To summarize the critical fixes:
- Update
__iadd__to handle bothDistanceandint/float, and make sure it updatesself.kmusingother.kmwhenotheris aDistance. - Update all comparison methods to accept
Distance | int | float, useother.kmforDistanceinstances, and compare directly to numericothervalues to support examples likedistance < 60anddistance >= 42.5. - Add
from __future__ import annotationsat the top of the file or change the forward-reference type hints so they don’t causeNameError[CHECKLIST ITEM #2]. - Broaden your type hints for
kmand numeric operations to supportfloatresults, consistent with the division behavior.
You’ve done good work on__str__,__repr__, standard__add__,__mul__, and__truediv__, so once you address these blocking issues, your solution should fully meet the task requirements.
✨ 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
| class Distance: | ||
| # Write your code here | ||
| pass | ||
| def __init__(self, km: int) -> None: |
There was a problem hiding this comment.
__init__ limits km to int, but the task requires supporting results like 2.85 after division, so km should accept float as well to keep Distance.km consistent with later operations.
| def __repr__(self) -> str: | ||
| return f"Distance(km={self.km})" | ||
|
|
||
| def __add__(self, other: Distance | int) -> Distance: |
There was a problem hiding this comment.
This violates checklist item #2: "Fix NameError: name 'Distance' is not defined due to annotations" because you annotate arguments and return types with Distance but don’t add from __future__ import annotations at the top, which can cause a NameError in some Python versions.
| def __lt__(self, other: Distance) -> bool: | ||
| return self.km < other | ||
|
|
||
| def __gt__(self, other: Distance) -> bool: |
There was a problem hiding this comment.
__gt__ has the same issues as __lt__: the annotation only accepts Distance and the body compares self.km to other instead of other.km, and it doesn’t support comparison with numbers, violating the comparison magic method requirements.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am requesting changes because there is still a critical functional issue in your Distance class that breaks the core requirements. In app/main.py, inside __iadd__, the Distance branch currently does self.km = other + self.km (around line 21), which calls __add__ and assigns a Distance object to self.km instead of a numeric value. This means that after d1 = Distance(20); d2 = Distance(30); d1 += d2, d1.km becomes a Distance instance rather than the numeric sum 50, violating the invariant that self.km must always store the kilometers as a number and breaking subsequent arithmetic/comparison behavior. To fix this, in the isinstance(other, Distance) branch you should update the numeric value directly, e.g. self.km += other.km, and still return self. As a minor style improvement, you can remove the else after the if isinstance(other, Distance) in __iadd__ and let the numeric-handling code be the default fall-through, which aligns better with the “avoid unnecessary else” guideline. Once you correct the __iadd__ implementation for Distance operands, the class will satisfy the specified functionality and checklist constraints—everything else in your implementation looks solid and well-aligned with the requirements.
✨ 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
|
|
||
| def __iadd__(self, other: Distance | int | float) -> Distance: | ||
| if isinstance(other, Distance): | ||
| self.km = other + self.km |
There was a problem hiding this comment.
__iadd__ for a Distance operand currently assigns self.km = other + self.km, which uses __add__ and results in self.km being a Distance instance instead of a numeric value; this violates the requirement that in-place addition with another Distance should update self.km using other.km (e.g. self.km += other.km).
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am approving your solution, with a small educational note to help you keep improving. Functionally, your Distance class in app/main.py looks solid: arithmetic, comparisons, and string representations all behave correctly, and your use of type annotations and magic methods is well done.
The only remaining issue is a minor style point in __iadd__ around checklist guidance [CHECKLIST ITEM #1]. At the __iadd__ method (around the lines where you handle other), you still use an else after if isinstance(other, Distance). This is considered unnecessary because you can return immediately in the Distance branch and let the numeric branch “fall through,” like:
if isinstance(other, Distance):
self.km += other.km
return self
self.km += other
return selfThis approval is an educational override to help you progress while you learn, since the remaining issue is purely stylistic, not functional. Overall, you’ve done a very good job implementing the required behavior; refining this small style detail will make your code even cleaner and closer to professional standards.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| def __iadd__(self, other: Distance | int | float) -> Distance: | ||
| if isinstance(other, Distance): | ||
| self.km += other.km | ||
| else : |
There was a problem hiding this comment.
This violates checklist item #1: "Avoid unnecessary else statements". You can restructure __iadd__ so the numeric branch is a fall-through instead of using else, e.g. if isinstance(other, Distance): self.km += other.km; return self; self.km += other; return self.
No description provided.