-
Notifications
You must be signed in to change notification settings - Fork 54
Add helpful hint error message if optional dependencies not yet installed. #1656
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
Open
Sevans711
wants to merge
27
commits into
sevans/tests-for-optional-deps
from
sevans/_raise_hint_if_optional_deps_missing
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
61d3ea2
add _raise_hint_if_optional_deps_missing
Sevans711 b958afd
optional deps test ensure helpful hint gets raised
Sevans711 6fb1bfd
forgot pre-commit ruff formatting
Sevans711 8e2a07a
fix opt deps error hint tests typos
Sevans711 89d8128
add test_optional_deps files & ci commands
Sevans711 8043308
forgot pre-commit ruff formatting
Sevans711 e048476
fix ruff complaint about unused import
Sevans711 c213dad
add healpix-sensitive optional deps test
Sevans711 8ebb674
fix optional deps test: cannot plot UxDataset
Sevans711 b54f9d2
add _raise_hint_if_optional_deps_missing
Sevans711 b3a58e8
optional deps test ensure helpful hint gets raised
Sevans711 71036d1
forgot pre-commit ruff formatting
Sevans711 1210ae0
fix opt deps error hint tests typos
Sevans711 4ff7f7f
Merge branch 'sevans/_raise_hint_if_optional_deps_missing' of https:/…
Sevans711 72ece1c
Merge branch 'main' into sevans/_raise_hint_if_optional_deps_missing
Sevans711 0f638de
Merge branch 'sevans/tests-for-optional-deps' into sevans/_raise_hint…
Sevans711 b89b944
fix and test messages of missing opt deps hints
Sevans711 da84ef6
forgot pre-commit ruff formatting
Sevans711 7caf9b3
Merge branch 'sevans/tests-for-optional-deps' into sevans/_raise_hint…
Sevans711 c78f80a
test usage of _raise_hint_if_optional_deps_missing
Sevans711 c942ed7
forgot pre-commit ruff formatting
Sevans711 c5deb67
Merge branch 'sevans/tests-for-optional-deps' into sevans/_raise_hint…
Sevans711 35ee6dc
fix: specify utf-8 encoding to avoid windows crash
Sevans711 7a5e216
Merge branch 'main' into sevans/_raise_hint_if_optional_deps_missing
Sevans711 686d1f8
improve installation docs page
Sevans711 1e457ed
Revert "improve installation docs page"
Sevans711 f12f269
Merge branch 'sevans/tests-for-optional-deps' into sevans/_raise_hint…
Sevans711 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| """ | ||
| Purpose: testing tools from imports.py | ||
| """ | ||
|
|
||
| import os | ||
| import warnings | ||
|
|
||
| import pytest | ||
|
|
||
| from uxarray.utils.imports import _optional_import_usage_throughout | ||
|
|
||
| HERE = __file__ # e.g. path0/uxarray/test/utils/test_imports | ||
| SRC_ROOT = os.path.abspath(os.path.join(os.path.dirname(HERE), "..", "..", "uxarray")) | ||
| # e.g. SRC_ROOT = path0/uxarray/uxarray | ||
|
|
||
|
|
||
| def test_optional_dependency_imports_are_hinted(): | ||
| """Ensures that all optional dependencies are hinted for in the functions that use them. | ||
| I.e., in a function which imports optional dependencies "dep1", "dep2", "dep3", | ||
| need to call _raise_hint_if_optional_deps_missing("dep1", "dep2", "dep3"). | ||
| """ | ||
| results = _optional_import_usage_throughout(SRC_ROOT) | ||
|
|
||
| missing = [r for r in results if r.missing_deps] | ||
| extra = [r for r in results if r.extra_deps] | ||
|
|
||
| for r in extra: | ||
| warnings.warn( | ||
| f"{r.filepath}:{r.lineno} in {r.qualname} — " | ||
| "_raise_hint_if_optional_deps_missing() lists deps that " | ||
| f"aren't actually imported here: {sorted(r.extra_deps)}", | ||
| stacklevel=1, | ||
| ) | ||
|
|
||
| if missing: | ||
| details = "\n".join( | ||
| f" {r.filepath}:{r.lineno} in {r.qualname} — " | ||
| f"failed to hint for these deps: {sorted(r.missing_deps)}" | ||
| for r in missing | ||
| ) | ||
| pytest.fail( | ||
| "Function(s) import optional dependencies without including all of them in " | ||
| f"_raise_hint_if_optional_deps_missing():\n{details}" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This pattern looks like it will be difficult to maintain consistently throughout the codebase as we develop it. If we're going to have this, there should definitely be some kind of linting for this so we'll know in the PRs if these are done correctly, and provide an easy way to fix it.
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.
Thank you for looking into this! Commit c78f80a adds a test to the pytest test suite which should fail if anyone defines a function that imports optional dependencies without all of them being properly included in a call to _raise_hint_if_optional_deps_missing(). The failure mode includes a helpful message which should make it clear enough on how to fix it.
(It also raises clear warnings if bonus optional dependencies are being included, unnecessarily, inside a call to the raise_hint... function.)
I chose to implement this as a pytest test instead of full-blown linting because I have a suspicion that this will be easier to maintain (e.g., I'm not yet familiar with building customized linting algorithms). There's no "fix it for me" button like a proper linting algorithm might provide, but I think that this should still be sufficient? I believe it grants the most important benefits of ensuring this pattern gets maintained, and providing clear instructions for how to fix if it if needed.
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.
Eh, it's better, but the "fix it for me" button is my ideal in this case.