From 3cda34ebdea94b325441a27eee60a2d9b31d1ece Mon Sep 17 00:00:00 2001 From: Yuanyuan Chen Date: Mon, 29 Sep 2025 03:35:32 +0000 Subject: [PATCH] [2/N] Apply ruff UP035 check in torch files (#164054) This is the result of applying the ruff `UP035` check. `Callable` is imported from `collections.abc` instead of `typing`. `TypeAlias` is also imported from `typing`. This PR is the follow-up of #163947. Pull Request resolved: https://github.com/pytorch/pytorch/pull/164054 Approved by: https://github.com/ezyang, https://github.com/Skylion007 --- torch/nn/attention/flex_attention.py | 3 ++- torch/nn/common_types.py | 3 +-- torch/nn/functional.py | 3 ++- torch/nn/init.py | 3 ++- torch/nn/modules/loss.py | 3 ++- torch/nn/modules/module.py | 4 ++-- torch/nn/modules/transformer.py | 3 ++- torch/nn/parallel/distributed.py | 3 ++- torch/nn/utils/_deprecation_utils.py | 2 +- .../utils/_expanded_weights/conv_expanded_weights.py | 3 ++- .../utils/_expanded_weights/expanded_weights_impl.py | 2 +- torch/nn/utils/clip_grad.py | 5 +++-- torch/nn/utils/rnn.py | 4 ++-- torch/package/analyze/trace_dependencies.py | 4 ++-- torch/package/package_exporter.py | 4 ++-- torch/package/package_importer.py | 4 ++-- torch/utils/_appending_byte_serializer.py | 4 ++-- torch/utils/_config_module.py | 12 ++---------- torch/utils/_contextlib.py | 3 ++- torch/utils/_cxx_pytree.py | 4 ++-- torch/utils/_exposed_in.py | 3 ++- torch/utils/_foreach_utils.py | 3 +-- torch/utils/_functools.py | 5 +++-- torch/utils/_pytree.py | 3 +-- torch/utils/_stats.py | 3 ++- torch/utils/_strobelight/cli_function_profiler.py | 4 ++-- torch/utils/_sympy/functions.py | 3 ++- torch/utils/_sympy/value_ranges.py | 4 ++-- torch/utils/_thunk.py | 3 ++- torch/utils/benchmark/utils/_stubs.py | 3 ++- torch/utils/benchmark/utils/compile.py | 3 ++- torch/utils/benchmark/utils/fuzzer.py | 3 ++- torch/utils/benchmark/utils/timer.py | 3 ++- .../utils/valgrind_wrapper/timer_interface.py | 3 ++- torch/utils/bundled_inputs.py | 4 ++-- torch/utils/data/_utils/collate.py | 3 ++- torch/utils/data/dataloader.py | 3 ++- torch/utils/data/datapipes/datapipe.py | 4 ++-- torch/utils/data/datapipes/iter/callable.py | 4 ++-- torch/utils/data/datapipes/iter/combining.py | 4 ++-- torch/utils/data/datapipes/iter/grouping.py | 4 ++-- torch/utils/data/datapipes/iter/routeddecoder.py | 4 ++-- torch/utils/data/datapipes/iter/selecting.py | 4 ++-- torch/utils/data/datapipes/map/callable.py | 3 ++- torch/utils/data/datapipes/utils/common.py | 4 ++-- torch/utils/flop_counter.py | 3 ++- torch/xpu/__init__.py | 3 ++- torch/xpu/_gpu_trace.py | 2 +- 48 files changed, 92 insertions(+), 79 deletions(-) diff --git a/torch/nn/attention/flex_attention.py b/torch/nn/attention/flex_attention.py index 5c4c8184a06f..0c5cb63626d5 100644 --- a/torch/nn/attention/flex_attention.py +++ b/torch/nn/attention/flex_attention.py @@ -8,8 +8,9 @@ import itertools import math import operator import warnings +from collections.abc import Callable from enum import Enum -from typing import Any, Callable, NamedTuple, Optional, Union +from typing import Any, NamedTuple, Optional, Union import torch from torch import Tensor diff --git a/torch/nn/common_types.py b/torch/nn/common_types.py index 0629e6441e45..a8a9d70f4002 100644 --- a/torch/nn/common_types.py +++ b/torch/nn/common_types.py @@ -1,5 +1,4 @@ -from typing import Optional, TypeVar, Union -from typing_extensions import TypeAlias as _TypeAlias +from typing import Optional, TypeAlias as _TypeAlias, TypeVar, Union from torch import Tensor diff --git a/torch/nn/functional.py b/torch/nn/functional.py index 92142fd44df8..6038520008ff 100644 --- a/torch/nn/functional.py +++ b/torch/nn/functional.py @@ -3,7 +3,8 @@ import importlib import math import warnings -from typing import Callable, Optional, TYPE_CHECKING, Union +from collections.abc import Callable +from typing import Optional, TYPE_CHECKING, Union import torch from torch import _VF, sym_int as _sym_int, Tensor diff --git a/torch/nn/init.py b/torch/nn/init.py index af31b6fa2282..8dc932fa8035 100644 --- a/torch/nn/init.py +++ b/torch/nn/init.py @@ -2,7 +2,8 @@ import math import warnings -from typing import Callable, Literal, Optional as _Optional, TypeVar, Union +from collections.abc import Callable +from typing import Literal, Optional as _Optional, TypeVar, Union from typing_extensions import ParamSpec import torch diff --git a/torch/nn/modules/loss.py b/torch/nn/modules/loss.py index cf234ed2c2df..c5d2a94cf394 100644 --- a/torch/nn/modules/loss.py +++ b/torch/nn/modules/loss.py @@ -1,5 +1,6 @@ # mypy: allow-untyped-defs -from typing import Callable, Optional, Union +from collections.abc import Callable +from typing import Optional, Union from typing_extensions import deprecated from torch import Tensor diff --git a/torch/nn/modules/module.py b/torch/nn/modules/module.py index bd00a09722c6..531315646e9f 100644 --- a/torch/nn/modules/module.py +++ b/torch/nn/modules/module.py @@ -6,8 +6,8 @@ import itertools import warnings import weakref from collections import namedtuple, OrderedDict -from collections.abc import Iterator, Mapping -from typing import Any, Callable, Optional, overload, TypeVar, Union +from collections.abc import Callable, Iterator, Mapping +from typing import Any, Optional, overload, TypeVar, Union from typing_extensions import Self import torch diff --git a/torch/nn/modules/transformer.py b/torch/nn/modules/transformer.py index ff76b644a679..1aad5d54bc1a 100644 --- a/torch/nn/modules/transformer.py +++ b/torch/nn/modules/transformer.py @@ -1,7 +1,8 @@ # mypy: allow-untyped-defs import copy import warnings -from typing import Any, Callable, Optional, Union +from collections.abc import Callable +from typing import Any, Optional, Union import torch import torch.nn.functional as F diff --git a/torch/nn/parallel/distributed.py b/torch/nn/parallel/distributed.py index c5db538f52bb..11389251ea2f 100644 --- a/torch/nn/parallel/distributed.py +++ b/torch/nn/parallel/distributed.py @@ -9,10 +9,11 @@ import sys import warnings import weakref from collections import defaultdict, deque +from collections.abc import Callable from contextlib import contextmanager from dataclasses import dataclass, fields, is_dataclass from enum import auto, Enum -from typing import Any, Callable, Optional, TYPE_CHECKING +from typing import Any, Optional, TYPE_CHECKING import torch import torch.distributed as dist diff --git a/torch/nn/utils/_deprecation_utils.py b/torch/nn/utils/_deprecation_utils.py index d20292b8c0bb..995da89c70bd 100644 --- a/torch/nn/utils/_deprecation_utils.py +++ b/torch/nn/utils/_deprecation_utils.py @@ -1,6 +1,6 @@ import importlib import warnings -from typing import Callable +from collections.abc import Callable _MESSAGE_TEMPLATE = ( diff --git a/torch/nn/utils/_expanded_weights/conv_expanded_weights.py b/torch/nn/utils/_expanded_weights/conv_expanded_weights.py index ad1adf06d0fe..9ccbc774612e 100644 --- a/torch/nn/utils/_expanded_weights/conv_expanded_weights.py +++ b/torch/nn/utils/_expanded_weights/conv_expanded_weights.py @@ -1,4 +1,5 @@ -from typing import Any, Callable, TypeVar +from collections.abc import Callable +from typing import Any, TypeVar from typing_extensions import ParamSpec import torch diff --git a/torch/nn/utils/_expanded_weights/expanded_weights_impl.py b/torch/nn/utils/_expanded_weights/expanded_weights_impl.py index 2dd4b6de4f69..3a5b99c41c65 100644 --- a/torch/nn/utils/_expanded_weights/expanded_weights_impl.py +++ b/torch/nn/utils/_expanded_weights/expanded_weights_impl.py @@ -1,7 +1,7 @@ # mypy: allow-untyped-defs import functools +from collections.abc import Callable from contextlib import contextmanager -from typing import Callable import torch from torch._decomp import decomposition_table diff --git a/torch/nn/utils/clip_grad.py b/torch/nn/utils/clip_grad.py index fd6e908e1ff0..4aefe32d48ed 100644 --- a/torch/nn/utils/clip_grad.py +++ b/torch/nn/utils/clip_grad.py @@ -4,8 +4,9 @@ import functools import types import typing import warnings -from typing import Callable, cast, Optional, TypeVar, Union -from typing_extensions import deprecated, ParamSpec, TypeAlias +from collections.abc import Callable +from typing import cast, Optional, TypeAlias, TypeVar, Union +from typing_extensions import deprecated, ParamSpec import torch from torch import Tensor diff --git a/torch/nn/utils/rnn.py b/torch/nn/utils/rnn.py index aa7c0f35a183..5529cbc83ef0 100644 --- a/torch/nn/utils/rnn.py +++ b/torch/nn/utils/rnn.py @@ -1,6 +1,6 @@ import warnings -from collections.abc import Iterable -from typing import Any, Callable, NamedTuple, Optional, overload, TypeVar, Union +from collections.abc import Callable, Iterable +from typing import Any, NamedTuple, Optional, overload, TypeVar, Union from typing_extensions import Self import torch diff --git a/torch/package/analyze/trace_dependencies.py b/torch/package/analyze/trace_dependencies.py index e029fe130bdd..839c2da8cabc 100644 --- a/torch/package/analyze/trace_dependencies.py +++ b/torch/package/analyze/trace_dependencies.py @@ -1,7 +1,7 @@ # mypy: allow-untyped-defs import sys -from collections.abc import Iterable -from typing import Any, Callable +from collections.abc import Callable, Iterable +from typing import Any __all__ = ["trace_dependencies"] diff --git a/torch/package/package_exporter.py b/torch/package/package_exporter.py index 6118e8ce8096..4ac2c33f1633 100644 --- a/torch/package/package_exporter.py +++ b/torch/package/package_exporter.py @@ -8,12 +8,12 @@ import pickletools import platform import types from collections import defaultdict, OrderedDict -from collections.abc import Sequence +from collections.abc import Callable, Sequence from dataclasses import dataclass from enum import Enum from importlib.machinery import SourceFileLoader from pathlib import Path -from typing import Any, Callable, cast, IO, Optional, Union +from typing import Any, cast, IO, Optional, Union import torch from torch.serialization import location_tag, normalize_storage_type diff --git a/torch/package/package_importer.py b/torch/package/package_importer.py index 7291227e42ae..10bf8981e28a 100644 --- a/torch/package/package_importer.py +++ b/torch/package/package_importer.py @@ -8,9 +8,9 @@ import linecache import os import sys import types -from collections.abc import Iterable +from collections.abc import Callable, Iterable from contextlib import contextmanager -from typing import Any, Callable, cast, Optional, TYPE_CHECKING, Union +from typing import Any, cast, Optional, TYPE_CHECKING, Union from weakref import WeakValueDictionary import torch diff --git a/torch/utils/_appending_byte_serializer.py b/torch/utils/_appending_byte_serializer.py index 91936ab6fc06..67f548481938 100644 --- a/torch/utils/_appending_byte_serializer.py +++ b/torch/utils/_appending_byte_serializer.py @@ -1,7 +1,7 @@ import base64 import zlib -from collections.abc import Iterable -from typing import Callable, Generic, TypeVar +from collections.abc import Callable, Iterable +from typing import Generic, TypeVar T = TypeVar("T") diff --git a/torch/utils/_config_module.py b/torch/utils/_config_module.py index 57c67aeab8f7..d24f58c60740 100644 --- a/torch/utils/_config_module.py +++ b/torch/utils/_config_module.py @@ -8,18 +8,10 @@ import os import pickle import tokenize import unittest +from collections.abc import Callable from dataclasses import dataclass from types import FunctionType, ModuleType -from typing import ( - Any, - Callable, - Generic, - NoReturn, - Optional, - TYPE_CHECKING, - TypeVar, - Union, -) +from typing import Any, Generic, NoReturn, Optional, TYPE_CHECKING, TypeVar, Union from typing_extensions import deprecated from unittest import mock diff --git a/torch/utils/_contextlib.py b/torch/utils/_contextlib.py index 8db27efa270a..10fc16a13c5d 100644 --- a/torch/utils/_contextlib.py +++ b/torch/utils/_contextlib.py @@ -6,7 +6,8 @@ import functools import inspect import sys import warnings -from typing import Any, Callable, cast, TypeVar +from collections.abc import Callable +from typing import Any, cast, TypeVar # Used for annotating the decorator usage of _DecoratorContextManager (e.g., diff --git a/torch/utils/_cxx_pytree.py b/torch/utils/_cxx_pytree.py index f5bf88907119..708a588fa8d0 100644 --- a/torch/utils/_cxx_pytree.py +++ b/torch/utils/_cxx_pytree.py @@ -14,8 +14,8 @@ collection support for PyTorch APIs. import functools import types -from collections.abc import Iterable -from typing import Any, Callable, Optional, overload, TypeVar, Union +from collections.abc import Callable, Iterable +from typing import Any, Optional, overload, TypeVar, Union from typing_extensions import deprecated, TypeIs import torch.utils._pytree as python_pytree diff --git a/torch/utils/_exposed_in.py b/torch/utils/_exposed_in.py index a0963b0e4e6a..2cca4ce240ad 100644 --- a/torch/utils/_exposed_in.py +++ b/torch/utils/_exposed_in.py @@ -1,4 +1,5 @@ -from typing import Callable, TypeVar +from collections.abc import Callable +from typing import TypeVar F = TypeVar("F") diff --git a/torch/utils/_foreach_utils.py b/torch/utils/_foreach_utils.py index e3a2070f2d4d..8b682d96c191 100644 --- a/torch/utils/_foreach_utils.py +++ b/torch/utils/_foreach_utils.py @@ -1,5 +1,4 @@ -from typing import Optional -from typing_extensions import TypeAlias +from typing import Optional, TypeAlias import torch from torch import Tensor diff --git a/torch/utils/_functools.py b/torch/utils/_functools.py index 0b555ffc27f9..e862953a908d 100644 --- a/torch/utils/_functools.py +++ b/torch/utils/_functools.py @@ -1,6 +1,7 @@ import functools -from typing import Callable, TypeVar -from typing_extensions import Concatenate, ParamSpec +from collections.abc import Callable +from typing import Concatenate, TypeVar +from typing_extensions import ParamSpec _P = ParamSpec("_P") diff --git a/torch/utils/_pytree.py b/torch/utils/_pytree.py index 69f3ddf56b87..07f9b09f9911 100644 --- a/torch/utils/_pytree.py +++ b/torch/utils/_pytree.py @@ -24,11 +24,10 @@ import threading import types import warnings from collections import defaultdict, deque, namedtuple, OrderedDict -from collections.abc import Hashable, Iterable, Mapping, Sequence +from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence from enum import Enum from typing import ( Any, - Callable, cast, ClassVar, Final, diff --git a/torch/utils/_stats.py b/torch/utils/_stats.py index 74b513932c30..b8a2978c3ea7 100644 --- a/torch/utils/_stats.py +++ b/torch/utils/_stats.py @@ -4,7 +4,8 @@ import collections import functools from collections import OrderedDict -from typing import Callable, TypeVar +from collections.abc import Callable +from typing import TypeVar from typing_extensions import ParamSpec diff --git a/torch/utils/_strobelight/cli_function_profiler.py b/torch/utils/_strobelight/cli_function_profiler.py index 9b94a7b7a484..ef1a6edc682e 100644 --- a/torch/utils/_strobelight/cli_function_profiler.py +++ b/torch/utils/_strobelight/cli_function_profiler.py @@ -6,9 +6,9 @@ import os import re import subprocess import time -from collections.abc import Sequence +from collections.abc import Callable, Sequence from threading import Lock -from typing import Any, Callable, Optional, TypeVar +from typing import Any, Optional, TypeVar from typing_extensions import ParamSpec diff --git a/torch/utils/_sympy/functions.py b/torch/utils/_sympy/functions.py index d06f6dfed2d8..53da60ff5017 100644 --- a/torch/utils/_sympy/functions.py +++ b/torch/utils/_sympy/functions.py @@ -3,7 +3,8 @@ import functools import math import operator import sys -from typing import Callable, Optional, SupportsFloat, TYPE_CHECKING, TypeVar, Union +from collections.abc import Callable +from typing import Optional, SupportsFloat, TYPE_CHECKING, TypeVar, Union from typing_extensions import TypeVarTuple, Unpack import sympy diff --git a/torch/utils/_sympy/value_ranges.py b/torch/utils/_sympy/value_ranges.py index e02e049cc36d..2137d21662f3 100644 --- a/torch/utils/_sympy/value_ranges.py +++ b/torch/utils/_sympy/value_ranges.py @@ -7,17 +7,17 @@ import itertools import logging import math import operator +from collections.abc import Callable from typing import ( - Callable, Generic, Optional, overload, SupportsFloat, TYPE_CHECKING, + TypeGuard, TypeVar, Union, ) -from typing_extensions import TypeGuard import sympy from sympy.logic.boolalg import Boolean as SympyBoolean, BooleanAtom diff --git a/torch/utils/_thunk.py b/torch/utils/_thunk.py index 08cf6efc96fc..28689f2f76f1 100644 --- a/torch/utils/_thunk.py +++ b/torch/utils/_thunk.py @@ -1,4 +1,5 @@ -from typing import Callable, Generic, Optional, TypeVar +from collections.abc import Callable +from typing import Generic, Optional, TypeVar R = TypeVar("R") diff --git a/torch/utils/benchmark/utils/_stubs.py b/torch/utils/benchmark/utils/_stubs.py index 068e62ec87a3..c91e3d12b29e 100644 --- a/torch/utils/benchmark/utils/_stubs.py +++ b/torch/utils/benchmark/utils/_stubs.py @@ -1,4 +1,5 @@ -from typing import Any, Callable +from typing import Any +from collections.abc import Callable from typing_extensions import Protocol, runtime_checkable diff --git a/torch/utils/benchmark/utils/compile.py b/torch/utils/benchmark/utils/compile.py index cee9c8d7f717..fbca93d221bd 100644 --- a/torch/utils/benchmark/utils/compile.py +++ b/torch/utils/benchmark/utils/compile.py @@ -1,5 +1,6 @@ # mypy: allow-untyped-defs -from typing import Any, Callable, cast, Optional, Union +from typing import Any, cast, Optional, Union +from collections.abc import Callable import torch import torch._dynamo diff --git a/torch/utils/benchmark/utils/fuzzer.py b/torch/utils/benchmark/utils/fuzzer.py index 6fd52a7aecd3..56c8189be2c9 100644 --- a/torch/utils/benchmark/utils/fuzzer.py +++ b/torch/utils/benchmark/utils/fuzzer.py @@ -1,7 +1,8 @@ # mypy: allow-untyped-defs import functools import itertools as it -from typing import Any, Callable, Optional, Union +from typing import Any, Optional, Union +from collections.abc import Callable import torch diff --git a/torch/utils/benchmark/utils/timer.py b/torch/utils/benchmark/utils/timer.py index 377fc1221dbe..9590b88ed153 100644 --- a/torch/utils/benchmark/utils/timer.py +++ b/torch/utils/benchmark/utils/timer.py @@ -2,7 +2,8 @@ import enum import timeit import textwrap -from typing import overload, Any, Callable, NoReturn, Optional, Union +from typing import overload, Any, NoReturn, Optional, Union +from collections.abc import Callable import torch from torch.utils.benchmark.utils import common, cpp_jit diff --git a/torch/utils/benchmark/utils/valgrind_wrapper/timer_interface.py b/torch/utils/benchmark/utils/valgrind_wrapper/timer_interface.py index 900d8c3722a8..6777d7ba41b4 100644 --- a/torch/utils/benchmark/utils/valgrind_wrapper/timer_interface.py +++ b/torch/utils/benchmark/utils/valgrind_wrapper/timer_interface.py @@ -11,8 +11,9 @@ import subprocess import sys import textwrap from typing import ( - cast, Any, Callable, NamedTuple, + cast, Any, NamedTuple, Optional, Union, TYPE_CHECKING) +from collections.abc import Callable from collections.abc import Iterator import torch diff --git a/torch/utils/bundled_inputs.py b/torch/utils/bundled_inputs.py index 6209fc8ee874..62972c354e7d 100644 --- a/torch/utils/bundled_inputs.py +++ b/torch/utils/bundled_inputs.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # mypy: allow-untyped-defs -from typing import Any, TypeVar, Optional, NamedTuple, Union, Callable -from collections.abc import Sequence +from typing import Any, TypeVar, Optional, NamedTuple, Union +from collections.abc import Callable, Sequence import textwrap import torch from torch._C import TupleType, ListType diff --git a/torch/utils/data/_utils/collate.py b/torch/utils/data/_utils/collate.py index 3b291b1e60a4..e5da3b3ba2b6 100644 --- a/torch/utils/data/_utils/collate.py +++ b/torch/utils/data/_utils/collate.py @@ -12,7 +12,8 @@ import collections import contextlib import copy import re -from typing import Callable, Optional, Union +from collections.abc import Callable +from typing import Optional, Union import torch diff --git a/torch/utils/data/dataloader.py b/torch/utils/data/dataloader.py index 991b4f00eb85..f48f726e7955 100644 --- a/torch/utils/data/dataloader.py +++ b/torch/utils/data/dataloader.py @@ -16,7 +16,8 @@ import os import queue import threading import warnings -from typing import Any, Callable, Generic, Optional, TYPE_CHECKING, TypeVar, Union +from collections.abc import Callable +from typing import Any, Generic, Optional, TYPE_CHECKING, TypeVar, Union from typing_extensions import Self import torch diff --git a/torch/utils/data/datapipes/datapipe.py b/torch/utils/data/datapipes/datapipe.py index 506f642c411d..54b0f7510923 100644 --- a/torch/utils/data/datapipes/datapipe.py +++ b/torch/utils/data/datapipes/datapipe.py @@ -1,7 +1,7 @@ import functools import pickle -from collections.abc import Iterable, Iterator -from typing import Callable, Optional, TypeVar +from collections.abc import Callable, Iterable, Iterator +from typing import Optional, TypeVar from torch.utils._import_utils import import_dill from torch.utils.data.datapipes._hook_iterator import _SnapshotState diff --git a/torch/utils/data/datapipes/iter/callable.py b/torch/utils/data/datapipes/iter/callable.py index 41c6bb362af2..335b8c888668 100644 --- a/torch/utils/data/datapipes/iter/callable.py +++ b/torch/utils/data/datapipes/iter/callable.py @@ -1,8 +1,8 @@ # mypy: allow-untyped-defs import functools from collections import namedtuple -from collections.abc import Iterator, Sized -from typing import Any, Callable, Optional, TypeVar, Union +from collections.abc import Callable, Iterator, Sized +from typing import Any, Optional, TypeVar, Union import torch from torch.utils.data._utils.collate import default_collate diff --git a/torch/utils/data/datapipes/iter/combining.py b/torch/utils/data/datapipes/iter/combining.py index 8c6abc506210..d1495dd81863 100644 --- a/torch/utils/data/datapipes/iter/combining.py +++ b/torch/utils/data/datapipes/iter/combining.py @@ -3,8 +3,8 @@ import copy as copymodule import warnings from abc import ABC, abstractmethod from collections import deque -from collections.abc import Iterator, Sized -from typing import Any, Callable, Literal, Optional, TypeVar +from collections.abc import Callable, Iterator, Sized +from typing import Any, Literal, Optional, TypeVar from torch.utils.data.datapipes._decorator import functional_datapipe from torch.utils.data.datapipes._hook_iterator import _SnapshotState diff --git a/torch/utils/data/datapipes/iter/grouping.py b/torch/utils/data/datapipes/iter/grouping.py index e7e50d302e12..72f4cb49b60d 100644 --- a/torch/utils/data/datapipes/iter/grouping.py +++ b/torch/utils/data/datapipes/iter/grouping.py @@ -1,7 +1,7 @@ # mypy: allow-untyped-defs from collections import defaultdict -from collections.abc import Iterator, Sized -from typing import Any, Callable, Optional, TypeVar +from collections.abc import Callable, Iterator, Sized +from typing import Any, Optional, TypeVar from torch.utils.data.datapipes._decorator import functional_datapipe from torch.utils.data.datapipes.datapipe import DataChunk, IterDataPipe diff --git a/torch/utils/data/datapipes/iter/routeddecoder.py b/torch/utils/data/datapipes/iter/routeddecoder.py index 611b4870a493..ba4d708a0a31 100644 --- a/torch/utils/data/datapipes/iter/routeddecoder.py +++ b/torch/utils/data/datapipes/iter/routeddecoder.py @@ -1,6 +1,6 @@ -from collections.abc import Iterable, Iterator, Sized +from collections.abc import Callable, Iterable, Iterator, Sized from io import BufferedIOBase -from typing import Any, Callable +from typing import Any from torch.utils.data.datapipes._decorator import functional_datapipe from torch.utils.data.datapipes.datapipe import IterDataPipe diff --git a/torch/utils/data/datapipes/iter/selecting.py b/torch/utils/data/datapipes/iter/selecting.py index 97dcb2d6c491..78d1820cb6aa 100644 --- a/torch/utils/data/datapipes/iter/selecting.py +++ b/torch/utils/data/datapipes/iter/selecting.py @@ -1,6 +1,6 @@ # mypy: allow-untyped-defs -from collections.abc import Iterator -from typing import Callable, TypeVar +from collections.abc import Callable, Iterator +from typing import TypeVar from torch.utils.data.datapipes._decorator import functional_datapipe from torch.utils.data.datapipes.dataframe import dataframe_wrapper as df_wrapper diff --git a/torch/utils/data/datapipes/map/callable.py b/torch/utils/data/datapipes/map/callable.py index cee08b7a8c8d..be55dc160d73 100644 --- a/torch/utils/data/datapipes/map/callable.py +++ b/torch/utils/data/datapipes/map/callable.py @@ -1,5 +1,6 @@ # mypy: allow-untyped-defs -from typing import Callable, TypeVar +from collections.abc import Callable +from typing import TypeVar from torch.utils.data.datapipes._decorator import functional_datapipe from torch.utils.data.datapipes.datapipe import MapDataPipe diff --git a/torch/utils/data/datapipes/utils/common.py b/torch/utils/data/datapipes/utils/common.py index ddf3eecdd949..55bceec0cb18 100644 --- a/torch/utils/data/datapipes/utils/common.py +++ b/torch/utils/data/datapipes/utils/common.py @@ -4,9 +4,9 @@ import functools import inspect import os import warnings -from collections.abc import Iterable +from collections.abc import Callable, Iterable from io import IOBase -from typing import Any, Callable, Optional, Union +from typing import Any, Optional, Union from torch.utils._import_utils import dill_available diff --git a/torch/utils/flop_counter.py b/torch/utils/flop_counter.py index b8d4e878b7f0..9fcc87a28fa3 100644 --- a/torch/utils/flop_counter.py +++ b/torch/utils/flop_counter.py @@ -2,7 +2,8 @@ import torch from torch.utils._pytree import tree_map, tree_flatten, tree_unflatten from .module_tracker import ModuleTracker -from typing import Any, Optional, Union, TypeVar, Callable +from typing import Any, Optional, Union, TypeVar +from collections.abc import Callable from collections.abc import Iterator from typing_extensions import ParamSpec from collections import defaultdict diff --git a/torch/xpu/__init__.py b/torch/xpu/__init__.py index 6e15bf4380e3..df2b8a6f5334 100644 --- a/torch/xpu/__init__.py +++ b/torch/xpu/__init__.py @@ -9,8 +9,9 @@ This package is lazily initialized, so you can always import it, and use import threading import traceback +from collections.abc import Callable from functools import lru_cache -from typing import Any, Callable, Optional, Union +from typing import Any, Optional, Union import torch import torch._C diff --git a/torch/xpu/_gpu_trace.py b/torch/xpu/_gpu_trace.py index 9ab5ac8f1bad..7c3a8b9bf785 100644 --- a/torch/xpu/_gpu_trace.py +++ b/torch/xpu/_gpu_trace.py @@ -1,4 +1,4 @@ -from typing import Callable +from collections.abc import Callable from torch._utils import CallbackRegistry