Skip to content

Commit

Permalink
WIP: ENH: Added type hints for wrapped Python classes and methods.
Browse files Browse the repository at this point in the history
Logic was added to the igenerator.py file to produce type hints for wrapped ITK classes in Python.

Over half of the available ITK classes are currently supported by this feature, with more being worked on.

Two files are created, init.pyi and methods.pyi which contain the hint information needed for an IDE like Pycharm. Once built there should be no extra setup needed to get the hinting functionality.

The hints use have Template and Proxy names appended to them to differentiate them from the actual ITK classes. The hints are not perfect, they are merely suggestions for potentially correct input options. For example methods may only accept a double or character depending on the object type, but the hints will suggest both leaving it up to the user to determine which type is valid.
  • Loading branch information
kian-weimer committed Oct 6, 2021
1 parent c54ad67 commit d8ca113
Show file tree
Hide file tree
Showing 2 changed files with 645 additions and 94 deletions.
20 changes: 20 additions & 0 deletions Wrapping/Generators/SwigInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,25 @@ set(ITK_WRAP_SWIGINTERFACE_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERN
set(WRAPPER_MASTER_INDEX_OUTPUT_DIR "${ITK_DIR}/Wrapping/Typedefs" CACHE INTERNAL "typedefs dir")
set(IGENERATOR "${CMAKE_CURRENT_SOURCE_DIR}/igenerator.py" CACHE INTERNAL "igenerator.py path" FORCE)

# Create/Clear .pyi file
file(WRITE "${ITK_DIR}/Wrapping/Generators/Python/itk-stubs/__init__.pyi"
"# Python Header Interface File
# imports as they appear in __init__.py
from itkConfig import ITK_GLOBAL_VERSION_STRING as __version__
from support.extras import *
from support.init_helpers import *
from support.types import itkCType, F, D, UC, US, UI, UL, SL, LD, ULL, SC, SS, SI, SLL, B
\n\
# additional imports
from support.template_class import itkTemplate as _itkTemplate
from typing import Union, Any\n
import itk.methods as methods\n\n")

# Create/Clear .pyi file
file(WRITE "${ITK_DIR}/Wrapping/Generators/Python/itk-stubs/methods.pyi"
"# Python Method Interface File\n\n")

macro(itk_wrap_module_swig_interface library_name)
# store the content of the mdx file
set(SWIG_INTERFACE_MDX_CONTENT )
Expand Down Expand Up @@ -374,6 +393,7 @@ macro(itk_end_wrap_module_swig_interface)
--interface-output-dir "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}"
--library-output-dir "${WRAPPER_LIBRARY_OUTPUT_DIR}"
--submodule-order "${WRAPPER_SUBMODULE_ORDER}"
--pyi_dir "${ITK_DIR}/Wrapping/Generators/Python/itk-stubs"
DEPENDS ${ITK_WRAP_DOC_DOCSTRING_FILES} ${xml_files} ${IGENERATOR} ${typedef_in_files}
WORKING_DIRECTORY / # Arguments to WORKING_DIRECTORY may use generator expressions
VERBATIM
Expand Down
Loading

0 comments on commit d8ca113

Please sign in to comment.