-
Notifications
You must be signed in to change notification settings - Fork 612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution #606
base: master
Are you sure you want to change the base?
Solution #606
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job so far! 😊 But we need to make a few changes to improve the code. Please make sure to document why certain exceptions are being caught in the can_access
method. Also, it seems like the limitation_class
instance isn't being used in can_access
method. If it's meant to validate visitor's attributes, please make sure to call this validation method in can_access
. Lastly, if there are no abstract methods to be implemented in SlideLimitationValidator
, there's no need to inherit from ABC
. Keep going! 💪
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
app/main.py
Outdated
class SlideLimitationValidator(ABC): | ||
pass | ||
def __init__(self, age: int, height: int, weight: int) -> None: | ||
self.age = age | ||
self.height = height | ||
self.weight = weight |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class SlideLimitationValidator
inherits from ABC
which is the base class for defining Abstract Base Classes (ABCs). However, it doesn't seem to have any abstract methods. If there are no abstract methods to be implemented by the child classes, there is no need to inherit from ABC
.
app/main.py
Outdated
class Slide: | ||
pass | ||
def __init__( | ||
self, name: str, | ||
limitation_class: SlideLimitationValidator | ||
) -> None: | ||
self.name = name | ||
self.limitation_class = limitation_class | ||
|
||
def can_access(self, visitor: Visitor) -> bool: | ||
try: | ||
self.limitation_class(visitor.age, visitor.height, visitor.weight) | ||
except (TypeError, ValueError): | ||
return False | ||
else: | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Slide
class, the can_access
method is creating an instance of the limitation_class
but it doesn't seem to use it anywhere. If the limitation_class
is meant to validate the visitor's attributes, it should have a method that performs this validation. This method should then be called in can_access
to determine if access should be granted or not.
app/main.py
Outdated
def can_access(self, visitor: Visitor) -> bool: | ||
try: | ||
self.limitation_class(visitor.age, visitor.height, visitor.weight) | ||
except (TypeError, ValueError): | ||
return False | ||
else: | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The can_access
method is catching TypeError
and ValueError
exceptions and returning False
when these exceptions are raised. However, it's not clear from the code why these specific exceptions would be raised. If these exceptions are expected because of the way the limitation_class
is implemented, this should be clearly documented in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work so far! 🚀 However, there are a few critical issues that need to be addressed before we can move forward. There are places in the code where bugs might occur and also some improvements needed for code quality. Please take a look at the comments and make the necessary changes. Keep going! 💪
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
pass | ||
def __init__(self, min_amount: int, max_amount: int) -> None: | ||
self.min_amount = min_amount | ||
self.max_amount = max_amount |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good practice to validate the inputs to your class. Consider adding checks to ensure that min_amount
is less than or equal to max_amount
.
return getattr(instance, self.protected_name) | ||
|
||
def __set__(self, instance: object, value: Any) -> None: | ||
if not isinstance(value, int): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a code quality issue. The isinstance
function checks if value
is an instance of int
. However, it will also return True
if value
is an instance of a subclass of int
. If you want to check if value
is exactly an int
(and not a subclass), you should compare type(value)
with int
.
def __set__(self, instance: object, value: Any) -> None: | ||
if not isinstance(value, int): | ||
raise TypeError("The value should be an integer.") | ||
if not self.min_amount <= value <= self.max_amount: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bug. The condition should be self.min_amount <= value and value <= self.max_amount
. Otherwise, if value
is smaller than self.min_amount
and larger than self.max_amount
at the same time, it will pass the check, which is not correct.
|
||
|
||
class Visitor: | ||
pass | ||
def __init__(self, name: str, age: int, height: int, weight: int) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code quality issue: Consider using the IntegerRange
class to validate the age
, height
, and weight
of a Visitor
during initialization.
def can_access(self, visitor: Visitor) -> bool: | ||
try: | ||
self.limitation_class( | ||
age=visitor.age, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a potential bug. The SlideLimitationValidator
class and its subclasses do not have a constructor that takes age
, height
, and weight
as arguments. Therefore, this line will raise a TypeError
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job on the overall structure of the code! The use of Python's descriptor protocol in the IntegerRange
class is well executed. However, there are a few areas that need attention for future improvements. The Visitor
class attributes need validation. Consider using the IntegerRange
descriptor for these attributes as well. Be cautious with the can_access
method in the Slide
class, as it could potentially hide unrelated errors. Try catching only the specific exceptions that your code raises. Lastly, consider providing more descriptive error messages in the IntegerRange
descriptor class. Keep up the good work and continue learning from your mistakes! 👍
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
def __set__(self, instance: object, value: Any) -> None: | ||
if not isinstance(value, int): | ||
raise TypeError("The value should be an integer.") | ||
if not self.min_amount <= value <= self.max_amount: | ||
raise ValueError(f"The value should not be " | ||
f"less than {self.min_amount} " | ||
f"and greater than {self.max_amount}.") | ||
setattr(instance, self.protected_name, value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The __set__
method in the IntegerRange
descriptor class currently raises a TypeError
if the value is not an integer and a ValueError
if the value is not within the specified range. However, these exceptions are not very descriptive. It would be more helpful to the user if the error messages included more information, such as the name of the attribute and the value that caused the error.
def can_access(self, visitor: Visitor) -> bool: | ||
try: | ||
self.limitation_class( | ||
age=visitor.age, | ||
height=visitor.height, | ||
weight=visitor.weight | ||
) | ||
return True | ||
except (TypeError, ValueError): | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The can_access
method in the Slide
class catches TypeError
and ValueError
exceptions and returns False
if either is raised. This approach could potentially hide other, unrelated errors that raise the same types of exceptions. It would be safer to catch only the specific exceptions that you expect your code to raise. In this case, you might define custom exception classes for the IntegerRange
descriptor to raise, and catch those in the can_access
method.
def __init__(self, name: str, age: int, height: int, weight: int) -> None: | ||
self.name = name | ||
self.age = age | ||
self.height = height | ||
self.weight = weight |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Visitor
class, the age
, height
and weight
attributes are not validated in any way. It might be a good idea to use the IntegerRange
descriptor for these attributes as well, to ensure that they are always valid integers within a reasonable range.
No description provided.