From c0aea8e8aaac61c77cd3ac1baafda24d22c8e707 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Mon, 26 Oct 2015 17:35:14 -0700 Subject: [PATCH 1/2] Finish implementation of the types module Note that many classes will need special handling in the typing module. --- builtins/2.7/__builtin__.pyi | 2 + stdlib/2.7/types.pyi | 152 ++++++++++++++++++++++++++++++++--- stdlib/3/types.pyi | 109 ++++++++++++++++++++++--- 3 files changed, 237 insertions(+), 26 deletions(-) diff --git a/builtins/2.7/__builtin__.pyi b/builtins/2.7/__builtin__.pyi index 3beaeeaf5..b558a03bb 100644 --- a/builtins/2.7/__builtin__.pyi +++ b/builtins/2.7/__builtin__.pyi @@ -806,3 +806,5 @@ def eval(s: str) -> Any: ... def cmp(x: Any, y: Any) -> int: ... def execfile(filename: str, globals: Dict[str, Any] = None, locals: Dict[str, Any] = None) -> None: ... + +class file(BinaryIO): ... diff --git a/stdlib/2.7/types.pyi b/stdlib/2.7/types.pyi index a9b5b4475..3cd3cfbb8 100644 --- a/stdlib/2.7/types.pyi +++ b/stdlib/2.7/types.pyi @@ -1,23 +1,52 @@ # Stubs for types +# Note, all classes "defined" here require special handling. -from typing import Any, Tuple, Optional +from typing import ( + Any, Callable, Dict, Iterable, Iterator, List, Optional, + Tuple, TypeVar, Union, overload, +) -class ModuleType: - __name__ = ... # type: str - __file__ = ... # type: str - def __init__(self, name: str, doc: str) -> None: ... +_T = TypeVar('_T') -class TracebackType: - ... +class NoneType: ... +TypeType = type +ObjectType = object -class FrameType: - ... +IntType = int +LongType = long +FloatType = float +BooleanType = bool +ComplexType = complex +StringType = str +UnicodeType = unicode +StringTypes = (StringType, UnicodeType) +BufferType = buffer +TupleType = tuple +ListType = list +DictType = DictionaryType = dict -class GeneratorType: - ... +class _Cell: + cell_contents = ... # type: Any -class ListType: - ... +class FunctionType: + func_closure = ... # type: Optional[Tuple[_Cell, ...]] + func_code = ... # type: CodeType + func_defaults = ... # type: Optional[Tuple[Any, ...]] + func_dict = ... # type: Dict[str, Any] + func_doc = ... # type: Optional[str] + func_globals = ... # type: Dict[str, Any] + func_name = ... # type: str + __closure__ = func_closure + __code__ = func_code + __defaults__ = func_defaults + __dict__ = func_dict + __doc__ = func_doc + __globals__ = func_globals + __name__ = func_name + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... + def __get__(self, obj: Optional[object], type: Optional[type]) -> 'UnboundMethodType': ... + +LambdaType = FunctionType class CodeType: co_argcount = ... # type: int @@ -34,3 +63,100 @@ class CodeType: co_nlocals= ... # type: int co_stacksize= ... # type: int co_varnames = ... # type: Tuple[str, ...] + +class GeneratorType: + gi_code = ... # type: CodeType + gi_frame = ... # type: FrameType + gi_running = ... # type: int + def __iter__(self) -> 'GeneratorType': ... + def close(self) -> None: ... + def next(self) -> Any: ... + def send(self, arg: Any) -> Any: ... + @overload + def throw(self, val: BaseException) -> Any: ... + @overload + def throw(self, typ: type, val: BaseException = ..., tb: 'TracebackType' = ...) -> Any: ... + +class ClassType: ... +class UnboundMethodType: + im_class = ... # type: type + im_func = ... # type: FunctionType + im_self = ... # type: Optional[object] + __func__ = im_func + __self__ = im_self + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... +class InstanceType: ... +MethodType = UnboundMethodType + +class BuiltinFunctionType: + __self__ = ... # type: Optional[object] + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... +BuiltinMethodType = BuiltinFunctionType + +class ModuleType: + __doc__ = ... # type: Optional[str] + __file__ = ... # type: Optional[str] + __name__ = ... # type: str + __package__ = ... # type: Optional[str] + __path__ = ... # type: Optional[Iterable[str]] + def __init__(self, name: str, doc: str) -> None: ... +FileType = file +XRangeType = xrange + +class TracebackType: + tb_frame = ... # type: FrameType + tb_lasti = ... # type: int + tb_lineno = ... # type: int + tb_next = ... # type: TracebackType + +class FrameType: + f_back = ... # type: FrameType + f_builtins = ... # type: Dict[str, Any] + f_code = ... # type: CodeType + f_exc_type = ... # type: None + f_exc_value = ... # type: None + f_exc_traceback = ... # type: None + f_globals = ... # type: Dict[str, Any] + f_lasti = ... # type: int + f_lineno = ... # type: int + f_locals = ... # type: Dict[str, Any] + f_restricted = ... # type: bool + f_trace = ... # type: Callable[[], None] + + def clear(self) -> None: pass + +SliceType = slice +class EllipsisType: ... + +class DictProxyType: + # TODO is it possible to have non-string keys? + # no __init__ + def copy(self) -> dict: ... + def get(self, key: str, default: _T = None) -> Union[Any, _T]: ... + def has_key(self, key: str) -> bool: ... + def items(self) -> List[Tuple[str, Any]]: ... + def iteritems(self) -> Iterator[Tuple[str, Any]]: ... + def iterkeys(self) -> Iterator[str]: ... + def itervalues(self) -> Iterator[Any]: ... + def keys(self) -> List[str]: ... + def values(self) -> List[Any]: ... + def __contains__(self, key: str) -> bool: ... + def __getitem__(self, key: str) -> Any: ... + def __iter__(self) -> Iterator[str]: ... + def __len__(self) -> int: ... + +class NotImplementedType: ... + +class GetSetDescriptorType: + __name__ = ... # type: str + __objclass__ = ... # type: type + def __get__(self, obj: Any, type: type = None) -> Any: ... + def __set__(self, obj: Any) -> None: ... + def __delete__(self, obj: Any) -> None: ... +# Same type on Jython, different on CPython and PyPy, unknown on IronPython. +class MemberDescriptorType: + __name__ = ... # type: str + __objclass__ = ... # type: type + def __get__(self, obj: Any, type: type = None) -> Any: ... + def __set__(self, obj: Any) -> None: ... + def __delete__(self, obj: Any) -> None: ... diff --git a/stdlib/3/types.pyi b/stdlib/3/types.pyi index 1a8d96dbf..4c53d16e5 100644 --- a/stdlib/3/types.pyi +++ b/stdlib/3/types.pyi @@ -1,16 +1,26 @@ # Stubs for types +# Note, all classes "defined" here require special handling. -# TODO this is work in progress +# TODO parts of this should be conditional on version -from typing import Any, Callable, Dict, Sequence, Optional, Tuple +from typing import Any, Callable, Dict, Iterator, Optional, Tuple, TypeVar, Union, overload -class ModuleType: - __name__ = ... # type: str - __file__ = ... # type: str - def __init__(self, name: str, doc: Any) -> None: ... +_T = TypeVar('_T') -class MethodType: ... -class BuiltinMethodType: ... +class _Cell: + cell_contents = ... # type: Any + +class FunctionType: + __closure__ = ... # type: Optional[Tuple[_Cell, ...]] + __code__ = ... # type: CodeType + __defaults__ = ... # type: Optional[Tuple[Any, ...]] + __dict__ = ... # type: Dict[str, Any] + __doc__ = ... # type: Optional[str] + __globals__ = ... # type: Dict[str, Any] + __name__ = ... # type: str + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... + def __get__(self, obj: Optional[object], type: Optional[type]) -> 'MethodType': ... +LambdaType = FunctionType class CodeType: """Create a code object. Not for the faint of heart.""" @@ -47,6 +57,64 @@ class CodeType: cellvars: Tuple[str, ...] = (), ) -> None: ... +class MappingProxyType: + def copy(self) -> dict: ... + def get(self, key: str, default: _T = None) -> Union[Any, _T]: ... + def items(self) -> Iterator[Tuple[str, Any]]: ... + def keys(self) -> Iterator[str]: ... + def values(self) -> Iterator[Any]: ... + def __contains__(self, key: str) -> bool: ... + def __getitem__(self, key: str) -> Any: ... + def __iter__(self) -> Iterator[str]: ... + def __len__(self) -> int: ... +class SimpleNamespace: ... + +class GeneratorType: + gi_code = ... # type: CodeType + gi_frame = ... # type: FrameType + gi_running = ... # type: bool + gi_yieldfrom = ... # type: Optional[GeneratorType] + def __iter__(self) -> 'GeneratorType': ... + def __next__(self) -> Any: ... + def close(self) -> None: ... + def send(self, arg: Any) -> Any: ... + @overload + def throw(self, val: BaseException) -> Any: ... + @overload + def throw(self, typ: type, val: BaseException = ..., tb: 'TracebackType' = ...) -> Any: ... + +class CoroutineType: + cr_await = ... # type: Optional[Any] + cr_code = ... # type: CodeType + cr_frame = ... # type: FrameType + cr_running = ... # type: bool + def close(self) -> None: ... + def send(self, arg: Any) -> Any: ... + @overload + def throw(self, val: BaseException) -> Any: ... + @overload + def throw(self, typ: type, val: BaseException = ..., tb: 'TracebackType' = ...) -> Any: ... + +class MethodType: + __func__ = ... # type: FunctionType + __self__ = ... # type: object + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... +class BuiltinFunctionType: + __self__ = ... # type: Union[object, ModuleType] + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... +BuiltinMethodType = BuiltinFunctionType + +class ModuleType: + __name__ = ... # type: str + __file__ = ... # type: str + def __init__(self, name: str, doc: Any) -> None: ... + +class TracebackType: + tb_frame = ... # type: FrameType + tb_lasti = ... # type: int + tb_lineno = ... # type: int + tb_next = ... # type: TracebackType + class FrameType: f_back = ... # type: FrameType f_builtins = ... # type: Dict[str, Any] @@ -59,8 +127,23 @@ class FrameType: def clear(self) -> None: pass -class TracebackType: - tb_frame = ... # type: FrameType - tb_lasti = ... # type: int - tb_lineno = ... # type: int - tb_next = ... # type: TracebackType +class GetSetDescriptorType: + __name__ = ... # type: str + __objclass__ = ... # type: type + def __get__(self, obj: Any, type: type = None) -> Any: ... + def __set__(self, obj: Any) -> None: ... + def __delete__(self, obj: Any) -> None: ... +class MemberDescriptorType: + __name__ = ... # type: str + __objclass__ = ... # type: type + def __get__(self, obj: Any, type: type = None) -> Any: ... + def __set__(self, obj: Any) -> None: ... + def __delete__(self, obj: Any) -> None: ... + +def new_class(name: str, bases: Tuple[type, ...] = (), kwds: Dict[str, Any] = None, exec_body: Callable[[Dict[str, Any]], None] = None) -> type: ... +def prepare_class(name: str, bases: Tuple[type, ...] = (), kwds: Dict[str, Any] = None) -> Tuple[type, Dict[str, Any], Dict[str, Any]]: ... + +# Actually a different type, but `property` is special and we want that too. +DynamicClassAttribute = property + +def coroutine(f: Callable[..., Any]) -> CoroutineType: ... From 2e5489d0e694e1f7b260ab2d03924d5e87175aa9 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Tue, 27 Oct 2015 14:53:11 -0700 Subject: [PATCH 2/2] Add six, except six.moves --- third_party/2.7/six/__init__.pyi | 90 +++++++++++++++++++++++++++ third_party/3/six/__init__.pyi | 103 +++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 third_party/2.7/six/__init__.pyi create mode 100644 third_party/3/six/__init__.pyi diff --git a/third_party/2.7/six/__init__.pyi b/third_party/2.7/six/__init__.pyi new file mode 100644 index 000000000..60ba11759 --- /dev/null +++ b/third_party/2.7/six/__init__.pyi @@ -0,0 +1,90 @@ +# Stubs for six (Python 2.7) + +from __future__ import print_function + +from typing import ( + Any, AnyStr, Callable, Iterable, Mapping, Optional, + Pattern, Tuple, TypeVar, Union, overload, +) +import typing + +import unittest +import types + +_T = TypeVar('_T') +_K = TypeVar('_K') +_V = TypeVar('_V') + +# TODO make constant, then move this stub to 2and3 +# https://github.com/python/typeshed/issues/17 +PY2 = True +PY3 = False +PY34 = False + +string_types = basestring, +integer_types = (int, long) +class_types = (type, types.ClassType) +text_type = unicode +binary_type = str + +MAXSIZE = ... # type: int + +#def add_move +#def remove_move + +def advance_iterator(it: typing.Iterator[_T]) -> _T: ... +next = advance_iterator + +def callable(obj: object) -> bool: ... + +def get_unbound_function(unbound: types.MethodType) -> types.FunctionType: ... +def create_bound_method(func: types.FunctionType, obj: object) -> types.MethodType: ... +def create_unbound_method(func: types.FunctionType, cls: Union[type, types.ClassType]) -> types.MethodType: ... + +class Iterator: + def next(self) -> Any: ... + +def get_method_function(meth: types.MethodType) -> types.FunctionType: ... +def get_method_self(meth: types.MethodType) -> Optional[object]: ... +def get_function_closure(fun: types.FunctionType) -> Optional[Tuple[types._Cell, ...]]: ... +def get_function_code(fun: types.FunctionType) -> types.CodeType: ... +def get_function_defaults(fun: types.FunctionType) -> Optional[Tuple[Any, ...]]: ... +def get_function_globals(fun: types.FunctionType) -> Dict[str, Any]: ... + +def iterkeys(d: Mapping[_K, _V]) -> typing.Iterator[_K]: ... +def itervalues(d: Mapping[_K, _V]) -> typing.Iterator[_V]: ... +def iteritems(d: Mapping[_K, _V]) -> typing.Iterator[Tuple[_K, _V]]: ... +#def iterlists + +# TODO fix return types - python2 typing doesn't include KeysView etc yet. +def viewkeys(d: Mapping[_K, _V]) -> Iterable[_K]: ... +def viewvalues(d: Mapping[_K, _V]) -> Iterable[_V]: ... +def viewitems(d: Mapping[_K, _V]) -> Iterable[Tuple[_K, _V]]: ... + +def b(s: str) -> binary_type: ... +def u(s: str) -> text_type: ... +from __builtin__ import unichr as unichr +int2byte = chr +def byte2int(bs: binary_type) -> int: ... +def indexbytes(buf: binary_type, i: int) -> int: ... +def iterbytes(buf: binary_type) -> typing.Iterator[int]: ... +from StringIO import StringIO as StringIO, StringIO as BytesIO + +def assertCountEqual(self: unittest.TestCase, first: Iterable[_T], second: Iterable[_T], msg: str = None) -> None: ... +@overload +def assertRaisesRegex(self: unittest.TestCase, msg: str = None) -> Any: ... +@overload +def assertRaisesRegex(self: unittest.TestCase, callable_obj: Callable[..., Any], *args: Any, **kwargs: Any) -> Any: ... +def assertRegex(self: unittest.TestCase, text: AnyStr, expected_regex: Union[AnyStr, Pattern[AnyStr]], msg: str = None) -> None: ... + +def reraise(tp: type, value: Optional[BaseException], tb: types.TracebackType = None) -> None: ... +def exec_(_code_: Union[unicode, types.CodeType], _globs_: Dict[str, Any] = None, _locs_: Dict[str, Any] = None): ... +def raise_from(value: BaseException, from_value: BaseException) -> None: ... + +print_ = print + +from functools import wraps as wraps + +def with_metaclass(meta: type, *bases: type) -> type: ... +def add_metaclass(metaclass: type) -> Callable[[_T], _T]: ... +def python_2_unicode_compatible(klass: _T) -> _T: ... diff --git a/third_party/3/six/__init__.pyi b/third_party/3/six/__init__.pyi new file mode 100644 index 000000000..0607cc18f --- /dev/null +++ b/third_party/3/six/__init__.pyi @@ -0,0 +1,103 @@ +# Stubs for six (Python 3.5) + +from __future__ import print_function + +from typing import ( + Any, + AnyStr, + Callable, + Dict, + ItemsView, + Iterable, + KeysView, + Mapping, + Optional, + Pattern, + Tuple, + TypeVar, + Union, + ValuesView, + overload, +) +import typing + +import unittest +import types + +_T = TypeVar('_T') +_K = TypeVar('_K') +_V = TypeVar('_V') + +# TODO make constant, then move this stub to 2and3 +# https://github.com/python/typeshed/issues/17 +PY2 = False +PY3 = True +PY34 = ... # type: bool + +string_types = str, +integer_types = int, +class_types = type, +text_type = str +binary_type = bytes + +MAXSIZE = ... # type: int + +#def add_move +#def remove_move + +from builtins import next as advance_iterator +next = advance_iterator + +def callable(obj: object) -> bool: ... + +def get_unbound_function(unbound: types.FunctionType) -> types.FunctionType: ... +def create_bound_method(func: types.FunctionType, obj: object) -> types.MethodType: ... +def create_unbound_method(func: types.FunctionType, cls: type) -> types.FunctionType: ... + +Iterator = object + +def get_method_function(meth: types.MethodType) -> types.FunctionType: ... +def get_method_self(meth: types.MethodType) -> Optional[object]: ... +def get_function_closure(fun: types.FunctionType) -> Optional[Tuple[types._Cell, ...]]: ... +def get_function_code(fun: types.FunctionType) -> types.CodeType: ... +def get_function_defaults(fun: types.FunctionType) -> Optional[Tuple[Any, ...]]: ... +def get_function_globals(fun: types.FunctionType) -> Dict[str, Any]: ... + +def iterkeys(d: Mapping[_K, _V]) -> typing.Iterator[_K]: ... +def itervalues(d: Mapping[_K, _V]) -> typing.Iterator[_V]: ... +def iteritems(d: Mapping[_K, _V]) -> typing.Iterator[Tuple[_K, _V]]: ... +#def iterlists + +def viewkeys(d: Mapping[_K, _V]) -> KeysView[_K]: ... +def viewvalues(d: Mapping[_K, _V]) -> ValuesView[_V]: ... +def viewitems(d: Mapping[_K, _V]) -> ItemsView[_K, _V]: ... + +def b(s: str) -> binary_type: ... +def u(s: str) -> text_type: ... + +unichr = chr +def int2byte(i: int) -> bytes: ... +def byte2int(bs: binary_type) -> int: ... +def indexbytes(buf: binary_type, i: int) -> int: ... +def iterbytes(buf: binary_type) -> typing.Iterator[int]: ... +from io import StringIO as StringIO, BytesIO as BytesIO + +def assertCountEqual(self: unittest.TestCase, first: Iterable[_T], second: Iterable[_T], msg: str = None) -> None: ... +@overload +def assertRaisesRegex(self: unittest.TestCase, msg: str = None) -> Any: ... +@overload +def assertRaisesRegex(self: unittest.TestCase, callable_obj: Callable[..., Any], *args: Any, **kwargs: Any) -> Any: ... +def assertRegex(self: unittest.TestCase, text: AnyStr, expected_regex: Union[AnyStr, Pattern[AnyStr]], msg: str = None) -> None: ... + +exec_ = exec + +def reraise(tp: type, value: Optional[BaseException], tb: types.TracebackType = None) -> None: ... +def raise_from(value: BaseException, from_value: BaseException) -> None: ... + +print_ = print + +from functools import wraps as wraps + +def with_metaclass(meta: type, *bases: type) -> type: ... +def add_metaclass(metaclass: type) -> Callable[[_T], _T]: ... +def python_2_unicode_compatible(klass: _T) -> _T: ...