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

Support import type annotation from slicer during module load #6847

Conversation

jcfr
Copy link
Member

@jcfr jcfr commented Feb 28, 2023

This commit ensures that python wrapped C++ classes implemented by loadable module (Logic, MRML, MRMLDisplayableManager, VTKWidgets, PythonQt) are imported and associated with the "slicer" package during module instantiation.

This ensures that classes that are both provided by other modules and referenced in python script associated with scripted loadable module can be successfully resolved.

Note that since wrapped C++ VTK modules explicitly import other wrapped C++ VTK modules they depend one (using vtkPythonUtil::ImportModule), calling qSlicerScriptedUtils::importModulePythonExtensions during module instantiation done independently of the module dependency graph is not a problem.

$ ctest -R test_slicer_parameter 
Test project /home/jcfr/Projects/Slicer-Release/Slicer-build
    Start 588: py_nomainwindow_test_slicer_parameter_node_wrapper
1/4 Test #588: py_nomainwindow_test_slicer_parameter_node_wrapper ................   Passed    3.89 sec
    Start 589: py_nomainwindow_test_slicer_parameter_pack
2/4 Test #589: py_nomainwindow_test_slicer_parameter_pack ........................   Passed    3.46 sec
    Start 590: py_nomainwindow_test_slicer_parameter_node_wrapper_guis
3/4 Test #590: py_nomainwindow_test_slicer_parameter_node_wrapper_guis ...........   Passed    3.58 sec
    Start 591: py_nomainwindow_test_slicer_parameter_node_wrapper_gui_creation
4/4 Test #591: py_nomainwindow_test_slicer_parameter_node_wrapper_gui_creation ...   Passed    3.39 sec

100% tests passed, 0 tests failed out of 4
Original description

Pending:

  • fix py_nomainwindow_test_slicer_parameter_node_wrapper
  • review if out-of-order import of python modules wrapping VTK classes is a problem (indeed importing at instantiation time is done independently of the module dependency graph)

@jcfr jcfr marked this pull request as draft February 28, 2023 20:52
@jcfr jcfr force-pushed the support-import-type-annotation-from-slicer-during-module-load branch from 672426a to 40cfdab Compare February 28, 2023 22:50
@jcfr
Copy link
Member Author

jcfr commented Mar 2, 2023

out-of-order import of python modules wrapping VTK classes

This is not an issue, each wrapped C++ VTK module takes care of importing its dependencies.

image

@jcfr jcfr force-pushed the support-import-type-annotation-from-slicer-during-module-load branch 2 times, most recently from 03088e7 to dcfd1e5 Compare March 3, 2023 07:03
@jcfr jcfr marked this pull request as ready for review March 3, 2023 07:04
@jcfr jcfr requested a review from Connor-Bowley March 3, 2023 07:04
jcfr added 2 commits March 3, 2023 02:05
…her modules

This commit ensures that python wrapped C++ classes implemented by loadable
module (Logic, MRML, MRMLDisplayableManager, VTKWidgets, PythonQt) are imported
and associated with the "slicer" package during module instantiation.

This ensures that classes that are both provided by other modules and
referenced in python script associated with scripted loadable module
can be successfully resolved.

Note that since wrapped C++ VTK modules explicitly import
other wrapped C++ VTK modules they depend one (using
vtkPythonUtil::ImportModule), calling qSlicerScriptedUtils::importModulePythonExtensions
during module instantiation done independently of the module dependency
graph is not a problem.
…ndencies

Update test_slicer_parameter_node_wrapper and test_slicer_parameter_pack
tests execution options to ensure loadable modules are loaded.

This is required to ensure the following imports work:
* import of `qMRMLSubjectHierarchyTreeView` in slicer.parameterNodeWrapper.guiConnectors
* import of vtkMRML*Node in test_slicer_parameter_node_wrapper
@jcfr jcfr force-pushed the support-import-type-annotation-from-slicer-during-module-load branch from dcfd1e5 to d1e1004 Compare March 3, 2023 07:22
@Connor-Bowley
Copy link
Contributor

LGTM.

Tests pass. I was able to import a MRML node from an extension (s-reps) to use as a type hint via the slicer namespace. Thanks!

@jcfr jcfr merged commit 5aeb615 into Slicer:main Mar 3, 2023
@jcfr jcfr deleted the support-import-type-annotation-from-slicer-during-module-load branch March 3, 2023 17:00
@jcfr
Copy link
Member Author

jcfr commented Mar 3, 2023

I realized the following tests are failing:

	552 - py_nowarning_mainwindow_noloadableTest (Failed)
	554 - py_nowarning_mainwindow_nocli_noloadableTest (Failed)
	563 - py_nowarning_nomainwindow_noloadableTest (Failed)
	565 - py_nowarning_nomainwindow_nocli_noloadableTest (Failed)
	573 - py_nomainwindow_SlicerOptionModulesToIgnoreTest (Failed)

Currently looking into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants