-
Notifications
You must be signed in to change notification settings - Fork 1
Raise a warning for unused petsc options in OptionsManager.
#21
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ddb0e53
raise a warning for unused petsc options
JHopeCollins 1b363ee
grammar
JHopeCollins 436c468
do not import PETSc at module level in tests
JHopeCollins daedb13
warn_unused_options message tidy, and remove Optional from type hints
JHopeCollins 8ef0e5f
optionally only warn on unused arguments if -options_left is set
JHopeCollins 4cd3db3
warn_unused_options checks in its own options for -options_left
JHopeCollins 2f5746d
linting
JHopeCollins 36aa9d1
actually forward all kwargs for warn_unused_option
JHopeCollins aa9908f
raise options_left warnings using weakref.finalize
JHopeCollins 7b1000d
Update tests/test_options.py
JHopeCollins 5cf575e
default string for warn_unused_options
JHopeCollins 6be41bb
Merge branch 'JHopeCollins/warn_unused_options' of github.com:firedra…
JHopeCollins c45b05c
Update petsctools/options.py
JHopeCollins 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import warnings | ||
| import pytest | ||
| import petsctools | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True, scope="module") | ||
| def temporarily_remove_options(): | ||
| """Remove all options when the module is entered and reinsert them at exit. | ||
| This ensures that options in e.g. petscrc files will not pollute the tests. | ||
| """ | ||
| if petsctools.PETSC4PY_INSTALLED: | ||
| PETSc = petsctools.init() | ||
| options = PETSc.Options() | ||
| previous_options = { | ||
| k: v for k, v in options.getAll().items() | ||
| } | ||
| options.clear() | ||
| yield | ||
| if petsctools.PETSC4PY_INSTALLED: | ||
| for k, v in previous_options.items(): | ||
| options[k] = v | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def clear_options(): | ||
| """Clear any options from the database at the end of each test. | ||
| """ | ||
| yield | ||
| # PETSc already initialised by module scope fixture | ||
| from petsc4py import PETSc | ||
| PETSc.Options().clear() | ||
|
|
||
|
|
||
| @pytest.mark.skipnopetsc4py | ||
| @pytest.mark.parametrize("options_left", (-1, 0, 1), | ||
| ids=("no_options_left", | ||
| "options_left=0", | ||
| "options_left=1")) | ||
| def test_unused_options(options_left): | ||
| """Check that unused solver options result in a warning in the log.""" | ||
| # PETSc already initialised by module scope fixture | ||
| from petsc4py import PETSc | ||
|
|
||
| if options_left >= 0: | ||
| PETSc.Options()["options_left"] = options_left | ||
|
|
||
| parameters = { | ||
| "used": 1, | ||
| "not_used": 2, | ||
| } | ||
| options = petsctools.OptionsManager(parameters, options_prefix="optobj") | ||
|
|
||
| with options.inserted_options(): | ||
| _ = PETSc.Options().getInt(options.options_prefix + "used") | ||
|
|
||
| # No warnings should be raised in this case. | ||
| if options_left <= 0: | ||
| with warnings.catch_warnings(): | ||
| warnings.simplefilter("error") | ||
| del options | ||
| return | ||
|
|
||
| # Destroying the object will trigger the unused options warning | ||
| with pytest.warns() as records: | ||
JHopeCollins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| del options | ||
|
|
||
| # Exactly one option is both unused and not ignored | ||
| assert len(records) == 1 | ||
| message = str(records[0].message) | ||
|
|
||
| # Does the warning include the options prefix? | ||
| assert "optobj" in message | ||
|
|
||
| # Do we only raise a warning for the unused option? | ||
| assert "optobj_not_used" in message | ||
| assert "optobj_used" not in message | ||
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.
Uh oh!
There was an error while loading. Please reload this page.