Skip to content

Commit 69e0bda

Browse files
XuehaiPanpytorchmergebot
authored andcommitted
[BE] Import Literal, Protocol, and Final from standard library typing as of Python 3.8+ (pytorch#94490)
Changes: 1. `typing_extensions -> typing-extentions` in dependency. Use dash rather than underline to fit the [PEP 503: Normalized Names](https://peps.python.org/pep-0503/#normalized-names) convention. ```python import re def normalize(name): return re.sub(r"[-_.]+", "-", name).lower() ``` 2. Import `Literal`, `Protocal`, and `Final` from standard library as of Python 3.8+ 3. Replace `Union[Literal[XXX], Literal[YYY]]` to `Literal[XXX, YYY]`. Pull Request resolved: pytorch#94490 Approved by: https://github.com/ezyang, https://github.com/albanD
1 parent 527b646 commit 69e0bda

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+76
-102
lines changed

.ci/docker/common/install_conda.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ if [ -n "$ANACONDA_PYTHON_VERSION" ]; then
9090
# Install llvm-8 as it is required to compile llvmlite-0.30.0 from source
9191
conda_install numpy=1.18.5 ${CONDA_COMMON_DEPS} llvmdev=8.0.0
9292
else
93-
# Install `typing_extensions` for 3.7
94-
conda_install numpy=1.18.5 ${CONDA_COMMON_DEPS} typing_extensions
93+
# Install `typing-extensions` for 3.7
94+
conda_install numpy=1.18.5 ${CONDA_COMMON_DEPS} typing-extensions
9595
fi
9696

9797
# Use conda cmake in some cases. Conda cmake will be newer than our supported

.circleci/config.yml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.circleci/scripts/binary_ios_build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export PATH="~/anaconda/bin:${PATH}"
1515
source ~/anaconda/bin/activate
1616

1717
# Install dependencies
18-
conda install numpy ninja pyyaml mkl mkl-include setuptools cmake requests typing_extensions --yes
18+
conda install numpy ninja pyyaml mkl mkl-include setuptools cmake requests typing-extensions --yes
1919
conda install -c conda-forge valgrind --yes
2020
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
2121

.circleci/verbatim-sources/job-specs/job-specs-custom.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@
562562
$* || (sleep 1 && $*) || (sleep 2 && $*) || (sleep 4 && $*) || (sleep 8 && $*)
563563
}
564564
565-
retry conda install numpy ninja pyyaml mkl mkl-include setuptools cmake requests typing_extensions --yes
565+
retry conda install numpy ninja pyyaml mkl mkl-include setuptools cmake requests typing-extensions --yes
566566
567567
# sync submodules
568568
cd ${PROJ_ROOT}

.github/requirements/conda-env-Linux-X64

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ numpy=1.23.3
66
pyyaml=6.0
77
requests=2.28.1
88
setuptools=65.5.0
9-
typing_extensions=4.3.0
9+
typing-extensions=4.3.0

.github/requirements/conda-env-iOS

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ numpy=1.23.3
77
pyyaml=6.0
88
requests=2.28.1
99
setuptools=63.4.1
10-
typing_extensions=4.3.0
10+
typing-extensions=4.3.0

.github/requirements/conda-env-macOS-ARM64

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ numpy=1.22.3
22
pyyaml=6.0
33
setuptools=61.2.0
44
cmake=3.22.*
5-
typing_extensions=4.3.0
5+
typing-extensions=4.3.0
66
dataclasses=0.8
77
pip=22.2.2
88
six=1.16.0

.github/requirements/conda-env-macOS-X64

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ numpy=1.18.5
44
pyyaml=5.3
55
setuptools=46.0.0
66
cmake=3.22.*
7-
typing_extensions=4.3.0
7+
typing-extensions=4.3.0
88
dataclasses=0.8
99
pip=22.2.2
1010
six=1.16.0
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
typing_extensions
1+
typing-extensions
22
jinja2

.github/scripts/generate_ci_workflows.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
from dataclasses import asdict, dataclass, field
44
from pathlib import Path
5-
from typing import Dict, Set, List, Iterable
5+
from typing import Dict, Set, List, Literal, Iterable
66

77
import jinja2
88

99
import os
1010
import sys
11-
from typing_extensions import Literal, TypedDict
11+
from typing_extensions import TypedDict # Python 3.11+
1212

1313
import generate_binary_build_matrix # type: ignore[import]
1414

.github/workflows/run_torchbench.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
. "${SETUP_SCRIPT}"
4141
conda activate pr-ci
4242
conda install -y numpy="${NUMPY_VERSION}" requests ninja pyyaml mkl mkl-include \
43-
setuptools cmake=3.22.* typing_extensions boto3 \
43+
setuptools cmake=3.22.* typing-extensions boto3 \
4444
six pillow pytest tabulate gitpython git-lfs tqdm psutil
4545
pip install --pre torch torchvision torchtext -f https://download.pytorch.org/whl/nightly/cu116/torch_nightly.html
4646
- name: Setup TorchBench branch

benchmarks/dynamo/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pull-deps: clone-deps
1818
(cd ../../../torchvision && git pull && git submodule update --init --recursive)
1919
(cd ../../../torchdata && git pull && git submodule update --init --recursive)
2020
(cd ../../../torchtext && git pull && git submodule update --init --recursive)
21-
(cd ../../../torchaudio && git pull && git submodule update --init --recursive)
21+
(cd ../../../torchaudio && git pull && git submodule update --init --recursive)
2222
(cd ../../../detectron2 && git pull && git submodule update --init --recursive)
2323
(cd ../../../torchbenchmark && git pull && git submodule update --init --recursive)
2424
(cd ../../../triton && git fetch && git checkout $(TRITON_VERSION) && git submodule update --init --recursive)
@@ -28,7 +28,7 @@ build-deps: clone-deps
2828
# conda create --name torchdynamo -y python=3.8
2929
# conda activate torchdynamo
3030
conda install -y astunparse numpy scipy ninja pyyaml mkl mkl-include setuptools cmake \
31-
typing_extensions six requests protobuf numba cython scikit-learn
31+
typing-extensions six requests protobuf numba cython scikit-learn
3232
conda install -y -c pytorch magma-cuda116
3333
conda install -y -c conda-forge librosa
3434
(cd ../../../torchvision && python setup.py clean && python setup.py develop)

docs/source/jit.rst

+1-6
Original file line numberDiff line numberDiff line change
@@ -831,12 +831,7 @@ New API:
831831

832832
::
833833

834-
try:
835-
from typing_extensions import Final
836-
except:
837-
# If you don't have `typing_extensions` installed, you can use a
838-
# polyfill from `torch.jit`.
839-
from torch.jit import Final
834+
from typing import Final
840835

841836
class MyModule(torch.nn.Module):
842837

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires = [
88
"pyyaml",
99
"setuptools",
1010
"cmake",
11-
"typing_extensions",
11+
"typing-extensions",
1212
"six",
1313
"requests",
1414
]

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pyyaml
88
requests
99
setuptools
1010
types-dataclasses
11-
typing_extensions
11+
typing-extensions
1212
sympy
1313
filelock
1414
networkx

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ def main():
10141014
# the list of runtime dependencies required by this built package
10151015
install_requires = [
10161016
'filelock',
1017-
'typing_extensions',
1017+
'typing-extensions',
10181018
'sympy',
10191019
'networkx',
10201020
]

tools/extract_scripts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Any, Dict, Optional
88

99
import yaml
10-
from typing_extensions import TypedDict
10+
from typing_extensions import TypedDict # Python 3.11+
1111

1212
Step = Dict[str, Any]
1313

tools/fast_nvcc/fast_nvcc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import time
1717
from typing import Awaitable, cast, DefaultDict, Dict, List, Match, Optional, Set
1818

19-
from typing_extensions import TypedDict
19+
from typing_extensions import TypedDict # Python 3.11+
2020

2121
help_msg = """fast_nvcc [OPTION]... -- [NVCC_ARG]...
2222

tools/jit/gen_unboxing.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pathlib
55
import sys
66
from dataclasses import dataclass
7-
from typing import List, Sequence, Union
7+
from typing import List, Literal, Sequence, Union
88

99
import yaml
1010

@@ -17,13 +17,12 @@
1717
from torchgen.model import Argument, NativeFunction, NativeFunctionsGroup, Variant
1818
from torchgen.selective_build.selector import SelectiveBuilder
1919
from torchgen.utils import FileManager, make_file_manager, mapMaybe, Target
20-
from typing_extensions import Literal
2120

2221

2322
# Generates UnboxingFunctions.h & UnboxingFunctions.cpp.
2423
@dataclass(frozen=True)
2524
class ComputeUnboxingFunctions:
26-
target: Union[Literal[Target.DECLARATION], Literal[Target.DEFINITION]]
25+
target: Literal[Target.DECLARATION, Target.DEFINITION]
2726
selector: SelectiveBuilder
2827

2928
@method_with_native_function

tools/onnx/sarif/gen_sarif.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ python -m jschema_to_python \
3333
-vv
3434

3535
# Generate SARIF version file
36-
echo "from typing_extensions import Final" > "${ROOT}/${SARIF_DIR}/version.py"
36+
echo "from typing import Final" > "${ROOT}/${SARIF_DIR}/version.py"
3737
echo "SARIF_VERSION: Final = \"${SARIF_VERSION}\"" >> "${ROOT}/${SARIF_DIR}/version.py"
3838
echo "SARIF_SCHEMA_LINK: Final = \"${SARIF_SCHEMA_LINK}\"" >> "${ROOT}/${SARIF_DIR}/version.py"
3939

torch/_C/_VariableFunctions.pyi.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# ${generated_comment}
22

33
from torch import Tensor, Generator, strided, memory_format, contiguous_format, strided
4-
from typing import List, Tuple, Optional, Union, Any, ContextManager, Callable, overload, Iterator, NamedTuple, Sequence, TypeVar
5-
from typing_extensions import Literal
4+
from typing import List, Tuple, Optional, Union, Any, ContextManager, Callable, overload, Iterator, NamedTuple, Sequence, Literal, TypeVar
65
from torch._six import inf
76

87
from torch.types import _int, _float, _bool, Number, _dtype, _device, _qscheme, _size, _layout, SymInt, Device

torch/_C/__init__.pyi.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ from pathlib import Path
99
from typing import (
1010
Any, BinaryIO, Callable, ContextManager, Dict, Iterable, Iterator, List,
1111
NamedTuple, Optional, overload, Sequence, Tuple, TypeVar, Type, Union,
12-
Generic, Set, AnyStr)
13-
from typing_extensions import Literal
12+
Literal, Generic, Set, AnyStr)
1413
from torch._six import inf
1514

1615
from torch.types import (

torch/_C/_profiler.pyi

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from enum import Enum
2-
from typing import List, Optional, Tuple, Union
2+
from typing import List, Literal, Optional, Tuple, Union
33

44
from torch._C import device, dtype, layout
55

6-
from typing_extensions import Literal
7-
86
# defined in torch/csrc/profiler/python/init.cpp
97

108
class RecordScope(Enum):

torch/_C/return_types.pyi.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# ${generated_comment}
22

33
from torch import Tensor, Generator, strided, memory_format, contiguous_format, strided
4-
from typing import List, Tuple, Optional, Union, Any, ContextManager, Callable, overload, Iterator, NamedTuple, Sequence, TypeVar
5-
from typing_extensions import Literal
4+
from typing import List, Tuple, Optional, Union, Any, ContextManager, Callable, overload, Iterator, NamedTuple, Sequence, Literal, TypeVar
65
from torch._six import inf
76

87
from torch.types import _int, _float, _bool, Number, _dtype, _device, _qscheme, _size, _layout

torch/_dynamo/backends/registry.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import functools
2-
from typing import Callable, Dict, List, Optional, Sequence, Tuple
3-
4-
from typing_extensions import Protocol
2+
from typing import Callable, Dict, List, Optional, Protocol, Sequence, Tuple
53

64
import torch
75
from torch import fx

torch/_dynamo/types.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import dataclasses
22
import sys
33
import types
4-
from typing import Callable, Dict, List, NamedTuple, Optional, OrderedDict, Union
4+
from typing import (
5+
Callable,
6+
Dict,
7+
List,
8+
NamedTuple,
9+
Optional,
10+
OrderedDict,
11+
Protocol,
12+
Union,
13+
)
514

6-
from typing_extensions import Protocol
715

816
if sys.version_info >= (3, 11):
917
from torch._C._dynamo import eval_frame

torch/_refs/fft.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import math
22

3-
from typing import Iterable, List, NamedTuple, Optional, Sequence, Tuple, Union
4-
5-
from typing_extensions import Literal
3+
from typing import Iterable, List, Literal, NamedTuple, Optional, Sequence, Tuple, Union
64

75
import torch
86
import torch._prims as prims

torch/distributed/pipeline/sync/checkpoint.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
from contextlib import contextmanager
2929
import threading
3030
from typing import (
31-
TYPE_CHECKING,
3231
Any,
3332
Deque,
3433
Generator,
3534
List,
3635
Optional,
36+
Protocol,
3737
Union,
3838
Sequence,
3939
Tuple
@@ -60,12 +60,6 @@
6060
RNGStates = Tuple[Tensor, Optional[Tensor]] # (cpu_rng_state, gpu_rng_state)
6161

6262

63-
if TYPE_CHECKING:
64-
from typing_extensions import Protocol
65-
else:
66-
Protocol = object
67-
68-
6963
# Protocol with __call__ instead of Callable can be used as an attribute type.
7064
# See: https://github.com/python/mypy/issues/708#issuecomment-561735949
7165
class Function(Protocol):

torch/nn/modules/lazy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import itertools
2-
from typing_extensions import Protocol
32
import warnings
3+
from typing import Protocol
44

55
import torch
66
from ..parameter import is_lazy

torch/onnx/_internal/diagnostics/infra/sarif/_artifact.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from __future__ import annotations
55

66
import dataclasses
7-
from typing import Any, List, Optional
8-
9-
from typing_extensions import Literal
7+
from typing import Any, List, Literal, Optional
108

119
from torch.onnx._internal.diagnostics.infra.sarif import (
1210
_artifact_content,

torch/onnx/_internal/diagnostics/infra/sarif/_external_properties.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from __future__ import annotations
55

66
import dataclasses
7-
from typing import List, Optional
8-
9-
from typing_extensions import Literal
7+
from typing import List, Literal, Optional
108

119
from torch.onnx._internal.diagnostics.infra.sarif import (
1210
_address,

torch/onnx/_internal/diagnostics/infra/sarif/_notification.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from __future__ import annotations
55

66
import dataclasses
7-
from typing import List, Optional
8-
9-
from typing_extensions import Literal
7+
from typing import List, Literal, Optional
108

119
from torch.onnx._internal.diagnostics.infra.sarif import (
1210
_exception,

torch/onnx/_internal/diagnostics/infra/sarif/_reporting_configuration.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from __future__ import annotations
55

66
import dataclasses
7-
from typing import Optional
8-
9-
from typing_extensions import Literal
7+
from typing import Literal, Optional
108

119
from torch.onnx._internal.diagnostics.infra.sarif import _property_bag
1210

torch/onnx/_internal/diagnostics/infra/sarif/_result.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from __future__ import annotations
55

66
import dataclasses
7-
from typing import Any, List, Optional
8-
9-
from typing_extensions import Literal
7+
from typing import Any, List, Literal, Optional
108

119
from torch.onnx._internal.diagnostics.infra.sarif import (
1210
_artifact_location,

torch/onnx/_internal/diagnostics/infra/sarif/_run.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from __future__ import annotations
55

66
import dataclasses
7-
from typing import Any, List, Optional
8-
9-
from typing_extensions import Literal
7+
from typing import Any, List, Literal, Optional
108

119
from torch.onnx._internal.diagnostics.infra.sarif import (
1210
_address,

torch/onnx/_internal/diagnostics/infra/sarif/_sarif_log.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from __future__ import annotations
55

66
import dataclasses
7-
from typing import List, Optional
8-
9-
from typing_extensions import Literal
7+
from typing import List, Literal, Optional
108

119
from torch.onnx._internal.diagnostics.infra.sarif import (
1210
_external_properties,

0 commit comments

Comments
 (0)