Skip to content

Commit

Permalink
feat: Implement type casting and set field values in Issue class
Browse files Browse the repository at this point in the history
This change introduces a new `_cast_and_set` static method in the `Issue` class to handle the type casting and setting of field values:

1. Adds a `_cast_and_set` static method that takes a field and a value, and casts the value to the appropriate data type based on the field's `dtype` attribute.
2. Updates the `editable_fields` method to use the new `_cast_and_set` method to set the field values, ensuring proper data type handling.
3. Handles cases where the value cannot be cast to the expected data type, printing an error message.

This change improves the robustness and maintainability of the field value handling in the `Issue` class.
  • Loading branch information
ronaldokun committed Aug 6, 2024
1 parent 05221dd commit 8968422
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion fiscaliza/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,25 @@ def _append_irregularity_options(
editable_fields["irregularidade"].options = options
return editable_fields

@staticmethod
def _cast_and_set(field, value):
try:
match field.dtype:
case "string":
value = str(value)
case "int":
value = int(value)
case "float":
value = float(value)
case "list":
value = listify(value)
case _:
print(f"Unknown dtype {field.dtype}, casting skipped...")
except ValueError as e:
print(f"Error casting value {value} to dtype {field.dtype}: {e}")

setattr(field, "value", value)

@cached_property
def editable_fields(self) -> dict:
"""Retrieves the editable fields of an issue as a dictionary."""
Expand All @@ -348,7 +367,7 @@ def editable_fields(self) -> dict:
self.attrs[key] = [str(k) for k in self.attrs[key]]
else:
self.attrs[key] = str(self.attrs[key])
setattr(field, "value", self.attrs[key])
self._cast_and_set(field, self.attrs[key])
if key == "fiscais":
setattr(field, "options", self.attrs["membros"])
elif key == "fiscal_responsavel":
Expand Down

0 comments on commit 8968422

Please sign in to comment.