-
Notifications
You must be signed in to change notification settings - Fork 382
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
[chore]Make get_field_attr safe and improve control flow in ts_core.py #319
[chore]Make get_field_attr safe and improve control flow in ts_core.py #319
Conversation
Refactor get_field_attr in library.py: - Ensure safety using list slicing to avoid IndexError. - Use .get() for dictionary access to handle default cases and prevent KeyError. Improve control flow in ts_core.py: - Remove nesting by exiting early in match_conditions. - Eliminate the need for try-catch blocks for better readability and control flow. Note: Kept existing_fields case unchanged due to add_field_to_entry's procedural nature. this commit introduces changes to library.py get_field_attr
cleans whitespace from file
Fix get_field_attr in library.py to ensure proper handling of empty keys list: - Previously, slicing avoided IndexError but returned a list instead. - Ensure the value is read only if present, defaulting to -1 otherwise. Ideal scenario: Define this function to return an int, as expected by the software elsewhere.
keep modified version only. This change ensures that the attribute value is correctly fetched using the _ensure_field dictionary, maintaining expected functionality.
FYI, in the future you don't need to close and open a new PR when you want to add new commits. You can just (sometimes, force) push to the same remote branch |
@seakrueger I know, but I didn't know if id find what was the introduced issue on time, so to prevent people wasting time taking a look I closed it and created a new one when ready. cheers |
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.
Nice rewrite, Approved once my comments are resolved.
] | ||
match attribute.lower(): | ||
case "id": | ||
return val[0] if not any(val := list(entry_field.keys())[:1]) else -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.
What is the -1
used for? this looked at first glance as a rewrite to structural pattern matching, are there anything else?
the dense nature of the code makes it hard to read compared to list(entry_field.keys())[0]
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.
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.
missclick.
I'm afraid this PR is now obsolete when #332 has been merged. If you disagree, feel free to reopen it. |
Refactor get_field_attr in library.py and improve control flow in ts_core.py
Changes:
1. Refactor
get_field_attr
inlibrary.py
:IndexError
.any
to determine if there is a value before using it..get()
for dictionary access to handle default cases and preventKeyError
.2. Improve control flow in
ts_core.py
:match_conditions
.Additional Fix:
Note:
existing_fields
case unchanged due toadd_field_to_entry
's procedural nature.