Skip to content

Commit

Permalink
Move to mypy 1.2 (pasqal-io#509)
Browse files Browse the repository at this point in the history
* Pin mypy == 1.2
* Fix implicit Optional types
* Misc type hint fixes
  • Loading branch information
HGSilveri committed Apr 24, 2023
1 parent a19f8d3 commit 0b7d8e5
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ disallow_untyped_defs = True
# 3rd-party libs without type hints nor stubs
[mypy-matplotlib.*,scipy.*,qutip.*,jsonschema.*]
follow_imports = silent
ignore_missing_imports = true
ignore_missing_imports = True

[mypy-tests.*]
disable_error_code = annotation-unchecked
disallow_untyped_defs = False
2 changes: 1 addition & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ black[jupyter] ~= 23.1
flake8
flake8-docstrings
isort
mypy == 0.921
mypy == 1.2
pytest
pytest-cov

Expand Down
2 changes: 1 addition & 1 deletion pulser-core/pulser/channels/base_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
ChannelType = TypeVar("ChannelType", bound="Channel")


@dataclass(init=True, repr=False, frozen=True) # type: ignore[misc]
@dataclass(init=True, repr=False, frozen=True)
class Channel(ABC):
"""Base class of a hardware channel.
Expand Down
7 changes: 5 additions & 2 deletions pulser-core/pulser/devices/_device_datacls.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
DIMENSIONS = Literal[2, 3]


@dataclass(frozen=True, repr=False) # type: ignore[misc]
@dataclass(frozen=True, repr=False)
class BaseDevice(ABC):
r"""Base class of a neutral-atom device.
Expand Down Expand Up @@ -236,7 +236,10 @@ def rydberg_blockade_radius(self, rabi_frequency: float) -> float:
Returns:
The rydberg blockade radius, in μm.
"""
return (self.interaction_coeff / rabi_frequency) ** (1 / 6)
# mypy can't guarantee that float**float is a float, so we need to cast
return cast(
float, (self.interaction_coeff / rabi_frequency) ** (1 / 6)
)

def rabi_from_blockade(self, blockade_radius: float) -> float:
"""The maximum Rabi frequency value to enforce a given blockade radius.
Expand Down
6 changes: 3 additions & 3 deletions pulser-core/pulser/register/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ def max_connectivity(
cls,
n_qubits: int,
device: pulser.devices._device_datacls.BaseDevice,
spacing: float = None,
prefix: str = None,
spacing: float | None = None,
prefix: str | None = None,
) -> Register:
"""Initializes the register with maximum connectivity for a device.
Expand Down Expand Up @@ -296,7 +296,7 @@ def draw(
draw_graph: bool = True,
draw_half_radius: bool = False,
qubit_colors: Mapping[QubitId, str] = dict(),
fig_name: str = None,
fig_name: str | None = None,
kwargs_savefig: dict = {},
custom_ax: Optional[Axes] = None,
show: bool = True,
Expand Down
2 changes: 1 addition & 1 deletion pulser-core/pulser/register/register3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def draw(
draw_half_radius: bool = False,
qubit_colors: Mapping[QubitId, str] = dict(),
projection: bool = False,
fig_name: str = None,
fig_name: str | None = None,
kwargs_savefig: dict = {},
) -> None:
"""Draws the entire register.
Expand Down
2 changes: 1 addition & 1 deletion pulser-core/pulser/register/register_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def draw(
draw_graph: bool = False,
draw_half_radius: bool = False,
projection: bool = True,
fig_name: str = None,
fig_name: str | None = None,
kwargs_savefig: dict = {},
) -> None:
"""Draws the entire register layout.
Expand Down
2 changes: 1 addition & 1 deletion pulser-core/pulser/sequence/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ def draw(
draw_phase_shifts: bool = False,
draw_register: bool = False,
draw_phase_curve: bool = False,
fig_name: str = None,
fig_name: str | None = None,
kwargs_savefig: dict = {},
show: bool = True,
) -> None:
Expand Down
5 changes: 2 additions & 3 deletions pulser-core/pulser/waveforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ class CustomWaveform(Waveform):
def __init__(self, samples: ArrayLike):
"""Initializes a custom waveform."""
samples_arr = np.array(samples, dtype=float)
self._samples: np.ndarray = samples_arr
self._samples_arr: np.ndarray = samples_arr
super().__init__(len(samples_arr))

@property
Expand All @@ -448,8 +448,7 @@ def _samples(self) -> np.ndarray:
Returns:
A numpy array with a value for each time step.
"""
# self._samples is already cached when initialized in __init__
pass
return self._samples_arr

def _to_dict(self) -> dict[str, Any]:
return obj_to_dict(self, self._samples)
Expand Down
2 changes: 1 addition & 1 deletion pulser-simulation/pulser_simulation/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def draw(
draw_interp_pts: bool = False,
draw_phase_shifts: bool = False,
draw_phase_curve: bool = False,
fig_name: str = None,
fig_name: str | None = None,
kwargs_savefig: dict = {},
) -> None:
"""Draws the input sequence and the one used by the solver.
Expand Down
8 changes: 4 additions & 4 deletions tests/test_abstract_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,8 @@ def test_mappable_reg_with_local_ops(


def _get_serialized_seq(
operations: list[dict] = None,
variables: dict[str, dict] = None,
operations: list[dict] = [],
variables: dict[str, dict] = {},
**override_kwargs: Any,
) -> dict[str, Any]:
seq_dict = {
Expand All @@ -756,8 +756,8 @@ def _get_serialized_seq(
{"name": "q666", "x": 12.0, "y": 0.0},
],
"channels": {"digital": "raman_local", "global": "rydberg_global"},
"operations": operations or [],
"variables": variables or {},
"operations": operations,
"variables": variables,
"measurement": None,
}
seq_dict.update(override_kwargs)
Expand Down

0 comments on commit 0b7d8e5

Please sign in to comment.