Skip to content

Commit

Permalink
STYLE: Remove unused items form typing
Browse files Browse the repository at this point in the history
Remove unused includes from typing that are not needed in python3.9
that are not builtin types (i.e. Dict -> dict, List -> list)

The Sequence type in typing was deprecated in 3.9 in favor of their
collections.abc equivalents.
  • Loading branch information
hjmjohnson committed Dec 14, 2024
1 parent 1790b05 commit b2ee615
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 31 deletions.
2 changes: 0 additions & 2 deletions Wrapping/Generators/Python/Tests/multiprocess_lazy_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
from multiprocessing.pool import ThreadPool
from multiprocessing import cpu_count

from typing import List

import sys


Expand Down
2 changes: 1 addition & 1 deletion Wrapping/Generators/Python/itk/pyi_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from io import StringIO
from pathlib import Path, PurePath
import pickle
from typing import Union, List
from typing import Union
import glob
import re
from collections import defaultdict
Expand Down
2 changes: 1 addition & 1 deletion Wrapping/Generators/Python/itk/support/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from sys import stderr as system_error_stream

# Required to work around weird import error with xarray
from typing import Dict, Any, List, Optional, Union
from typing import Any, Optional, Union
from collections.abc import Sequence

import itkConfig
Expand Down
30 changes: 15 additions & 15 deletions Wrapping/Generators/Python/itk/support/build_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,61 @@
from itk.support import types
from itk.support.template_class import itkTemplate, itkTemplateBase

from typing import List, Union
from typing import Union
from itkConfig import ITK_GLOBAL_WRAPPING_BUILD_OPTIONS as _itkwrapbo

DIMS: List[int] = [int(s) for s in _itkwrapbo["ITK_WRAP_IMAGE_DIMS"] if s]
USIGN_INTS: List[types.itkCType] = [
DIMS: list[int] = [int(s) for s in _itkwrapbo["ITK_WRAP_IMAGE_DIMS"] if s]
USIGN_INTS: list[types.itkCType] = [
getattr(types, s) for s in _itkwrapbo["WRAP_ITK_USIGN_INT"] if s
]
SIGN_INTS: List[types.itkCType] = [
SIGN_INTS: list[types.itkCType] = [
getattr(types, s) for s in _itkwrapbo["WRAP_ITK_SIGN_INT"] if s
]
REALS: List[types.itkCType] = [
REALS: list[types.itkCType] = [
getattr(types, s) for s in _itkwrapbo["WRAP_ITK_REAL"] if s
]

VECTOR_REALS: List[itkTemplate] = [
VECTOR_REALS: list[itkTemplate] = [
itkTemplateBase.__template_instantiations_name_to_object__[
itkTemplate.normalizeName(s)
]
for s in _itkwrapbo["ITK_WRAP_PYTHON_VECTOR_REAL"]
if s
]
COV_VECTOR_REALS: List[itkTemplate] = [
COV_VECTOR_REALS: list[itkTemplate] = [
itkTemplateBase.__template_instantiations_name_to_object__[
itkTemplate.normalizeName(s)
]
for s in _itkwrapbo["ITK_WRAP_PYTHON_COV_VECTOR_REAL"]
if s
]
RGBS: List[itkTemplate] = [
RGBS: list[itkTemplate] = [
itkTemplateBase.__template_instantiations_name_to_object__[
itkTemplate.normalizeName(s)
]
for s in _itkwrapbo["ITK_WRAP_PYTHON_RGB"]
if s
]
RGBAS: List[itkTemplate] = [
RGBAS: list[itkTemplate] = [
itkTemplateBase.__template_instantiations_name_to_object__[
itkTemplate.normalizeName(s)
]
for s in _itkwrapbo["ITK_WRAP_PYTHON_RGBA"]
if s
]
COMPLEX_REALS: List[itkTemplate] = [
COMPLEX_REALS: list[itkTemplate] = [
itkTemplateBase.__template_instantiations_name_to_object__[
itkTemplate.normalizeName(s)
]
for s in _itkwrapbo["ITK_WRAP_PYTHON_COMPLEX_REAL"]
if s
]

INTS: List[types.itkCType] = SIGN_INTS + USIGN_INTS
SCALARS: List[types.itkCType] = INTS + REALS
VECTORS: List[itkTemplate] = VECTOR_REALS + COV_VECTOR_REALS
COLORS: List[itkTemplate] = RGBS + RGBAS
ALL_TYPES: List[Union[types.itkCType, itkTemplate]] = (
INTS: list[types.itkCType] = SIGN_INTS + USIGN_INTS
SCALARS: list[types.itkCType] = INTS + REALS
VECTORS: list[itkTemplate] = VECTOR_REALS + COV_VECTOR_REALS
COLORS: list[itkTemplate] = RGBS + RGBAS
ALL_TYPES: list[Union[types.itkCType, itkTemplate]] = (
COLORS + VECTORS + SCALARS + COMPLEX_REALS
)

Expand Down
3 changes: 1 addition & 2 deletions Wrapping/Generators/Python/itk/support/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import enum
import re
from typing import Optional, Union, Dict, Any, List, Tuple, TYPE_CHECKING
from typing import Optional, Union, Any, TYPE_CHECKING
from collections.abc import Sequence
from sys import stderr as system_error_stream

Expand Down Expand Up @@ -50,7 +50,6 @@
except ImportError:
pass


__all__ = [
"output",
"image",
Expand Down
2 changes: 1 addition & 1 deletion Wrapping/Generators/Python/itk/support/template_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import types
import collections
import warnings
from typing import Dict, Any, List, Callable, Union
from typing import Any, Callable, Union

import itkConfig

Expand Down
5 changes: 2 additions & 3 deletions Wrapping/Generators/Python/itk/support/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import importlib
from importlib.metadata import metadata
from typing import Union, Optional, Tuple, TYPE_CHECKING
from typing import Union, TYPE_CHECKING
import os

try:
Expand Down Expand Up @@ -46,7 +46,7 @@
class itkCType:
# import locally to facilitate dynamic loading in itk/__init__.py
import numpy as np
from typing import Any, Dict, Optional, Tuple
from typing import Optional

__c_types__: dict[str, "itkCType"] = {}
__c_types_for_dtype__: dict[str, np.dtype] = {}
Expand All @@ -73,7 +73,6 @@ def __repr__(self) -> str:
@staticmethod
def GetCType(name: str) -> Optional["itkCType"]:
# import locally to facilitate dynamic loading in itk/__init__.py
from typing import Dict

"""Get the type corresponding to the provided C primitive type name."""
aliases: dict[str, str] = {
Expand Down
2 changes: 1 addition & 1 deletion Wrapping/Generators/Python/itk/support/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# ==========================================================================*/

import builtins
from typing import Tuple, TYPE_CHECKING
from typing import TYPE_CHECKING

import itk.support.types as itkt

Expand Down
10 changes: 5 additions & 5 deletions Wrapping/Generators/SwigInterface/igenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
from os.path import exists
from pathlib import Path
from keyword import iskeyword
from typing import List, Dict, Any
from typing import Any


def argument_parser():
Expand Down Expand Up @@ -222,7 +222,6 @@ def argument_parser():
import pygccxml
import logging


# Global debugging variables
pyi_approved_index_list: list[Path] = [
Path(x) for x in glb_options.pyi_index_list.split(";")
Expand Down Expand Up @@ -438,9 +437,9 @@ def get_arg_type(decls, arg_type, for_snake_case_hints=True):
elif arg_type_str.startswith("itk::FlatStructuringElement<"):
return lib + "FlatStructuringElement"
elif arg_type_str.startswith("itk::RGBPixel<"):
return "Tuple[int, int, int]"
return "tuple[int, int, int]"
elif arg_type_str.startswith("itk::RGBAPixel<"):
return "Tuple[int, int, int, int]"
return "tuple[int, int, int, int]"
elif not for_snake_case_hints and arg_type_str == "void":
return "None"
elif not for_snake_case_hints and arg_type_str.startswith("vnl_"):
Expand Down Expand Up @@ -1242,7 +1241,8 @@ def generate_process_object_snake_case_functions(self, typedefs):
self.outputFile.write(
f"""from itk.support import helpers
import itk.support.types as itkt
from typing import Sequence, Tuple, Union
from typing import Union
from collections.abc import Sequence
@helpers.accept_array_like_xarray_torch
def {snake_case}(*args{args_typehint}, {kwargs_typehints}**kwargs){return_typehint}:
Expand Down

0 comments on commit b2ee615

Please sign in to comment.