Skip to content
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

Fix TypeError when using CI json-cid endpoint #1691

Merged
merged 4 commits into from
Nov 4, 2024

Conversation

lucasmarchd01
Copy link
Contributor

This pull request fixes a TypeError in the get_suggested_fulltext function by adding exception handling for TypeError.

Previously, the function raised a TypeError when attempting to access a key in json_response that was None for chants that don't have a full text.

Also adds some black formatting changes.

Fixes #1690

- add TypeError for when json response is None. Occurs when chants don't have a full text
@ahankinson
Copy link
Member

In situations like this I generally prefer the .get() method, since it doesn’t raise anything. You can also chain them by making the default an empty dict.

suggested_fulltext:Optional[str] = json_response.get("info", {}).get (“field_full_text")

@lucasmarchd01
Copy link
Contributor Author

lucasmarchd01 commented Nov 4, 2024

In situations like this I generally prefer the .get() method, since it doesn’t raise anything. You can also chain them by making the default an empty dict.

suggested_fulltext:Optional[str] = json_response.get("info", {}).get (“field_full_text")

The only problem I came across when testing this is that sometimes "info" will be None leading to an AttributeError on the second chain. So we'll have to do something like this instead:

    info: Optional[dict] = json_response.get("info", {})
    if not isinstance(info, dict):
        return None
    suggested_fulltext: Optional[str] = info.get("field_full_text")
    return suggested_fulltext

- use .get() method instead to avoid error handling
@ahankinson
Copy link
Member

This will do pretty much the same thing without needing the isinstance check...

info: dict = json_response.get("info", {}) or {}
return info.get("field_full_text")

@lucasmarchd01 lucasmarchd01 merged commit 783d9ab into DDMAL:develop Nov 4, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError when using CI endpoint for suggested full text
3 participants