From bec0137c992504c1b59a14063d58e79d50ed5059 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 20 Nov 2022 19:56:45 -0800 Subject: [PATCH] update type hints and formatting --- meanas/fdfd/bloch.py | 10 +++++----- meanas/fdfd/farfield.py | 6 +++--- meanas/fdfd/functional.py | 4 ++-- meanas/fdfd/solvers.py | 6 +++--- meanas/fdfd/waveguide_2d.py | 2 +- meanas/fdtd/base.py | 12 +++++++++--- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/meanas/fdfd/bloch.py b/meanas/fdfd/bloch.py index 96c8ea8..c4c4b60 100644 --- a/meanas/fdfd/bloch.py +++ b/meanas/fdfd/bloch.py @@ -413,8 +413,8 @@ def find_k( band: int = 0, k_bounds: Tuple[float, float] = (0, 0.5), k_guess: Optional[float] = None, - solve_callback: Optional[Callable[[...], None]] = None, - iter_callback: Optional[Callable[[...], None]] = None, + solve_callback: Optional[Callable[..., None]] = None, + iter_callback: Optional[Callable[..., None]] = None, ) -> Tuple[float, float, NDArray[numpy.complex128], NDArray[numpy.complex128]]: """ Search for a bloch vector that has a given frequency. @@ -440,7 +440,7 @@ def find_k( """ direction = numpy.array(direction) / norm(direction) - k_bounds = tuple(sorted(k_bounds)) + k_bounds = tuple(sorted(k_bounds)) # type: ignore # we know the length already... assert len(k_bounds) == 2 if k_guess is None: @@ -481,7 +481,7 @@ def eigsolve( max_iters: int = 10000, reset_iters: int = 100, y0: Optional[ArrayLike] = None, - callback: Optional[Callable[[...], None]] = None, + callback: Optional[Callable[..., None]] = None, ) -> Tuple[NDArray[numpy.complex128], NDArray[numpy.complex128]]: """ Find the first (lowest-frequency) num_modes eigenmodes with Bloch wavevector @@ -531,7 +531,7 @@ def eigsolve( if y0 is None: Z = numpy.random.rand(*y_shape) + 1j * numpy.random.rand(*y_shape) else: - Z = y0 + Z = numpy.array(y0, copy=False) while True: Z *= num_modes / norm(Z) diff --git a/meanas/fdfd/farfield.py b/meanas/fdfd/farfield.py index bd02eb9..07c026d 100644 --- a/meanas/fdfd/farfield.py +++ b/meanas/fdfd/farfield.py @@ -1,7 +1,7 @@ """ Functions for performing near-to-farfield transformation (and the reverse). """ -from typing import Dict, List, Any +from typing import Dict, List, Any, Union, Sequence import numpy from numpy.fft import fft2, fftshift, fftfreq, ifft2, ifftshift from numpy import pi @@ -14,7 +14,7 @@ def near_to_farfield( H_near: cfdfield_t, dx: float, dy: float, - padded_size: List[int] = None + padded_size: Union[List[int], int, None] = None ) -> Dict[str, Any]: """ Compute the farfield, i.e. the distribution of the fields after propagation @@ -126,7 +126,7 @@ def far_to_nearfield( H_far: cfdfield_t, dkx: float, dky: float, - padded_size: List[int] = None + padded_size: Union[List[int], int, None] = None ) -> Dict[str, Any]: """ Compute the farfield, i.e. the distribution of the fields after propagation diff --git a/meanas/fdfd/functional.py b/meanas/fdfd/functional.py index cfc6187..3af38b2 100644 --- a/meanas/fdfd/functional.py +++ b/meanas/fdfd/functional.py @@ -19,7 +19,7 @@ def e_full( omega: complex, dxes: dx_lists_t, epsilon: fdfield_t, - mu: Optional[fdfield_t] = None + mu: Optional[fdfield_t] = None, ) -> cfdfield_updater_t: """ Wave operator for use with E-field. See `operators.e_full` for details. @@ -55,7 +55,7 @@ def eh_full( omega: complex, dxes: dx_lists_t, epsilon: fdfield_t, - mu: fdfield_t = None + mu: Optional[fdfield_t] = None, ) -> Callable[[cfdfield_t, cfdfield_t], Tuple[cfdfield_t, cfdfield_t]]: """ Wave operator for full (both E and H) field representation. diff --git a/meanas/fdfd/solvers.py b/meanas/fdfd/solvers.py index 6ced908..19cb418 100644 --- a/meanas/fdfd/solvers.py +++ b/meanas/fdfd/solvers.py @@ -67,9 +67,9 @@ def generic( dxes: dx_lists_t, J: vcfdfield_t, epsilon: vfdfield_t, - mu: vfdfield_t = None, - pec: vfdfield_t = None, - pmc: vfdfield_t = None, + mu: Optional[vfdfield_t] = None, + pec: Optional[vfdfield_t] = None, + pmc: Optional[vfdfield_t] = None, adjoint: bool = False, matrix_solver: Callable[..., ArrayLike] = _scipy_qmr, matrix_solver_opts: Optional[Dict[str, Any]] = None, diff --git a/meanas/fdfd/waveguide_2d.py b/meanas/fdfd/waveguide_2d.py index 3ac95d5..e9aed43 100644 --- a/meanas/fdfd/waveguide_2d.py +++ b/meanas/fdfd/waveguide_2d.py @@ -722,7 +722,7 @@ def solve_modes( omega: complex, dxes: dx_lists_t, epsilon: vfdfield_t, - mu: vfdfield_t = None, + mu: Optional[vfdfield_t] = None, mode_margin: int = 2, ) -> Tuple[NDArray[numpy.float64], NDArray[numpy.complex128]]: """ diff --git a/meanas/fdtd/base.py b/meanas/fdtd/base.py index 753eb71..1c43652 100644 --- a/meanas/fdtd/base.py +++ b/meanas/fdtd/base.py @@ -3,7 +3,7 @@ Basic FDTD field updates """ -from typing import Union +from typing import Union, Optional from ..fdmath import dx_lists_t, fdfield_t, fdfield_updater_t from ..fdmath.functional import curl_forward, curl_back @@ -12,7 +12,10 @@ from ..fdmath.functional import curl_forward, curl_back __author__ = 'Jan Petykiewicz' -def maxwell_e(dt: float, dxes: dx_lists_t = None) -> fdfield_updater_t: +def maxwell_e( + dt: float, + dxes: Optional[dx_lists_t] = None, + ) -> fdfield_updater_t: """ Build a function which performs a portion the time-domain E-field update, @@ -64,7 +67,10 @@ def maxwell_e(dt: float, dxes: dx_lists_t = None) -> fdfield_updater_t: return me_fun -def maxwell_h(dt: float, dxes: dx_lists_t = None) -> fdfield_updater_t: +def maxwell_h( + dt: float, + dxes: Optional[dx_lists_t] = None, + ) -> fdfield_updater_t: """ Build a function which performs part of the time-domain H-field update,