diff --git a/benchmarks/dynamo/cachebench.py b/benchmarks/dynamo/cachebench.py index 9244612b5aeb..c4d79a1b12ce 100644 --- a/benchmarks/dynamo/cachebench.py +++ b/benchmarks/dynamo/cachebench.py @@ -6,7 +6,7 @@ import os import subprocess import sys import tempfile -from typing import Callable +from collections.abc import Callable from torch._inductor.utils import fresh_cache diff --git a/benchmarks/dynamo/genai_layers/utils.py b/benchmarks/dynamo/genai_layers/utils.py index 749b9cea2032..2db2d7300df5 100644 --- a/benchmarks/dynamo/genai_layers/utils.py +++ b/benchmarks/dynamo/genai_layers/utils.py @@ -1,7 +1,8 @@ import os from collections import defaultdict +from collections.abc import Callable from dataclasses import dataclass -from typing import Any, Callable, Optional +from typing import Any, Optional import matplotlib.pyplot as plt diff --git a/benchmarks/dynamo/torchao_backend.py b/benchmarks/dynamo/torchao_backend.py index 96e1c4569274..6b4204db7b36 100644 --- a/benchmarks/dynamo/torchao_backend.py +++ b/benchmarks/dynamo/torchao_backend.py @@ -1,4 +1,5 @@ -from typing import Any, Callable +from collections.abc import Callable +from typing import Any import torch diff --git a/benchmarks/functional_autograd_benchmark/functional_autograd_benchmark.py b/benchmarks/functional_autograd_benchmark/functional_autograd_benchmark.py index a974eb8ae5ca..9d5772c4f124 100644 --- a/benchmarks/functional_autograd_benchmark/functional_autograd_benchmark.py +++ b/benchmarks/functional_autograd_benchmark/functional_autograd_benchmark.py @@ -1,7 +1,8 @@ import time from argparse import ArgumentParser from collections import defaultdict -from typing import Any, Callable, NamedTuple +from collections.abc import Callable +from typing import Any, NamedTuple import torch from torch.autograd import functional diff --git a/benchmarks/functional_autograd_benchmark/utils.py b/benchmarks/functional_autograd_benchmark/utils.py index 46f0061cd3fe..8efc0bdcddd1 100644 --- a/benchmarks/functional_autograd_benchmark/utils.py +++ b/benchmarks/functional_autograd_benchmark/utils.py @@ -1,5 +1,6 @@ from collections import defaultdict -from typing import Callable, Optional, Union +from collections.abc import Callable +from typing import Optional, Union import torch from torch import nn, Tensor diff --git a/benchmarks/gpt_fast/common.py b/benchmarks/gpt_fast/common.py index 5d9fc7c4aa6b..4cbd0bd0f2dc 100644 --- a/benchmarks/gpt_fast/common.py +++ b/benchmarks/gpt_fast/common.py @@ -1,5 +1,6 @@ import dataclasses -from typing import Callable, Optional +from collections.abc import Callable +from typing import Optional all_experiments: dict[str, Callable] = {} diff --git a/benchmarks/inductor_backends/cutlass.py b/benchmarks/inductor_backends/cutlass.py index 7141872ec3c4..b2ed506302ae 100644 --- a/benchmarks/inductor_backends/cutlass.py +++ b/benchmarks/inductor_backends/cutlass.py @@ -9,8 +9,9 @@ import logging import time from abc import abstractmethod from collections import defaultdict +from collections.abc import Callable from dataclasses import asdict, dataclass, field -from typing import Any, Callable, Optional +from typing import Any, Optional from tabulate import tabulate from tqdm import tqdm diff --git a/benchmarks/transformer/attention_bias_benchmarks.py b/benchmarks/transformer/attention_bias_benchmarks.py index 2154e11237e9..f6bf45063309 100644 --- a/benchmarks/transformer/attention_bias_benchmarks.py +++ b/benchmarks/transformer/attention_bias_benchmarks.py @@ -1,7 +1,8 @@ import itertools +from collections.abc import Callable from dataclasses import asdict, dataclass from functools import partial -from typing import Callable, Union +from typing import Union import numpy as np from tabulate import tabulate diff --git a/benchmarks/transformer/score_mod.py b/benchmarks/transformer/score_mod.py index 4be4a1e7c46c..f812ede7f635 100644 --- a/benchmarks/transformer/score_mod.py +++ b/benchmarks/transformer/score_mod.py @@ -3,10 +3,11 @@ import csv import itertools import random from collections import defaultdict +from collections.abc import Callable from contextlib import nullcontext from dataclasses import asdict, dataclass from functools import partial -from typing import Callable, Optional, Union +from typing import Optional, Union import numpy as np from tabulate import tabulate diff --git a/benchmarks/transformer/sdpa.py b/benchmarks/transformer/sdpa.py index 2eca4bf06b44..b4bc77bafdd6 100644 --- a/benchmarks/transformer/sdpa.py +++ b/benchmarks/transformer/sdpa.py @@ -1,8 +1,8 @@ import itertools from collections import defaultdict +from collections.abc import Callable from contextlib import nullcontext from dataclasses import asdict, dataclass -from typing import Callable from tabulate import tabulate from tqdm import tqdm diff --git a/functorch/dim/__init__.py b/functorch/dim/__init__.py index 1d7a4307c310..df9ca766e28f 100644 --- a/functorch/dim/__init__.py +++ b/functorch/dim/__init__.py @@ -3,11 +3,11 @@ from __future__ import annotations import dis import inspect import sys -from typing import Any, Callable, Optional, TYPE_CHECKING, Union +from typing import Any, Optional, TYPE_CHECKING, Union if TYPE_CHECKING: - from collections.abc import Sequence + from collections.abc import Callable, Sequence import torch from torch.utils._pytree import tree_flatten, tree_map, tree_unflatten diff --git a/functorch/dim/_wrap.py b/functorch/dim/_wrap.py index 4b359f6a1d58..3c3a12b54ceb 100644 --- a/functorch/dim/_wrap.py +++ b/functorch/dim/_wrap.py @@ -5,7 +5,7 @@ Python implementation of function wrapping functionality for functorch.dim. from __future__ import annotations import functools -from typing import Any, Callable, Optional +from typing import Any, Optional, TYPE_CHECKING import torch from torch.utils._pytree import tree_map @@ -15,6 +15,10 @@ from ._enable_all_layers import EnableAllLayers from ._tensor_info import TensorInfo +if TYPE_CHECKING: + from collections.abc import Callable + + def handle_from_tensor(tensor: torch.Tensor) -> torch.Tensor: """Handle tensor conversion for torch function integration.""" return tensor diff --git a/functorch/dim/wrap_type.py b/functorch/dim/wrap_type.py index cf4a195f3c74..5020e756ce6c 100644 --- a/functorch/dim/wrap_type.py +++ b/functorch/dim/wrap_type.py @@ -5,6 +5,7 @@ # LICENSE file in the root directory of this source tree. import functools +from collections.abc import Callable from types import ( BuiltinMethodType, FunctionType, @@ -12,7 +13,7 @@ from types import ( MethodDescriptorType, WrapperDescriptorType, ) -from typing import Any, Callable +from typing import Any FUNC_TYPES = ( diff --git a/functorch/einops/rearrange.py b/functorch/einops/rearrange.py index 473a43816668..21e3bfaad4d8 100644 --- a/functorch/einops/rearrange.py +++ b/functorch/einops/rearrange.py @@ -1,7 +1,7 @@ from __future__ import annotations import functools -from typing import Callable, TYPE_CHECKING, Union +from typing import TYPE_CHECKING, Union import torch from functorch.dim import dims # noqa: F401 @@ -16,7 +16,7 @@ from ._parsing import ( if TYPE_CHECKING: - from collections.abc import Sequence + from collections.abc import Callable, Sequence __all__ = ["rearrange"] diff --git a/tools/autograd/context.py b/tools/autograd/context.py index 146cf571d304..0ed4b2ee4d01 100644 --- a/tools/autograd/context.py +++ b/tools/autograd/context.py @@ -1,5 +1,5 @@ import functools -from typing import Callable +from collections.abc import Callable from torchgen.api.autograd import NativeFunctionWithDifferentiabilityInfo as NFWDI from torchgen.context import native_function_manager diff --git a/tools/autograd/gen_python_functions.py b/tools/autograd/gen_python_functions.py index 5a003cadf6b3..af25d55ef38d 100644 --- a/tools/autograd/gen_python_functions.py +++ b/tools/autograd/gen_python_functions.py @@ -36,7 +36,7 @@ from __future__ import annotations import itertools import re from collections import defaultdict -from typing import Callable, TYPE_CHECKING +from typing import TYPE_CHECKING import yaml @@ -77,7 +77,7 @@ from .gen_trace_type import should_trace if TYPE_CHECKING: - from collections.abc import Iterable, Sequence + from collections.abc import Callable, Iterable, Sequence # diff --git a/tools/autograd/gen_variable_type.py b/tools/autograd/gen_variable_type.py index ed5a6e6cf398..5ce3b06af145 100644 --- a/tools/autograd/gen_variable_type.py +++ b/tools/autograd/gen_variable_type.py @@ -29,7 +29,7 @@ from __future__ import annotations import re -from typing import Callable, TYPE_CHECKING +from typing import TYPE_CHECKING from torchgen.api import cpp from torchgen.api.autograd import ( @@ -106,7 +106,7 @@ from .gen_trace_type import ( if TYPE_CHECKING: - from collections.abc import Sequence + from collections.abc import Callable, Sequence # We don't set or modify grad_fn on these methods. Generally, they return diff --git a/tools/flight_recorder/components/fr_logger.py b/tools/flight_recorder/components/fr_logger.py index 9574df97437b..49d878bf4559 100644 --- a/tools/flight_recorder/components/fr_logger.py +++ b/tools/flight_recorder/components/fr_logger.py @@ -5,7 +5,8 @@ # LICENSE file in the root directory of this source tree. import logging -from typing import Any, Callable, Optional +from collections.abc import Callable +from typing import Any, Optional class FlightRecorderLogger: diff --git a/tools/github/github_utils.py b/tools/github/github_utils.py index 6442a0644282..dc078fe29fad 100644 --- a/tools/github/github_utils.py +++ b/tools/github/github_utils.py @@ -4,12 +4,16 @@ from __future__ import annotations import json import os -from typing import Any, Callable, cast +from typing import Any, cast, TYPE_CHECKING from urllib.error import HTTPError from urllib.parse import quote from urllib.request import Request, urlopen +if TYPE_CHECKING: + from collections.abc import Callable + + def gh_fetch_url_and_headers( url: str, *, diff --git a/tools/linter/adapters/docstring_linter.py b/tools/linter/adapters/docstring_linter.py index 477bfe7d9a80..ce891bedcf99 100644 --- a/tools/linter/adapters/docstring_linter.py +++ b/tools/linter/adapters/docstring_linter.py @@ -5,7 +5,7 @@ import json import sys from functools import cached_property from pathlib import Path -from typing import Any, Callable, TYPE_CHECKING +from typing import Any, TYPE_CHECKING _FILE = Path(__file__).absolute() @@ -18,7 +18,7 @@ else: import _linter if TYPE_CHECKING: - from collections.abc import Iterator, Sequence + from collections.abc import Callable, Iterator, Sequence GRANDFATHER_LIST = _FILE.parent / "docstring_linter-grandfather.json" diff --git a/tools/linter/adapters/no_workflows_on_fork.py b/tools/linter/adapters/no_workflows_on_fork.py index 81e11a47f67b..02efd5f6f62a 100644 --- a/tools/linter/adapters/no_workflows_on_fork.py +++ b/tools/linter/adapters/no_workflows_on_fork.py @@ -22,11 +22,15 @@ import os import re from enum import Enum from pathlib import Path -from typing import Any, Callable, NamedTuple, Optional +from typing import Any, NamedTuple, Optional, TYPE_CHECKING from yaml import load +if TYPE_CHECKING: + from collections.abc import Callable + + # Safely load fast C Yaml loader/dumper if they are available try: from yaml import CSafeLoader as Loader diff --git a/tools/nightly.py b/tools/nightly.py index 6361d7da67ce..ab60c71ae9b7 100755 --- a/tools/nightly.py +++ b/tools/nightly.py @@ -65,10 +65,11 @@ import textwrap import time import uuid from ast import literal_eval +from collections.abc import Callable from datetime import datetime from pathlib import Path from platform import system as platform_system -from typing import Any, Callable, cast, NamedTuple, TYPE_CHECKING, TypeVar +from typing import Any, cast, NamedTuple, TYPE_CHECKING, TypeVar if TYPE_CHECKING: diff --git a/tools/stats/import_test_stats.py b/tools/stats/import_test_stats.py index 8fb6be57e97d..a7c661340d13 100644 --- a/tools/stats/import_test_stats.py +++ b/tools/stats/import_test_stats.py @@ -7,10 +7,14 @@ import json import os import shutil from pathlib import Path -from typing import Any, Callable, cast +from typing import Any, cast, TYPE_CHECKING from urllib.request import urlopen +if TYPE_CHECKING: + from collections.abc import Callable + + REPO_ROOT = Path(__file__).resolve().parents[2] diff --git a/tools/stats/upload_external_contrib_stats.py b/tools/stats/upload_external_contrib_stats.py index 6de0e4952143..ab31cf645cd5 100644 --- a/tools/stats/upload_external_contrib_stats.py +++ b/tools/stats/upload_external_contrib_stats.py @@ -6,13 +6,17 @@ import json import os import time import urllib.parse -from typing import Any, Callable, cast +from typing import Any, cast, TYPE_CHECKING from urllib.error import HTTPError from urllib.request import Request, urlopen from tools.stats.upload_stats_lib import upload_to_s3 +if TYPE_CHECKING: + from collections.abc import Callable + + FILTER_OUT_USERS = { "pytorchmergebot", "facebook-github-bot", diff --git a/tools/stats/upload_stats_lib.py b/tools/stats/upload_stats_lib.py index 3ef60171acf6..34548b80d76b 100644 --- a/tools/stats/upload_stats_lib.py +++ b/tools/stats/upload_stats_lib.py @@ -9,12 +9,16 @@ import time import zipfile from functools import lru_cache from pathlib import Path -from typing import Any, Callable, cast, Optional +from typing import Any, cast, Optional, TYPE_CHECKING import boto3 # type: ignore[import] import requests +if TYPE_CHECKING: + from collections.abc import Callable + + PYTORCH_REPO = "https://api.github.com/repos/pytorch/pytorch" diff --git a/tools/testing/target_determination/heuristics/filepath.py b/tools/testing/target_determination/heuristics/filepath.py index e9bdd920b4ce..9cd4ccd862a4 100644 --- a/tools/testing/target_determination/heuristics/filepath.py +++ b/tools/testing/target_determination/heuristics/filepath.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections import defaultdict from functools import lru_cache from pathlib import Path -from typing import Any, Callable +from typing import Any, TYPE_CHECKING from warnings import warn from tools.testing.target_determination.heuristics.interface import ( @@ -17,6 +17,10 @@ from tools.testing.target_determination.heuristics.utils import ( from tools.testing.test_run import TestRun +if TYPE_CHECKING: + from collections.abc import Callable + + REPO_ROOT = Path(__file__).parents[3] keyword_synonyms: dict[str, list[str]] = { diff --git a/tools/testing/test_selections.py b/tools/testing/test_selections.py index 9493e35f97d7..4a5fbb6a836b 100644 --- a/tools/testing/test_selections.py +++ b/tools/testing/test_selections.py @@ -4,7 +4,7 @@ import math import os import subprocess from pathlib import Path -from typing import Callable, TYPE_CHECKING +from typing import TYPE_CHECKING from tools.stats.import_test_stats import get_disabled_tests from tools.testing.test_run import ShardedTest, TestRun @@ -19,7 +19,7 @@ except ImportError: if TYPE_CHECKING: - from collections.abc import Sequence + from collections.abc import Callable, Sequence REPO_ROOT = Path(__file__).resolve().parents[2]