Skip to content

Move providers from _providers.py to _qproviders.py#6743

Merged
Czaki merged 21 commits intonapari:mainfrom
lucyleeow:qprovider
May 7, 2024
Merged

Move providers from _providers.py to _qproviders.py#6743
Czaki merged 21 commits intonapari:mainfrom
lucyleeow:qprovider

Conversation

@lucyleeow
Copy link
Copy Markdown
Contributor

@lucyleeow lucyleeow commented Mar 13, 2024

References and relevant issues

closes #6585

Description

All the providers in napari/_app_model/injection/_providers.py depend on Qt.

_provide_viewer returns ViewerModel instance, which technically does not require Qt but AFAICT there is no way to get the ViewerModel without using Qt.

Searching in the codebase I cannot see ViewerModel ever being instantiated by napari. I think ViewerModel is so users can do viewer = napari.viewer.ViewerModel() in headless mode (as Viewer requires Qt)

@lucyleeow
Copy link
Copy Markdown
Contributor Author

cc @DragaDoncila @Czaki

@github-actions github-actions bot added tests Something related to our tests qt Relates to qt labels Mar 13, 2024
@lucyleeow lucyleeow added the maintenance PR with maintance changes, label Mar 13, 2024
@lucyleeow lucyleeow added this to the 0.5.0 milestone Mar 13, 2024
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 13, 2024

Codecov Report

Attention: Patch coverage is 92.91339% with 9 lines in your changes are missing coverage. Please review.

❗ No coverage uploaded for pull request base (main@c5846e8). Click here to learn what that means.
Report is 24 commits behind head on main.

Files Patch % Lines
napari/_qt/_qapp_model/injection/_qprocessors.py 90.38% 5 Missing ⚠️
napari/_qt/qthreading.py 0.00% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6743   +/-   ##
=======================================
  Coverage        ?   92.38%           
=======================================
  Files           ?      611           
  Lines           ?    54837           
  Branches        ?        0           
=======================================
  Hits            ?    50663           
  Misses          ?     4174           
  Partials        ?        0           

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

@lucyleeow
Copy link
Copy Markdown
Contributor Author

So the _provide_active_layer and _provide_active_layer_list do not current have tests and AFAICT they are not being used (I don't think any of our actions have a Layer/LayerList annotations). I feel like Talley may have written these for some future plans. Regardless I have added a simple test.

@DragaDoncila
Copy link
Copy Markdown
Contributor

We've narrowed down why the test_run_outside_ipython test fails when make_napari_viewer is used in a prior test. TLDR: we should be using make_napari_viewer for this test, as we do for all other tests using the Viewer, because this ensures proper clean up and set up for tests - in this case it's the cache clearing that we're missing.

This is what happens when you add a dummy test that uses make_napari_viewer to the top of the test_app file:

  • The first (dummy) test runs, mock_app fixture creates an app during test setup and registers all non-qt napari built-in app-model actions, providers and processors
  • The test starts, builds a make_napari_viewerwhich clears all caches, runs initialize_plugins and registers qt plugin actions with the app (samples, widgets)
  • qt_main_window.__init__ runs, registering napari built-in qt-dependent actions, qt providers and qt processors with the app
    • potential problem some of the plugin actions previously probably request the viewer, which we can't provide until this point and they also may need qprocessors to work at all e.g. widgets
  • dummy test concludes

Then, it's time for the real test to run - the test_run_outside_ipython test.

  • During test setup, mock_app builds a new app, registers non qt stuff including the viewer toggle action
    • potential problem: this action implicitly depends on qt because its "check toggled" function requires a Viewer - this really is a qt action and should be moved
    • potential problem the Viewer CAN be non-qt, but the viewer PROVIDER needs qt, is there a world where someone validly wants the headless viewer via injection
  • The test doesn't use make_napari_viewer; this means the cache isn't cleared (for plugins, or for qactions)
  • The test tries to instantiate a "normal" viewer
    • "normal" viewer doesn't run initialize_plugins because it's cached, so no qt or nonqt plugins stuff is registered
  • qt_main_window runs, doesn't init_qactions, because it's cached, so no qt processors, providers or actions are registered for built-in napari stuff
  • somewhere down the line we rebuild menus, triggering a check for whether the view action is toggled
  • the toggled check function requires a viewer
  • but qt providers were never registered, because init_qactions was never run
  • the test fails because a Viewer can't be provided

@lucyleeow
Copy link
Copy Markdown
Contributor Author

lucyleeow commented Mar 22, 2024

Note the test failures are because some non-qt view actions actually req Qt (and the viewer provider I have just moved). This can be fixed by using make_napari_viewer, (which forces init_qactions to be run) - this makes sense in some tests but not others.

E.g., this test fails:

def test_view(qtbot, napari_plugin_manager, layer_type, data, ndim):
np.random.seed(0)
viewer = getattr(napari, f'view_{layer_type.__name__.lower()}')(
data, show=False
)
view = viewer.window._qt_viewer
check_viewer_functioning(viewer, view, data, ndim)
viewer.close()

I think this needs to stay as is, and so this PR is probably dependent on #6767.

DragaDoncila pushed a commit that referenced this pull request Apr 1, 2024
# References and relevant issues
Noticed when working on #6743

# Description
The current 'non-qt' actions defined in:
`napari/_app_model/actions/_view_actions.py` actually only make sense
with a GUI viewer (visibility of axes etc). I have moved these inside:
`napari/_qt/_qapp_model/qactions/_view.py` with the other Qt view
actions.

Note I have kept `ViewerToggleAction` where it is as it is general
enough that it *could* be used with a non-qt viewer, but happy to move
if requested.
@lucyleeow
Copy link
Copy Markdown
Contributor Author

CI failure is real.
I will need to move the processors that use the newly migrated providers into _qt/ as well. The processor tests will also need to be moved.

@lucyleeow
Copy link
Copy Markdown
Contributor Author

Okay @DragaDoncila this is finally green!

Copy link
Copy Markdown
Contributor

@DragaDoncila DragaDoncila left a comment

Choose a reason for hiding this comment

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

This all works for me, as far as I can tell, and I agree that's the right spot for them. There's a fair few uncovered lines of code though - were these always not covered and they're showing now because of the move?

layer = viewer.layers[name]
layer.data = datum[0]
for k, v in datum[1].items():
setattr(layer, k, v)
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.

@lucyleeow were these lines previously also not covered by tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. I notice when I move things over to qt I get mypy errors, so I assumed mypy was not checking in the old location. I guess it could be the same for code coverage?

I don't think the processors are well. There is just one test_processors.py, which just checks for for error in _add_layer_data_to_viewer

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.

ahh yeah I remember we had an issue about adding more tests for these. Ok cool, I'm approving!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ref: #6847

Copy link
Copy Markdown
Contributor

@DragaDoncila DragaDoncila left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @lucyleeow. @Czaki aside from the coverage issues, which were already a problem prior to this PR, any concerns?

@Czaki Czaki added the ready to merge Last chance for comments! Will be merged in ~24h label May 4, 2024
@lucyleeow
Copy link
Copy Markdown
Contributor Author

@Czaki @DragaDoncila could this go in?

@Czaki Czaki merged commit a9c25e6 into napari:main May 7, 2024
@github-actions github-actions bot removed the ready to merge Last chance for comments! Will be merged in ~24h label May 7, 2024
@Czaki
Copy link
Copy Markdown
Collaborator

Czaki commented May 7, 2024

Yes. I just merged main to validate if latest updates (like mypy requirements) do not broke something.

@lucyleeow lucyleeow deleted the qprovider branch May 7, 2024 10:00
DragaDoncila pushed a commit that referenced this pull request May 24, 2024
# Description
There are now no more 'non-qt' providers and processors since #6743
Amended some wording to clarify.
andy-sweet pushed a commit to andy-sweet/napari that referenced this pull request May 28, 2024
# Description
There are now no more 'non-qt' providers and processors since napari#6743
Amended some wording to clarify.
DragaDoncila pushed a commit to napari/docs that referenced this pull request Jun 7, 2024
…416)

# References and relevant issues
Relates to napari/napari#6848
Depends on napari/napari#6743

# Description

Updates the info on where actions live in the napari codebase.

---------

Co-authored-by: Peter Sobolewski <76622105+psobolewskiPhD@users.noreply.github.com>
snorkelopstesting1-a11y pushed a commit to snorkel-marlin-repos/napari_napari_pr_6743_fb1f2b32-245d-4473-b53b-596efef1aebe that referenced this pull request Oct 22, 2025
snorkelopsstgtesting1-spec added a commit to snorkel-marlin-repos/napari_napari_pr_6743_fb1f2b32-245d-4473-b53b-596efef1aebe that referenced this pull request Oct 22, 2025
…iders.py`

Merged from original PR #6743
Original: napari/napari#6743
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance PR with maintance changes, qt Relates to qt tests Something related to our tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move providers from _providers.py to _qproviders.py

4 participants