-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: Convert values of resolved options #125
base: main
Are you sure you want to change the base?
Conversation
Reviewer's Guide by SourceryThis PR enhances the BaseOptions class by adding type conversion functionality and improving type validation. It replaces the previous warning-based approach with strict type checking and implements automatic conversion of field values to their annotated types during initialization. Updated class diagram for BaseOptionsclassDiagram
class BaseOptions {
+__init_subclass__() void
+from_options(options: dict[str, Any] | None) BaseOptions
+__post_init__() void
+_convert_and_set_field_value(field: Field[Any], value: Any, target_type: type) void
}
class UnsupportedTypeError {
}
BaseOptions <|-- UnsupportedTypeError
note for BaseOptions "Enhanced with type conversion and validation"
note for UnsupportedTypeError "New error class for unsupported types"
File-Level Changes
Assessment against linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey @dashmug - I've reviewed your changes - here's some feedback:
Overall Comments:
- The code references
SUPPORTED_TYPES
but this constant is not defined. Please define it with the supported types (str, int, float, bool).
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@@ -51,3 +51,36 @@ def from_options(cls, options: dict[str, Any] | None = None) -> Self: | |||
return cls( | |||
**{key: value for key, value in options.items() if key in field_names}, | |||
) | |||
|
|||
def __post_init__(self) -> 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.
issue (complexity): Consider consolidating the type conversion logic into a single method with simplified boolean handling.
The type conversion logic can be simplified while maintaining functionality:
- Merge the conversion logic into
__post_init__
- Simplify boolean conversion to standard true/false strings
- Move type validation to
__init_subclass__
def __post_init__(self) -> None:
"""Convert field values to their annotated types."""
type_hints = get_type_hints(self)
for field in fields(self):
value = getattr(self, field.name)
target_type = type_hints.get(field.name, Any)
if not isinstance(value, target_type):
try:
if target_type is bool:
converted = str(value).lower() == "true"
else:
converted = target_type(value)
setattr(self, field.name, converted)
except (ValueError, TypeError) as e:
msg = f"Could not convert field {field.name} value '{value}' to {target_type.__name__}"
raise ValueError(msg) from e
This maintains all functionality while:
- Removing unnecessary method abstraction
- Simplifying boolean conversion
- Reducing nesting levels
- Keeping error handling intact
try: | ||
if target_type is bool: | ||
# Special handling for boolean strings | ||
converted = str(value).lower() in ("true", "t", "yes", "y", "1") |
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.
suggestion (code-quality): Use set when checking membership of a collection of literals (collection-into-set
)
converted = str(value).lower() in ("true", "t", "yes", "y", "1") | |
converted = str(value).lower() in {"true", "t", "yes", "y", "1"} |
Resolves #64.
Summary by Sourcery
Implement automatic conversion of field values to their annotated types in the BaseOptions dataclass and replace warnings with errors for unsupported field types.
New Features:
Enhancements: