Skip to content

Implement Distance class with all required magic methods#2797

Open
Tiken-dev wants to merge 2 commits into
mate-academy:masterfrom
Tiken-dev:solution
Open

Implement Distance class with all required magic methods#2797
Tiken-dev wants to merge 2 commits into
mate-academy:masterfrom
Tiken-dev:solution

Conversation

@Tiken-dev

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

The Distance class implementation is correct and all runtime functionality works as expected - addition, in-place addition, multiplication, division, and all comparison operators handle both Distance and numeric types properly. The code passes functional requirements.

However, there are two categories of issues to address in future work:

Type annotation corrections needed:

  • The __iadd__ method should return Distance instead of int | float
  • Parameters like other: int in __mul__, __truediv__, and comparison operators should be other: int | float

Style checklist violations:

  • Several methods have unnecessary else blocks after return statements (__add__, __iadd__, __eq__, __lt__, __gt__, __le__, __ge__) - these should use early return pattern per checklist item #1

These are type safety and code style improvements rather than functional blockers, so your solution meets the core requirements.


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

Footnotes

  1. Rate AI review example

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The type annotation other: int doesn't match requirements - should accept both int and float. Change to other: int | float.

Comment thread app/main.py

def __mul__(self, other: int) -> Distance:
return 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.

The type annotation other: int doesn't match requirements - should accept both int and float. Change to other: int | float.

Comment thread app/main.py Outdated
else:
return Distance(self.km + other)

def __iadd__(self, other: Distance) -> int | float:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The return type annotation -> int | float is incorrect - __iadd__ should return Distance instance (self). Change to -> Distance.

Comment thread app/main.py Outdated
Comment on lines +15 to +18
def __add__(self, other: Distance) -> Distance:
if isinstance(other, Distance):
return 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.

Checklist item #1 violation: unnecessary else statement. Use early return pattern instead:

if isinstance(other, Distance):
    return Distance(self.km + other.km)
return Distance(self.km + other)

Comment thread app/main.py Outdated
Comment on lines +21 to +24
def __iadd__(self, other: Distance) -> int | float:
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.

Checklist item #1 violation: unnecessary else statement. Use early return pattern instead:

if isinstance(other, Distance):
    self.km += other.km
    return self
self.km += other
return self

Comment thread app/main.py
Comment on lines +33 to +36

def __lt__(self, other: Distance) -> bool:
if isinstance(other, Distance):
return self.km < other.km

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #1 violation: unnecessary else statement. Use early return pattern instead:

if isinstance(other, Distance):
    return self.km < other.km
return self.km < other

Comment thread app/main.py
Comment on lines +39 to +42

def __gt__(self, other: Distance) -> bool:
if isinstance(other, Distance):
return self.km > other.km

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #1 violation: unnecessary else statement. Use early return pattern instead:

if isinstance(other, Distance):
    return self.km > other.km
return self.km > other

Comment thread app/main.py
Comment on lines +45 to +48

def __eq__(self, other: Distance) -> bool:
if isinstance(other, Distance):
return self.km == other.km

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #1 violation: unnecessary else statement. Use early return pattern instead:

if isinstance(other, Distance):
    return self.km == other.km
return self.km == other

Comment thread app/main.py
Comment on lines +51 to +54

def __le__(self, other: Distance) -> bool:
if isinstance(other, Distance):
return self.km <= other.km

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #1 violation: unnecessary else statement. Use early return pattern instead:

if isinstance(other, Distance):
    return self.km <= other.km
return self.km <= other

Comment thread app/main.py
Comment on lines +57 to +60

def __ge__(self, other: Distance) -> bool:
if isinstance(other, Distance):
return self.km >= other.km

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #1 violation: unnecessary else statement. Use early return pattern instead:

if isinstance(other, Distance):
    return self.km >= other.km
return self.km >= other

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