Skip to content

Commit

Permalink
Resolve PYI041 (Lightning-AI#2151)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicki Skafte Detlefsen <skaftenicki@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 18, 2023
1 parent 1442e53 commit 4fe6d05
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 94 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ ignore = [
"S310", # todo: Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected. # todo
"B905", # todo: `zip()` without an explicit `strict=` parameter
"PYI024", # todo: Use `typing.NamedTuple` instead of `collections.namedtuple`
"PYI041", # todo: Use `float` instead of `int | float``
]
# Exclude a variety of commonly ignored directories.
exclude = [
Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/functional/image/_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _spectral_distortion_index(
def _error_relative_global_dimensionless_synthesis(
preds: Tensor,
target: Tensor,
ratio: Union[int, float] = 4,
ratio: float = 4,
reduction: Literal["elementwise_mean", "sum", "none", None] = "elementwise_mean",
) -> Tensor:
"""Wrapper for deprecated import.
Expand Down
6 changes: 3 additions & 3 deletions src/torchmetrics/functional/image/ergas.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple, Union
from typing import Tuple

import torch
from torch import Tensor
Expand Down Expand Up @@ -46,7 +46,7 @@ def _ergas_update(preds: Tensor, target: Tensor) -> Tuple[Tensor, Tensor]:
def _ergas_compute(
preds: Tensor,
target: Tensor,
ratio: Union[int, float] = 4,
ratio: float = 4,
reduction: Literal["elementwise_mean", "sum", "none", None] = "elementwise_mean",
) -> Tensor:
"""Erreur Relative Globale Adimensionnelle de Synthèse.
Expand Down Expand Up @@ -86,7 +86,7 @@ def _ergas_compute(
def error_relative_global_dimensionless_synthesis(
preds: Tensor,
target: Tensor,
ratio: Union[int, float] = 4,
ratio: float = 4,
reduction: Literal["elementwise_mean", "sum", "none", None] = "elementwise_mean",
) -> Tensor:
"""Erreur Relative Globale Adimensionnelle de Synthèse.
Expand Down
4 changes: 2 additions & 2 deletions src/torchmetrics/functional/multimodal/clip_iqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _clip_iqa_update(
images: Tensor,
model: _CLIPModel,
processor: _CLIPProcessor,
data_range: Union[int, float],
data_range: float,
device: Union[str, torch.device],
) -> Tensor:
images = images / float(data_range)
Expand Down Expand Up @@ -221,7 +221,7 @@ def clip_image_quality_assessment(
"openai/clip-vit-large-patch14-336",
"openai/clip-vit-large-patch14",
] = "clip_iqa",
data_range: Union[int, float] = 1.0,
data_range: float = 1.0,
prompts: Tuple[Union[str, Tuple[str, str]]] = ("quality",),
) -> Union[Tensor, Dict[str, Tensor]]:
"""Calculates `CLIP-IQA`_, that can be used to measure the visual content of images.
Expand Down
8 changes: 4 additions & 4 deletions src/torchmetrics/functional/nominal/cramers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
from typing import Optional, Union
from typing import Optional

import torch
from torch import Tensor
Expand All @@ -34,7 +34,7 @@ def _cramers_v_update(
target: Tensor,
num_classes: int,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
"""Compute the bins to update the confusion matrix with for Cramer's V calculation.
Expand Down Expand Up @@ -90,7 +90,7 @@ def cramers_v(
target: Tensor,
bias_correction: bool = True,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
r"""Compute `Cramer's V`_ statistic measuring the association between two categorical (nominal) data series.
Expand Down Expand Up @@ -142,7 +142,7 @@ def cramers_v_matrix(
matrix: Tensor,
bias_correction: bool = True,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
r"""Compute `Cramer's V`_ statistic between a set of multiple variables.
Expand Down
8 changes: 4 additions & 4 deletions src/torchmetrics/functional/nominal/pearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
from typing import Optional, Union
from typing import Optional

import torch
from torch import Tensor
Expand All @@ -32,7 +32,7 @@ def _pearsons_contingency_coefficient_update(
target: Tensor,
num_classes: int,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
"""Compute the bins to update the confusion matrix with for Pearson's Contingency Coefficient calculation.
Expand Down Expand Up @@ -76,7 +76,7 @@ def pearsons_contingency_coefficient(
preds: Tensor,
target: Tensor,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
r"""Compute `Pearson's Contingency Coefficient`_ for measuring the association between two categorical data series.
Expand Down Expand Up @@ -131,7 +131,7 @@ def pearsons_contingency_coefficient(
def pearsons_contingency_coefficient_matrix(
matrix: Tensor,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
r"""Compute `Pearson's Contingency Coefficient`_ statistic between a set of multiple variables.
Expand Down
8 changes: 4 additions & 4 deletions src/torchmetrics/functional/nominal/theils_u.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
from typing import Optional, Union
from typing import Optional

import torch
from torch import Tensor
Expand Down Expand Up @@ -57,7 +57,7 @@ def _theils_u_update(
target: Tensor,
num_classes: int,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
"""Compute the bins to update the confusion matrix with for Theil's U calculation.
Expand Down Expand Up @@ -109,7 +109,7 @@ def theils_u(
preds: Tensor,
target: Tensor,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
r"""Compute `Theils Uncertainty coefficient`_ statistic measuring the association between two nominal data series.
Expand Down Expand Up @@ -154,7 +154,7 @@ def theils_u(
def theils_u_matrix(
matrix: Tensor,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
r"""Compute `Theil's U`_ statistic between a set of multiple variables.
Expand Down
8 changes: 4 additions & 4 deletions src/torchmetrics/functional/nominal/tschuprows.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
from typing import Optional, Union
from typing import Optional

import torch
from torch import Tensor
Expand All @@ -34,7 +34,7 @@ def _tschuprows_t_update(
target: Tensor,
num_classes: int,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
"""Compute the bins to update the confusion matrix with for Tschuprow's T calculation.
Expand Down Expand Up @@ -92,7 +92,7 @@ def tschuprows_t(
target: Tensor,
bias_correction: bool = True,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
r"""Compute `Tschuprow's T`_ statistic measuring the association between two categorical (nominal) data series.
Expand Down Expand Up @@ -148,7 +148,7 @@ def tschuprows_t_matrix(
matrix: Tensor,
bias_correction: bool = True,
nan_strategy: Literal["replace", "drop"] = "replace",
nan_replace_value: Optional[Union[int, float]] = 0.0,
nan_replace_value: Optional[float] = 0.0,
) -> Tensor:
r"""Compute `Tschuprow's T`_ statistic between a set of multiple variables.
Expand Down
6 changes: 3 additions & 3 deletions src/torchmetrics/functional/nominal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional, Tuple, Union
from typing import Optional, Tuple

import torch
from torch import Tensor
Expand All @@ -20,12 +20,12 @@
from torchmetrics.utilities.prints import rank_zero_warn


def _nominal_input_validation(nan_strategy: str, nan_replace_value: Optional[Union[int, float]]) -> None:
def _nominal_input_validation(nan_strategy: str, nan_replace_value: Optional[float]) -> None:
if nan_strategy not in ["replace", "drop"]:
raise ValueError(
f"Argument `nan_strategy` is expected to be one of `['replace', 'drop']`, but got {nan_strategy}"
)
if nan_strategy == "replace" and not isinstance(nan_replace_value, (int, float)):
if nan_strategy == "replace" and not isinstance(nan_replace_value, (float, int)):
raise ValueError(
"Argument `nan_replace` is expected to be of a type `int` or `float` when `nan_strategy = 'replace`, "
f"but got {nan_replace_value}"
Expand Down
6 changes: 3 additions & 3 deletions src/torchmetrics/functional/pairwise/minkowski.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional, Union
from typing import Optional

import torch
from torch import Tensor
Expand All @@ -22,7 +22,7 @@


def _pairwise_minkowski_distance_update(
x: Tensor, y: Optional[Tensor] = None, exponent: Union[int, float] = 2, zero_diagonal: Optional[bool] = None
x: Tensor, y: Optional[Tensor] = None, exponent: float = 2, zero_diagonal: Optional[bool] = None
) -> Tensor:
"""Calculate the pairwise minkowski distance matrix.
Expand All @@ -49,7 +49,7 @@ def _pairwise_minkowski_distance_update(
def pairwise_minkowski_distance(
x: Tensor,
y: Optional[Tensor] = None,
exponent: Union[int, float] = 2,
exponent: float = 2,
reduction: Literal["mean", "sum", "none", None] = None,
zero_diagonal: Optional[bool] = None,
) -> Tensor:
Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/image/_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class _ErrorRelativeGlobalDimensionlessSynthesis(ErrorRelativeGlobalDimensionles

def __init__(
self,
ratio: Union[int, float] = 4,
ratio: float = 4,
reduction: Literal["elementwise_mean", "sum", "none", None] = "elementwise_mean",
**kwargs: Any,
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/image/ergas.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ErrorRelativeGlobalDimensionlessSynthesis(Metric):

def __init__(
self,
ratio: Union[int, float] = 4,
ratio: float = 4,
reduction: Literal["elementwise_mean", "sum", "none", None] = "elementwise_mean",
**kwargs: Any,
) -> None:
Expand Down
Loading

0 comments on commit 4fe6d05

Please sign in to comment.