Skip to content

Commit 3d99c3a

Browse files
committed
Merge branch 'lean-type-definition' into 'development'
Python type definition overhaul See merge request damask/DAMASK!1245
2 parents 61dc9c6 + 3647894 commit 3d99c3a

17 files changed

Lines changed: 413 additions & 345 deletions

python/damask/_colormap.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import json
55
import os
66
from itertools import chain
7-
from typing import Optional, Union
87

98
import matplotlib as mpl
109

@@ -19,7 +18,7 @@
1918
from PIL import Image
2019

2120
from . import Table, util
22-
from ._typehints import FileHandle, FloatSequence
21+
from ._typehints import FileHandleText, FloatSequence
2322

2423

2524
_EPS = 216./24389.
@@ -321,7 +320,7 @@ def from_predefined(name: str,
321320

322321

323322
def at(self,
324-
fraction : Union[float,FloatSequence]) -> np.ndarray:
323+
fraction : float | FloatSequence) -> np.ndarray:
325324
"""
326325
Interpolate color at fraction.
327326
@@ -352,8 +351,8 @@ def at(self,
352351

353352
def shade(self,
354353
field: np.ndarray,
355-
bounds: Optional[FloatSequence] = None,
356-
gap: Optional[float] = None) -> Image.Image:
354+
bounds: FloatSequence | None = None,
355+
gap: float | None = None) -> Image.Image:
357356
"""
358357
Generate PIL image of 2D field using colormap.
359358
@@ -394,7 +393,7 @@ def shade(self,
394393

395394

396395
def reversed(self,
397-
name: Optional[str] = None) -> 'Colormap':
396+
name: str | None = None) -> 'Colormap':
398397
"""
399398
Reverse.
400399
@@ -420,7 +419,7 @@ def reversed(self,
420419

421420

422421
def save_paraview(self,
423-
fname: Optional[FileHandle] = None):
422+
fname: FileHandleText | None = None):
424423
"""
425424
Save as JSON file for use in Paraview.
426425
@@ -443,7 +442,7 @@ def save_paraview(self,
443442

444443

445444
def save_ASCII(self,
446-
fname: Optional[FileHandle] = None):
445+
fname: FileHandleText | None = None):
447446
"""
448447
Save as ASCII file.
449448
@@ -460,7 +459,7 @@ def save_ASCII(self,
460459

461460

462461
def save_gmsh(self,
463-
fname: Optional[FileHandle] = None):
462+
fname: FileHandleText | None = None):
464463
"""
465464
Save as ASCII file for use in gmsh.
466465

python/damask/_configmaterial.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: AGPL-3.0-or-later
22
import logging
3-
from typing import Any, Optional, Sequence, Union
3+
from typing import Any, Sequence
44

55
import h5py
66
import numpy as np
@@ -21,10 +21,10 @@ class ConfigMaterial(YAML):
2121
"""
2222

2323
def __init__(self,
24-
config: Optional[Union[str,dict[str,Any]]] = None,*,
25-
homogenization: Optional[dict[str,dict]] = None,
26-
phase: Optional[dict[str,dict]] = None,
27-
material: Optional[list[dict[str,Any]]] = None):
24+
config: str | dict[str, Any] | None = None,*,
25+
homogenization: dict[str,dict] | None = None,
26+
phase: dict[str,dict] | None = None,
27+
material: list[dict[str,Any]] | None = None):
2828
"""
2929
New material configuration.
3030
@@ -42,7 +42,7 @@ def __init__(self,
4242
Materialpoint configuration.
4343
Defaults to an empty list if 'config' is not given.
4444
"""
45-
kwargs: dict[str,Union[dict[str,dict],list[dict[str,Any]]]] = {}
45+
kwargs: dict[str,dict[str,dict] | list[dict[str,Any]]] = {}
4646
for arg,value in zip(['homogenization','phase','material'],[homogenization,phase,material]):
4747
if value is None and config is None:
4848
kwargs[arg] = [] if arg == 'material' else {}
@@ -54,13 +54,13 @@ def __init__(self,
5454

5555
@staticmethod
5656
def load_DREAM3D(fname: str,
57-
grain_data: Optional[str] = None,
58-
cell_data: Optional[str] = None,
57+
grain_data: str | None = None,
58+
cell_data: str | None = None,
5959
cell_ensemble_data: str = 'CellEnsembleData',
6060
phases: str = 'Phases',
6161
Euler_angles: str = 'EulerAngles',
6262
phase_names: str = 'PhaseName',
63-
base_group: Optional[str] = None) -> 'ConfigMaterial':
63+
base_group: str | None = None) -> 'ConfigMaterial':
6464
"""
6565
Load DREAM3D (HDF5) file.
6666
@@ -157,11 +157,11 @@ def load_DREAM3D(fname: str,
157157

158158
@staticmethod
159159
def from_table(table: Table,*,
160-
homogenization: Optional[Union[str,StrSequence]] = None,
161-
phase: Optional[Union[str,StrSequence]] = None,
162-
v: Optional[Union[str,FloatSequence]] = None,
163-
O: Optional[Union[str,FloatSequence]] = None,
164-
V_e: Optional[Union[str,FloatSequence]] = None) -> 'ConfigMaterial':
160+
homogenization: str | StrSequence | None = None,
161+
phase: str | StrSequence | None = None,
162+
v: str | FloatSequence | None = None,
163+
O: str | FloatSequence | None = None,
164+
V_e: str | FloatSequence | None = None) -> 'ConfigMaterial':
165165
"""
166166
Generate from an ASCII table.
167167
@@ -357,8 +357,8 @@ def is_valid(self) -> bool:
357357

358358
def material_rename_phase(self,
359359
mapping: dict[str, str],
360-
ID: Optional[Sequence[int]] = None,
361-
constituent: Optional[Sequence[int]] = None) -> 'ConfigMaterial':
360+
ID: Sequence[int] | None = None,
361+
constituent: Sequence[int] | None = None) -> 'ConfigMaterial':
362362
"""
363363
Change phase name in material.
364364
@@ -390,7 +390,7 @@ def material_rename_phase(self,
390390

391391
def material_rename_homogenization(self,
392392
mapping: dict[str, str],
393-
ID: Optional[Sequence[int]] = None) -> 'ConfigMaterial':
393+
ID: Sequence[int] | None = None) -> 'ConfigMaterial':
394394
"""
395395
Change homogenization name in material.
396396
@@ -417,11 +417,11 @@ def material_rename_homogenization(self,
417417

418418

419419
def material_add(self,*,
420-
homogenization: Optional[Union[str,StrSequence]] = None,
421-
phase: Optional[Union[str,StrSequence]] = None,
422-
v: Optional[Union[float,FloatSequence]] = None,
423-
O: Optional[Union[float,FloatSequence]] = None,
424-
V_e: Optional[Union[float,FloatSequence]] = None) -> 'ConfigMaterial':
420+
homogenization: str | StrSequence | None = None,
421+
phase: str | StrSequence | None = None,
422+
v: float | FloatSequence | None = None,
423+
O: float | FloatSequence | None = None,
424+
V_e: float | FloatSequence | None = None) -> 'ConfigMaterial':
425425
"""
426426
Add material entries.
427427

python/damask/_crystal.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: AGPL-3.0-or-later
22
import math
3-
from typing import Literal, Optional, Union
3+
from typing import Literal
44

55
import numpy as np
66

@@ -245,7 +245,7 @@
245245
}
246246

247247

248-
lattice_symmetries: dict[Optional[BravaisLattice], CrystalFamily] = {
248+
lattice_symmetries: dict[BravaisLattice | None, CrystalFamily] = {
249249
'aP': 'triclinic',
250250

251251
'mP': 'monoclinic',
@@ -709,10 +709,10 @@ class Crystal():
709709
"""
710710

711711
def __init__(self, *,
712-
family: Optional[CrystalFamily] = None,
713-
lattice: Optional[BravaisLattice] = None,
714-
a: Optional[float] = None, b: Optional[float] = None, c: Optional[float] = None,
715-
alpha: Optional[float] = None, beta: Optional[float] = None, gamma: Optional[float] = None,
712+
family: CrystalFamily | None = None,
713+
lattice: BravaisLattice | None = None,
714+
a: float | None = None, b: float | None = None, c: float | None = None,
715+
alpha: float | None = None, beta: float | None = None, gamma: float | None = None,
716716
degrees: bool = False):
717717
"""
718718
New representation of a crystal.
@@ -825,7 +825,7 @@ def __eq__(self,
825825
self.family == other.family)
826826

827827
@property
828-
def parameters(self) -> Optional[dict]:
828+
def parameters(self) -> dict | None:
829829
"""
830830
Return lattice parameters.
831831
@@ -900,7 +900,7 @@ def orientation_relationships(self) -> list[str]:
900900

901901

902902
@property
903-
def standard_triangle(self) -> Union[dict[str, np.ndarray], None]:
903+
def standard_triangle(self) -> dict[str, np.ndarray] | None:
904904
"""
905905
Return corners of the standard triangle.
906906
@@ -1167,8 +1167,8 @@ def lattice_points(self) -> np.ndarray:
11671167
])
11681168

11691169
def to_lattice(self, *,
1170-
direction: Optional[FloatSequence] = None,
1171-
plane: Optional[FloatSequence] = None) -> np.ndarray: # numpydoc ignore=PR01,PR02
1170+
direction: FloatSequence | None = None,
1171+
plane: FloatSequence | None = None) -> np.ndarray: # numpydoc ignore=PR01,PR02
11721172
"""
11731173
Calculate lattice vector corresponding to crystal frame direction or plane normal.
11741174
@@ -1193,10 +1193,10 @@ def to_lattice(self, *,
11931193

11941194

11951195
def to_frame(self, *,
1196-
uvw: Optional[IntSequence] = None,
1197-
hkl: Optional[IntSequence] = None,
1198-
uvtw: Optional[IntSequence] = None,
1199-
hkil: Optional[IntSequence] = None) -> np.ndarray: # numpydoc ignore=PR01,PR02
1196+
uvw: IntSequence | None = None,
1197+
hkl: IntSequence | None = None,
1198+
uvtw: IntSequence | None = None,
1199+
hkil: IntSequence | None = None) -> np.ndarray: # numpydoc ignore=PR01,PR02
12001200
"""
12011201
Calculate crystal frame vector corresponding to lattice direction [uvw]/[uvtw] or plane normal (hkl)/(hkil).
12021202
@@ -1273,7 +1273,7 @@ def kinematics(self,
12731273

12741274

12751275
def characteristic_shear_twin(self,
1276-
N_twin: Union[list[int], Literal['*']] = '*') -> np.ndarray:
1276+
N_twin: list[int] | Literal['*'] = '*') -> np.ndarray:
12771277
"""
12781278
Return characteristic shear for twinning.
12791279
@@ -1375,8 +1375,8 @@ def relation_operations(self,
13751375

13761376

13771377
def Schmid(self, *,
1378-
N_slip: Optional[Union[IntSequence, Literal['*']]] = None,
1379-
N_twin: Optional[Union[IntSequence, Literal['*']]] = None) -> np.ndarray: # numpydoc ignore=PR01,PR02
1378+
N_slip: IntSequence | Literal['*'] | None = None,
1379+
N_twin: IntSequence | Literal['*'] | None = None) -> np.ndarray: # numpydoc ignore=PR01,PR02
13801380
u"""
13811381
Calculate Schmid matrix P = d ⨂ n for selected deformation systems.
13821382

0 commit comments

Comments
 (0)