Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mypy errors (utils) #8264

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions qiskit/utils/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import functools
import inspect
import warnings
from typing import Any, Callable, Dict, Optional, Type, Tuple, Union
from collections.abc import Callable
from typing import Any, Type


def deprecate_func(
*,
since: str,
additional_msg: Optional[str] = None,
additional_msg: str | None = None,
pending: bool = False,
package_name: str = "qiskit-terra",
removal_timeline: str = "no earlier than 3 months after the release date",
Expand Down Expand Up @@ -104,12 +105,12 @@ def deprecate_arg(
name: str,
*,
since: str,
additional_msg: Optional[str] = None,
deprecation_description: Optional[str] = None,
additional_msg: str | None = None,
deprecation_description: str | None = None,
pending: bool = False,
package_name: str = "qiskit-terra",
new_alias: Optional[str] = None,
predicate: Optional[Callable[[Any], bool]] = None,
new_alias: str | None = None,
predicate: Callable[[Any], bool] | None = None,
removal_timeline: str = "no earlier than 3 months after the release date",
):
"""Decorator to indicate an argument has been deprecated in some way.
Expand Down Expand Up @@ -204,10 +205,10 @@ def wrapper(*args, **kwargs):


def deprecate_arguments(
kwarg_map: Dict[str, Optional[str]],
kwarg_map: dict[str, str | None],
category: Type[Warning] = DeprecationWarning,
*,
since: Optional[str] = None,
since: str | None = None,
):
"""Deprecated. Instead, use `@deprecate_arg`.

Expand Down Expand Up @@ -280,7 +281,7 @@ def deprecate_function(
stacklevel: int = 2,
category: Type[Warning] = DeprecationWarning,
*,
since: Optional[str] = None,
since: str | None = None,
):
"""Deprecated. Instead, use `@deprecate_func`.

Expand Down Expand Up @@ -313,15 +314,15 @@ def wrapper(*args, **kwargs):

def _maybe_warn_and_rename_kwarg(
args: tuple[Any, ...],
kwargs: Dict[str, Any],
kwargs: dict[str, Any],
*,
func_name: str,
original_func_co_varnames: tuple[str, ...],
old_arg_name: str,
new_alias: Optional[str],
new_alias: str | None,
warning_msg: str,
category: Type[Warning],
predicate: Optional[Callable[[Any], bool]],
predicate: Callable[[Any], bool] | None,
) -> None:
# In Python 3.10+, we should set `zip(strict=False)` (the default). That is, we want to
# stop iterating once `args` is done, since some args may have not been explicitly passed as
Expand Down Expand Up @@ -353,9 +354,11 @@ def _write_deprecation_msg(
pending: bool,
additional_msg: str,
removal_timeline: str,
) -> Tuple[str, Union[Type[DeprecationWarning], Type[PendingDeprecationWarning]]]:
) -> tuple[str, Type[DeprecationWarning] | Type[PendingDeprecationWarning]]:
if pending:
category = PendingDeprecationWarning
category: Type[DeprecationWarning] | Type[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be clearer to declare the type for category before the if pending line.

PendingDeprecationWarning
] = PendingDeprecationWarning
deprecation_status = "pending deprecation"
removal_desc = f"marked deprecated in a future release, and then removed {removal_timeline}"
else:
Expand Down Expand Up @@ -412,7 +415,7 @@ def _write_deprecation_msg(


def add_deprecation_to_docstring(
func: Callable, msg: str, *, since: Optional[str], pending: bool
func: Callable, msg: str, *, since: str | None, pending: bool
) -> None:
"""Dynamically insert the deprecation message into ``func``'s docstring.

Expand Down
7 changes: 3 additions & 4 deletions qiskit/utils/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
# that they have been altered from the originals.

"""SI unit utilities"""

from typing import Tuple, Optional, Union
from __future__ import annotations

import numpy as np

from qiskit.circuit.parameterexpression import ParameterExpression


def apply_prefix(value: Union[float, ParameterExpression], unit: str) -> float:
def apply_prefix(value: float | ParameterExpression, unit: str) -> float | ParameterExpression:
"""
Given a SI unit prefix and value, apply the prefix to convert to
standard SI unit.
Expand Down Expand Up @@ -69,7 +68,7 @@ def apply_prefix(value: Union[float, ParameterExpression], unit: str) -> float:
return value * pow(10, pow10)


def detach_prefix(value: float, decimal: Optional[int] = None) -> Tuple[float, str]:
def detach_prefix(value: float, decimal: int | None = None) -> tuple[float, str]:
"""
Given a SI unit value, find the most suitable prefix to scale the value.

Expand Down