Skip to content

Replace bare except clauses with specific exceptions - #1183

Open
ya1b wants to merge 1 commit into
trustgraph-ai:release/v2.11from
ya1b:fix/bare-except-handlers
Open

ya1b wants to merge 1 commit into
trustgraph-ai:release/v2.11from
ya1b:fix/bare-except-handlers

Conversation

@ya1b

@ya1b ya1b commented Oct 27, 2026

Copy link
Copy Markdown

Summary

  • Replace the bare exception handler for invalid JSON schemas with json.JSONDecodeError.
  • Catch OSError when cleaning up temporary files.
  • Catch LookupError when the NLTK sentence tokenizer resource is unavailable.

Testing

  • Python compilation passed for all three modified files.
  • CLI tests: 22 passed, 17 skipped.
  • Ontology text-processing tests could not complete because of an NLTK resource-loading/setup issue in the local environment.

Closes #783

@github-actions

Copy link
Copy Markdown

Contributor License Agreement ❌

This PR contains commits from author(s) with no linked GitHub account. All commits must be authored by a GitHub user so the CLA can be verified.

Please ensure the git author email is associated with a GitHub account, or rewrite the commits with a linked identity.

@cybermaggedon
cybermaggedon changed the base branch from master to release/v2.11 October 27, 2026 10:05
@cybermaggedon
cybermaggedon self-requested a review October 27, 2026 10:06
@cybermaggedon

Copy link
Copy Markdown
Contributor

@ya1b please take a look at the CLA Assistant notice above. Are you able to execute this?

@cybermaggedon cybermaggedon self-assigned this Oct 27, 2026

@cybermaggedon cybermaggedon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean, minimal, each bare except replaced with the right specific exception.

@swinter-ctrl

Copy link
Copy Markdown

Heads-up on the failing "Run tests" check: I think it's caused by the text_processor.py change.

nltk.data.load('tokenizers/punkt_tab/english/') doesn't raise LookupError here. It raises ValueError ("Could not determine format for nltk:tokenizers/punkt_tab/english/ based on its file extension"), because NLTK checks the format before it looks up the resource. So with except LookupError: the fallback no longer runs and SentenceSplitter() raises. That matches the CI log, where every test in tests/unit/test_extract/test_ontology/test_text_processing.py errors with that ValueError.

Catching both keeps the current behaviour:

except (LookupError, ValueError):

I checked this locally with nltk 3.10.4: the ontology tests pass with that change.

Side note: the punkt_tab branch never succeeds today. It works anyway because, on nltk >= 3.9, loading tokenizers/punkt/english.pickle is silently redirected to PunktTokenizer (which uses punkt_tab data). So the "Using NLTK sentence tokenizer (punkt)" log message is misleading. A simpler follow-up could be self.sent_detector = nltk.tokenize.PunktTokenizer('english') with no try/except, since nltk>=3.10.2 is already required.

Minor, optional: in set_prompt.py, except json.JSONDecodeError as e: raise RuntimeError(...) from e would keep the position of the JSON error.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace bare except: with specific exceptions

3 participants