Skip to content

Commit

Permalink
fix(Python): Use field for dataclass types
Browse files Browse the repository at this point in the history
To address with Python 3.11:

   ValueError: mutable default <class 'itkwasm.image.ImageType'> for field imageType is not allowed: use default_factory
  • Loading branch information
thewtex committed Feb 10, 2023
1 parent 188d896 commit cc035f8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/python/itkwasm/itkwasm/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ class ImageType:
pixelType: PixelTypes = PixelTypes.Scalar
components: int = 1

def _default_direction() -> ArrayLike:
return np.empty((0,), np.float32)

@dataclass
class Image:
imageType: Union[ImageType, Dict] = ImageType()
imageType: Union[ImageType, Dict] = field(default_factory=ImageType)
name: str = 'image'
origin: Sequence[float] = field(default_factory=list)
spacing: Sequence[float] = field(default_factory=list)
direction: ArrayLike = np.empty((0,), np.float32)
direction: ArrayLike = field(default_factory=_default_direction)
size: Sequence[int] = field(default_factory=list)
metadata: Dict = field(default_factory=dict)
data: Optional[ArrayLike] = None
Expand All @@ -45,4 +48,4 @@ def __post_init__(self):
self.direction = np.eye(dimension).astype(np.float32).ravel()

if len(self.size) == 0:
self.size += [1,] * dimension
self.size += [1,] * dimension
6 changes: 3 additions & 3 deletions src/python/itkwasm/itkwasm/mesh.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import dataclass
from dataclasses import dataclass, field

from typing import Optional, Union, Dict

Expand Down Expand Up @@ -28,7 +28,7 @@ class MeshType:

@dataclass
class Mesh:
meshType: Union[MeshType, Dict] = MeshType()
meshType: Union[MeshType, Dict] = field(default_factory=MeshType)

name: str = 'mesh'

Expand All @@ -47,4 +47,4 @@ class Mesh:

def __post_init__(self):
if isinstance(self.meshType, dict):
self.meshType = MeshType(**self.meshType)
self.meshType = MeshType(**self.meshType)
10 changes: 5 additions & 5 deletions src/python/itkwasm/itkwasm/polydata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Union, Optional, Dict

try:
Expand All @@ -7,9 +7,9 @@
from numpy import ndarray as ArrayLike
import numpy as np

from .float_types import FloatTypes
from .float_types import FloatTypes
from .int_types import IntTypes
from .pixel_types import PixelTypes
from .pixel_types import PixelTypes

@dataclass
class PolyDataType:
Expand All @@ -23,7 +23,7 @@ class PolyDataType:

@dataclass
class PolyData:
polyDataType: Union[PolyDataType, Dict] = PolyDataType()
polyDataType: Union[PolyDataType, Dict] = field(default_factory=PolyDataType)
name: str = 'polydata'

numberOfPoints: int = 0
Expand All @@ -49,4 +49,4 @@ class PolyData:

def __post_init__(self):
if isinstance(self.polyDataType, dict):
self.polyDataType = PolyDataType(**self.polyDataType)
self.polyDataType = PolyDataType(**self.polyDataType)

0 comments on commit cc035f8

Please sign in to comment.