diff --git a/setup.py b/setup.py index 55fdd3c066d7..b147c87caf74 100644 --- a/setup.py +++ b/setup.py @@ -1132,7 +1132,7 @@ def main(): ) install_requires = [ "filelock", - "typing-extensions>=4.10.0", + "typing-extensions>=4.8.0", 'setuptools ; python_version >= "3.12"', 'sympy==1.12.1 ; python_version == "3.8"', 'sympy==1.13.1 ; python_version >= "3.9"', diff --git a/torch/__init__.py b/torch/__init__.py index 26bb4f846bab..0a0776ce459f 100644 --- a/torch/__init__.py +++ b/torch/__init__.py @@ -34,7 +34,7 @@ from typing import ( TypeVar as _TypeVar, Union as _Union, ) -from typing_extensions import ParamSpec as _ParamSpec, TypeIs as _TypeIs +from typing_extensions import ParamSpec as _ParamSpec, TypeGuard as _TypeGuard if TYPE_CHECKING: @@ -1002,7 +1002,7 @@ def typename(obj: _Any, /) -> str: return f"{module}.{qualname}" -def is_tensor(obj: _Any, /) -> _TypeIs["torch.Tensor"]: +def is_tensor(obj: _Any, /) -> _TypeGuard["torch.Tensor"]: r"""Returns True if `obj` is a PyTorch tensor. Note that this function is simply doing ``isinstance(obj, Tensor)``. @@ -1022,7 +1022,7 @@ def is_tensor(obj: _Any, /) -> _TypeIs["torch.Tensor"]: return isinstance(obj, torch.Tensor) -def is_storage(obj: _Any, /) -> _TypeIs[_Union["TypedStorage", "UntypedStorage"]]: +def is_storage(obj: _Any, /) -> _TypeGuard[_Union["TypedStorage", "UntypedStorage"]]: r"""Returns True if `obj` is a PyTorch storage object. Args: diff --git a/torch/_dynamo/utils.py b/torch/_dynamo/utils.py index d423333b8862..2d3f7ce226c2 100644 --- a/torch/_dynamo/utils.py +++ b/torch/_dynamo/utils.py @@ -53,7 +53,7 @@ from typing import ( Union, ValuesView, ) -from typing_extensions import Literal, TypeIs +from typing_extensions import Literal, TypeGuard import torch import torch._functorch.config @@ -532,14 +532,14 @@ class ExactWeakKeyDictionary: @overload -def istype(obj: object, allowed_types: Type[T]) -> TypeIs[T]: +def istype(obj: object, allowed_types: Type[T]) -> TypeGuard[T]: ... @overload def istype( obj: object, allowed_types: Tuple[Type[List[T]], Type[Tuple[T, ...]]] -) -> TypeIs[T]: +) -> TypeGuard[T]: ... diff --git a/torch/_inductor/pattern_matcher.py b/torch/_inductor/pattern_matcher.py index 51661bd33247..3a4d5c429210 100644 --- a/torch/_inductor/pattern_matcher.py +++ b/torch/_inductor/pattern_matcher.py @@ -70,7 +70,7 @@ from typing import ( TypeVar, Union, ) -from typing_extensions import Self, TypeIs +from typing_extensions import Self, TypeGuard import torch import torch._guards @@ -277,10 +277,10 @@ class FailedMatch(RuntimeError): MatchResult = Union[Match, FailedMatch] -def is_match(m: MatchResult) -> TypeIs[Match]: +def is_match(m: MatchResult) -> TypeGuard[Match]: """ - TypeIs cannot act on `self`. Thus this function exists to let mypy - recognize FailedMatch.__bool__ as a TypeIs. + TypeGuards cannot act on `self`. Thus this function exists to let mypy + recognize FailedMatch.__bool__ as a TypeGuard. """ return bool(m) diff --git a/torch/_subclasses/fake_tensor.py b/torch/_subclasses/fake_tensor.py index afddd87bfa7d..747b118921f7 100644 --- a/torch/_subclasses/fake_tensor.py +++ b/torch/_subclasses/fake_tensor.py @@ -31,7 +31,7 @@ from typing import ( TypeVar, Union, ) -from typing_extensions import Self, TypeIs +from typing_extensions import Self, TypeGuard from weakref import ReferenceType import torch @@ -168,7 +168,7 @@ def get_plain_tensors(subclass: Tensor) -> List[Tensor]: return plain_tensors -def is_fake(x: object) -> TypeIs[Tensor]: +def is_fake(x: object) -> TypeGuard[Tensor]: if isinstance(x, FakeTensor): return True if is_traceable_wrapper_subclass(x): @@ -1213,7 +1213,7 @@ class FakeTensorMode(TorchDispatchMode): # In this case, it's insufficient to test only one FakeTensor: you need # to distinguish between our fake tensor and other fake tensors. That's # what this function does. - def is_our_fake(self, t: object) -> TypeIs[FakeTensor]: + def is_our_fake(self, t: object) -> TypeGuard[FakeTensor]: return isinstance(t, FakeTensor) and t.fake_mode is self # If we should avoid device init. This changes the behavior of various APIs: diff --git a/torch/masked/maskedtensor/core.py b/torch/masked/maskedtensor/core.py index 59e8e9530636..69850df81c65 100644 --- a/torch/masked/maskedtensor/core.py +++ b/torch/masked/maskedtensor/core.py @@ -3,7 +3,7 @@ import warnings from typing import Any -from typing_extensions import TypeIs +from typing_extensions import TypeGuard import torch from torch.overrides import get_default_nowrap_functions @@ -15,7 +15,7 @@ __all__ = [ ] -def is_masked_tensor(obj: Any, /) -> TypeIs["MaskedTensor"]: +def is_masked_tensor(obj: Any, /) -> TypeGuard["MaskedTensor"]: r"""Returns True if the input is a MaskedTensor, else False Args: diff --git a/torch/nn/parameter.pyi b/torch/nn/parameter.pyi index 6b5afa860b86..9c998fb07f2c 100644 --- a/torch/nn/parameter.pyi +++ b/torch/nn/parameter.pyi @@ -1,5 +1,5 @@ # mypy: allow-untyped-defs -from typing_extensions import TypeIs +from typing_extensions import TypeGuard from torch import device, dtype, Tensor @@ -8,7 +8,7 @@ class Parameter(Tensor): def is_lazy( param: Tensor, -) -> TypeIs[UninitializedParameter | UninitializedBuffer]: ... +) -> TypeGuard[UninitializedParameter | UninitializedBuffer]: ... class UninitializedParameter(Tensor): def __init__(self, data: Tensor = ..., requires_grad: bool = ...) -> None: ... diff --git a/torch/serialization.py b/torch/serialization.py index 8d02b1bd72fc..36e5c035ff15 100644 --- a/torch/serialization.py +++ b/torch/serialization.py @@ -27,7 +27,7 @@ from typing import ( Type, Union, ) -from typing_extensions import TypeAlias, TypeIs +from typing_extensions import TypeAlias, TypeGuard # Python 3.10+ import torch import torch._weights_only_unpickler as _weights_only_unpickler @@ -549,7 +549,7 @@ def storage_to_tensor_type(storage): return getattr(module, storage_type.__name__.replace("Storage", "Tensor")) -def _is_path(name_or_buffer) -> TypeIs[Union[str, os.PathLike]]: +def _is_path(name_or_buffer) -> TypeGuard[Union[str, os.PathLike]]: return isinstance(name_or_buffer, (str, os.PathLike)) diff --git a/torch/utils/_python_dispatch.py b/torch/utils/_python_dispatch.py index 45d780306a14..7e8d3bd0d890 100644 --- a/torch/utils/_python_dispatch.py +++ b/torch/utils/_python_dispatch.py @@ -4,7 +4,7 @@ import contextlib import warnings from dataclasses import dataclass from typing import Any, Dict, List, Optional, Set, Union, Protocol, Tuple, Sequence, overload, Deque -from typing_extensions import TypeIs +from typing_extensions import TypeGuard from collections import deque import torch @@ -354,7 +354,7 @@ class TensorWithFlatten(Protocol): -def is_traceable_wrapper_subclass(t: object) -> TypeIs[TensorWithFlatten]: +def is_traceable_wrapper_subclass(t: object) -> TypeGuard[TensorWithFlatten]: """ Returns whether or not a tensor subclass that implements __torch_dispatch__ is 'traceable' with torch.compile.