mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
[Code Clean] Remove deadcodes about Python3.9 [6/N] (#163645)
As the title stated. Pull Request resolved: https://github.com/pytorch/pytorch/pull/163645 Approved by: https://github.com/albanD ghstack dependencies: #163626, #163627, #163629, #163643, #163644
This commit is contained in:
@ -25,9 +25,9 @@ __all__ = [
|
||||
"dropwhile",
|
||||
"filterfalse",
|
||||
"islice",
|
||||
"pairwise",
|
||||
"tee",
|
||||
"zip_longest",
|
||||
"pairwise",
|
||||
]
|
||||
|
||||
|
||||
|
@ -28,7 +28,6 @@ import itertools
|
||||
import logging
|
||||
import math
|
||||
import operator
|
||||
import sys
|
||||
import types
|
||||
import typing
|
||||
import unittest
|
||||
@ -2115,9 +2114,7 @@ class BuiltinVariable(VariableTracker):
|
||||
getattr(isinstance_type, "__instancecheck__", None)
|
||||
):
|
||||
isinstance_type_tuple = (isinstance_type,)
|
||||
elif sys.version_info >= (3, 10) and isinstance(
|
||||
isinstance_type, types.UnionType
|
||||
):
|
||||
elif isinstance(isinstance_type, types.UnionType):
|
||||
isinstance_type_tuple = isinstance_type.__args__
|
||||
elif isinstance(isinstance_type, tuple) and all(
|
||||
isinstance(tp, type) or callable(getattr(tp, "__instancecheck__", None))
|
||||
|
@ -13,7 +13,6 @@ collection support for PyTorch APIs.
|
||||
"""
|
||||
|
||||
import functools
|
||||
import sys
|
||||
import types
|
||||
from collections.abc import Iterable
|
||||
from typing import Any, Callable, Optional, overload, TypeVar, Union
|
||||
@ -628,10 +627,7 @@ def map_only(
|
||||
|
||||
You can also directly use 'tree_map_only'
|
||||
"""
|
||||
if isinstance(type_or_types_or_pred, (type, tuple)) or (
|
||||
sys.version_info >= (3, 10)
|
||||
and isinstance(type_or_types_or_pred, types.UnionType)
|
||||
):
|
||||
if isinstance(type_or_types_or_pred, (type, tuple, types.UnionType)):
|
||||
|
||||
def pred(x: Any) -> bool:
|
||||
return isinstance(x, type_or_types_or_pred) # type: ignore[arg-type]
|
||||
|
@ -20,7 +20,6 @@ import functools
|
||||
import importlib
|
||||
import importlib.metadata
|
||||
import json
|
||||
import sys
|
||||
import threading
|
||||
import types
|
||||
import warnings
|
||||
@ -1470,10 +1469,7 @@ def map_only(
|
||||
|
||||
You can also directly use 'tree_map_only'
|
||||
"""
|
||||
if isinstance(type_or_types_or_pred, (type, tuple)) or (
|
||||
sys.version_info >= (3, 10)
|
||||
and isinstance(type_or_types_or_pred, types.UnionType)
|
||||
):
|
||||
if isinstance(type_or_types_or_pred, (type, tuple, types.UnionType)):
|
||||
|
||||
def pred(x: Any) -> bool:
|
||||
return isinstance(x, type_or_types_or_pred) # type: ignore[arg-type]
|
||||
|
@ -7,9 +7,10 @@ import os
|
||||
import re
|
||||
import sys
|
||||
import textwrap
|
||||
from dataclasses import fields, is_dataclass
|
||||
from dataclasses import is_dataclass
|
||||
from enum import auto, Enum
|
||||
from pathlib import Path
|
||||
from pprint import pformat
|
||||
from typing import Any, Callable, Generic, Literal, NoReturn, TYPE_CHECKING, TypeVar
|
||||
from typing_extensions import assert_never, deprecated, Self
|
||||
|
||||
@ -354,47 +355,9 @@ def dataclass_repr(
|
||||
indent: int = 0,
|
||||
width: int = 80,
|
||||
) -> str:
|
||||
# built-in pprint module support dataclasses from python 3.10
|
||||
from pprint import pformat
|
||||
|
||||
return pformat(obj, indent, width)
|
||||
|
||||
|
||||
def _pformat(
|
||||
obj: Any,
|
||||
indent: int,
|
||||
width: int,
|
||||
curr_indent: int = 0,
|
||||
) -> str:
|
||||
assert is_dataclass(obj), f"obj should be a dataclass, received: {type(obj)}"
|
||||
|
||||
class_name = obj.__class__.__name__
|
||||
# update current indentation level with class name
|
||||
curr_indent += len(class_name) + 1
|
||||
|
||||
fields_list = [(f.name, getattr(obj, f.name)) for f in fields(obj) if f.repr]
|
||||
|
||||
fields_str = []
|
||||
for name, attr in fields_list:
|
||||
# update the current indent level with the field name
|
||||
# dict, list, set and tuple also add indent as done in pprint
|
||||
_curr_indent = curr_indent + len(name) + 1
|
||||
if is_dataclass(attr):
|
||||
str_repr = _pformat(attr, indent, width, _curr_indent)
|
||||
elif isinstance(attr, dict):
|
||||
str_repr = _format_dict(attr, indent, width, _curr_indent)
|
||||
elif isinstance(attr, (list, set, tuple)):
|
||||
str_repr = _format_list(attr, indent, width, _curr_indent)
|
||||
else:
|
||||
str_repr = repr(attr)
|
||||
|
||||
fields_str.append(f"{name}={str_repr}")
|
||||
|
||||
indent_str = curr_indent * " "
|
||||
body = f",\n{indent_str}".join(fields_str)
|
||||
return f"{class_name}({body})"
|
||||
|
||||
|
||||
def _format_dict(
|
||||
attr: dict[Any, Any],
|
||||
indent: int,
|
||||
@ -406,7 +369,7 @@ def _format_dict(
|
||||
for k, v in attr.items():
|
||||
k_repr = repr(k)
|
||||
v_str = (
|
||||
_pformat(v, indent, width, curr_indent + len(k_repr))
|
||||
pformat(v, indent, width, curr_indent + len(k_repr))
|
||||
if is_dataclass(v)
|
||||
else repr(v)
|
||||
)
|
||||
@ -423,7 +386,7 @@ def _format_list(
|
||||
) -> str:
|
||||
curr_indent += indent + 1
|
||||
list_repr = [
|
||||
_pformat(l, indent, width, curr_indent) if is_dataclass(l) else repr(l)
|
||||
pformat(l, indent, width, curr_indent) if is_dataclass(l) else repr(l)
|
||||
for l in attr
|
||||
]
|
||||
start, end = ("[", "]") if isinstance(attr, list) else ("(", ")")
|
||||
|
Reference in New Issue
Block a user