Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
from __future__ import division

Choose a reason for hiding this comment

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

This import is not what is needed for this task. According to checklist item #2.2, you should use from __future__ import annotations to allow forward references in type hints (like using Distance inside the Distance class definition) without needing to use strings.

Choose a reason for hiding this comment

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

In Python 3, true division (/) is the default behavior, so this import is no longer necessary and can be removed.



class Distance:
# Write your code here
pass
def __init__(self, km: float) -> None:
self.km = km

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

def __repr__(self) -> str:
return f"Distance(km={self.km})"

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

Choose a reason for hiding this comment

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

This violates checklist item #2.1. The type hint for other is incomplete; it should also accept a Distance object (Distance | float | int). Additionally, the return type should simply be Distance, as the method always returns a Distance instance.

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

Choose a reason for hiding this comment

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

This else block is unnecessary. Since the if block above it contains a return, the code inside the else can be unindented and the else: line removed. This violates checklist item #1.1: 'Avoid unnecessary else statements'.

return Distance(self.km + other)

Choose a reason for hiding this comment

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

This else block is unnecessary because the preceding if block contains a return statement. This violates checklist item #1.1: 'Avoid unnecessary else statements'. You can remove the else and unindent the return statement.


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

Choose a reason for hiding this comment

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

This violates checklist item #2.1. The type hint for other is incomplete; it should accept Distance | int | float. The return type hint is also incorrect and should be Distance, as __iadd__ should return self.

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

def __mul__(self, other: float) -> "float | Distance":

Choose a reason for hiding this comment

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

This violates checklist item #2.1. The type hint for other should be int | float to accept both integers and floats. The return type should be Distance since a new Distance instance is always returned.

return Distance(self.km * other)

def __truediv__(self, other: float) -> "float | Distance":

Choose a reason for hiding this comment

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

This violates checklist item #2.1. The type hint for other should be int | float. The return type should be Distance.

return Distance(round((self.km / other), 2))

def __lt__(self, other: "Distance | float | int") -> bool:

Choose a reason for hiding this comment

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

This violates checklist item #2.1. Using a string for the type hint is discouraged. If you add from __future__ import annotations at the top of the file, you can write the type hint directly as other: Distance | int | float. This applies to all the comparison methods (__lt__, __gt__, __eq__, __le__, __ge__).

return self.km < other

Choose a reason for hiding this comment

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

The logic here is brittle and might not work as expected in all cases. It currently relies on Python's rich comparison dispatching. A more explicit and readable implementation would check if other is a Distance instance and compare self.km with other.km accordingly.


def __gt__(self, other: "Distance | float | int") -> bool:
return self.km > other

Choose a reason for hiding this comment

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

The logic here is brittle and might not work as expected in all cases. It currently relies on Python's rich comparison dispatching. A more explicit and readable implementation would check if other is a Distance instance and compare self.km with other.km accordingly.


def __eq__(self, other: "Distance | float | int") -> bool:
return self.km == other

Choose a reason for hiding this comment

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

The logic here is brittle and might not work as expected in all cases. It currently relies on Python's rich comparison dispatching. A more explicit and readable implementation would check if other is a Distance instance and compare self.km with other.km accordingly.


def __le__(self, other: "Distance | float | int") -> bool:
return self.km <= other

Choose a reason for hiding this comment

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

The logic here is brittle and might not work as expected in all cases. It currently relies on Python's rich comparison dispatching. A more explicit and readable implementation would check if other is a Distance instance and compare self.km with other.km accordingly.


def __ge__(self, other: "Distance | float | int") -> bool:
return self.km >= other

Choose a reason for hiding this comment

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

The logic here is brittle and might not work as expected in all cases. It currently relies on Python's rich comparison dispatching. A more explicit and readable implementation would check if other is a Distance instance and compare self.km with other.km accordingly.