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

Smarter initialization of optional analyzers #820

Merged
merged 2 commits into from
Nov 22, 2024

Conversation

osma
Copy link
Member

@osma osma commented Nov 21, 2024

This PR fixes a problem with the initialization of optional analyzers that I noticed while working on the EstNLTK analyzer in PR #818 . For the spaCy analyzer, the code in analyzer/__init__.py never noticed the case when spaCy is not installed, because the import spacy statement only happens inside a method (due to performance reasons).

This PR adds a new static method is_available to all Analyzer implementations and uses that to determine whether the analyzer is available (i.e. the optional dependency is installed) or not. For spaCy and Voikko analyzers, the method is implemented by using importlib.find_spec to see if the necessary modules are available or not. This should be faster than actually importing the module, which can be done at a later time within the methods if it's actually needed.

@osma osma added the bug label Nov 21, 2024
@osma osma added this to the 1.3 milestone Nov 21, 2024
Copy link

codecov bot commented Nov 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.63%. Comparing base (4809561) to head (419f8df).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #820      +/-   ##
==========================================
+ Coverage   99.60%   99.63%   +0.02%     
==========================================
  Files          93       93              
  Lines        7134     7141       +7     
==========================================
+ Hits         7106     7115       +9     
+ Misses         28       26       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@osma osma changed the title WIP: Smarter initialization of optional analyzers Smarter initialization of optional analyzers Nov 21, 2024
@osma osma marked this pull request as ready for review November 21, 2024 15:34
@osma osma requested a review from juhoinkinen November 21, 2024 15:34
Copy link
Member

@juhoinkinen juhoinkinen left a comment

Choose a reason for hiding this comment

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

LGTM

@osma osma merged commit d907024 into main Nov 22, 2024
17 checks passed
@osma osma deleted the fix-optional-analyzer-use-importlib branch November 22, 2024 07:22
@osma osma mentioned this pull request Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants