[jit] Delete polyfill typing (#27510)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/27510
We could delete polyfill typing bc requirements.txt require user to
install typing as a dependency whether in py2 or py3, so those typing
actually not getting used either ways.
Test Plan: Imported from OSS
Differential Revision: D20673393
fbshipit-source-id: ea5276824c6e275c1f991f8c12329040b0058d2b
diff --git a/torch/_jit_internal.py b/torch/_jit_internal.py
index 2ea73f0..a0176d6 100644
--- a/torch/_jit_internal.py
+++ b/torch/_jit_internal.py
@@ -10,6 +10,7 @@
import torch
from torch._six import builtins
from torch._utils_internal import get_source_lines_and_file
+from typing import Tuple, List, Dict, Optional, Union, Any, TypeVar, Generic # noqa: F401
# Wrapper functions that can call either of 2 functions depending on a boolean
# argument
@@ -556,124 +557,74 @@
raise Exception("Overloads are not useable when a module is redeclared within the same file: " + str(method))
return overloads
-try:
- import typing
- from typing import Tuple, List, Dict, Optional, Any
- def is_tuple(ann):
- # For some reason Python 3.7 violates the Type[A, B].__origin__ == Type rule
- if not hasattr(ann, '__module__'):
+def is_tuple(ann):
+ # For some reason Python 3.7 violates the Type[A, B].__origin__ == Type rule
+ if not hasattr(ann, '__module__'):
+ return False
+ return ann.__module__ == 'typing' and \
+ (getattr(ann, '__origin__', None) is Tuple or
+ getattr(ann, '__origin__', None) is tuple)
+
+def is_list(ann):
+ if not hasattr(ann, '__module__'):
+ return False
+ return ann.__module__ == 'typing' and \
+ (getattr(ann, '__origin__', None) is List or
+ getattr(ann, '__origin__', None) is list)
+
+def is_dict(ann):
+ if not hasattr(ann, '__module__'):
+ return False
+ return ann.__module__ == 'typing' and \
+ (getattr(ann, '__origin__', None) is Dict or
+ getattr(ann, '__origin__', None) is dict)
+
+def is_optional(ann):
+ # Optional[T] is just shorthand for Union[T, None], so check for both
+ def safe_is_subclass(the_type, super_type):
+ # Don't throw if `the_type` isn't a class type (e.g. if it is
+ # another type annotation instance)
+ if not inspect.isclass(the_type):
return False
- return ann.__module__ == 'typing' and \
- (getattr(ann, '__origin__', None) is typing.Tuple or
- getattr(ann, '__origin__', None) is tuple)
+ return issubclass(the_type, super_type)
- def is_list(ann):
- if not hasattr(ann, '__module__'):
- return False
- return ann.__module__ == 'typing' and \
- (getattr(ann, '__origin__', None) is typing.List or
- getattr(ann, '__origin__', None) is list)
+ if not hasattr(ann, '__module__'):
+ return False
- def is_dict(ann):
- if not hasattr(ann, '__module__'):
- return False
- return ann.__module__ == 'typing' and \
- (getattr(ann, '__origin__', None) is typing.Dict or
- getattr(ann, '__origin__', None) is dict)
+ union_optional = False
+ if ann.__module__ == 'typing' and \
+ (getattr(ann, '__origin__', None) is Union):
+ args = getattr(ann, '__args__', ())
+ if len(args) == 2:
+ union_optional = (safe_is_subclass(args[1], type(None)) and not safe_is_subclass(args[0], type(None))) \
+ or (safe_is_subclass(args[0], type(None)) and not safe_is_subclass(args[1], type(None)))
- def is_optional(ann):
- # Optional[T] is just shorthand for Union[T, None], so check for both
- def safe_is_subclass(the_type, super_type):
- # Don't throw if `the_type` isn't a class type (e.g. if it is
- # another type annotation instance)
- if not inspect.isclass(the_type):
- return False
- return issubclass(the_type, super_type)
+ optional = ann.__module__ == 'typing' and \
+ (getattr(ann, '__origin__', None) is Optional)
- if not hasattr(ann, '__module__'):
- return False
+ return optional or union_optional
- union_optional = False
- if ann.__module__ == 'typing' and \
- (getattr(ann, '__origin__', None) is typing.Union):
- args = getattr(ann, '__args__', ())
- if len(args) == 2:
- union_optional = (safe_is_subclass(args[1], type(None)) and not safe_is_subclass(args[0], type(None))) \
- or (safe_is_subclass(args[0], type(None)) and not safe_is_subclass(args[1], type(None)))
+# fake Python container type for Future/RRef
+T = TypeVar('T')
- optional = ann.__module__ == 'typing' and \
- (getattr(ann, '__origin__', None) is typing.Optional)
+class Future(Generic[T]):
+ __slots__ = ['__args__']
- return optional or union_optional
+ def __init__(self, types):
+ self.__args__ = types
-except ImportError:
- # A minimal polyfill for versions of Python that don't have typing.
- # Note that this means that they also don't support the fancy annotation syntax, so
- # those instances will only be used in our tiny `type: ` comment interpreter.
+class RRef(Generic[T]):
+ __slots__ = ['__args__']
- # The __getitem__ in typing is implemented using metaclasses, but I'm too lazy for that.
- class TupleCls(object):
- def __getitem__(self, types):
- return TupleInstance(types)
+ def __init__(self, types):
+ self.__args__ = types
- class TupleInstance(object):
- __slots__ = ['__args__']
+def is_future(ann):
+ return getattr(ann, "__origin__", None) is Future
- def __init__(self, types):
- self.__args__ = types
-
- class ListInstance(object):
- __slots__ = ['__args__']
-
- def __init__(self, types):
- self.__args__ = types
-
- class ListCls(object):
- def __getitem__(self, types):
- return TupleInstance(types)
-
- class DictInstance(object):
- __slots__ = ['__args__']
-
- def __init__(self, types):
- self.__args__ = types
-
- class DictCls(object):
- def __getitem__(self, types):
- return DictInstance(types)
-
- class OptionalInstance(object):
- __slots__ = ['__args__']
-
- def __init__(self, types):
- self.__args__ = types
-
- class OptionalCls(object):
- def __getitem__(self, types):
- return OptionalInstance(types)
-
- class AnyCls(object):
- pass
-
- Tuple = TupleCls() # noqa: T484
- List = ListCls() # noqa: T484
- Dict = DictCls() # noqa: T484
- Optional = DictCls() # noqa: T484
- Any = AnyCls() # noqa: T484
-
- def is_tuple(ann):
- return isinstance(ann, TupleInstance)
-
- def is_list(ann):
- return isinstance(ann, ListInstance)
-
- def is_dict(ann):
- return isinstance(ann, DictInstance)
-
- def is_optional(ann):
- return isinstance(ann, OptionalInstance)
-
+def is_rref(ann):
+ return getattr(ann, "__origin__", None) is RRef
try:
import typing_extensions
@@ -699,62 +650,6 @@
def is_final(ann):
return isinstance(ann, FinalInstance)
-
-try:
- from typing import TypeVar, Generic
-
- T = TypeVar('T')
-
- class Future(Generic[T]):
- __slots__ = ['__args__']
-
- def __init__(self, types):
- self.__args__ = types
-
- class RRef(Generic[T]):
- __slots__ = ['__args__']
-
- def __init__(self, types):
- self.__args__ = types
-
- def is_future(ann):
- return getattr(ann, "__origin__", None) is Future
-
- def is_rref(ann):
- return getattr(ann, "__origin__", None) is RRef
-
-except ImportError:
- class FutureInstance(object):
- __slots__ = ['__args__']
-
- def __init__(self, types):
- self.__args__ = types
-
- class FutureCls(object):
- def __getitem__(self, types):
- return FutureInstance(types)
-
- Future = FutureCls() # noqa: T484
-
- def is_future(ann):
- return isinstance(ann, FutureInstance)
-
- class RRefInstance(object):
- __slots__ = ['__args__']
-
- def __init__(self, types):
- self.__args__ = types
-
- class RRefCls(object):
- def __getitem__(self, types):
- return RRefInstance(types)
-
- RRef = RRefCls() # noqa: T484
-
- def is_rref(ann):
- return isinstance(ann, RRefInstance)
-
-
# allows BroadcastingList instance to be subscriptable
class BroadcastingListCls(object):
def __getitem__(self, types):