From ebd32c59ed3ba888a727a12856d9f6b8a218dbf7 Mon Sep 17 00:00:00 2001 From: tharvik Date: Tue, 26 Jul 2016 13:13:15 +0200 Subject: [PATCH 1/5] remove old, new stubgen --- stdlib/2.7/contextlib.pyi | 17 ----------- stdlib/2and3/contextlib.pyi | 57 +++++++++++++++++++++++++++++++++++++ stdlib/3/contextlib.pyi | 17 ----------- 3 files changed, 57 insertions(+), 34 deletions(-) delete mode 100644 stdlib/2.7/contextlib.pyi create mode 100644 stdlib/2and3/contextlib.pyi delete mode 100644 stdlib/3/contextlib.pyi diff --git a/stdlib/2.7/contextlib.pyi b/stdlib/2.7/contextlib.pyi deleted file mode 100644 index 440009819473..000000000000 --- a/stdlib/2.7/contextlib.pyi +++ /dev/null @@ -1,17 +0,0 @@ -# Stubs for contextlib - -# NOTE: These are incomplete! - -from typing import Callable, Generic, Iterator, TypeVar - -_T = TypeVar('_T') - -class ContextManager(Generic[_T]): - def __enter__(self) -> _T: ... - def __exit__(self, *exc_info) -> None: ... - -# TODO this doesn't capture the relationship that the returned function's args are the same as func's. -def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ... - -class closing(ContextManager[_T], Generic[_T]): - def __init__(self, thing: _T) -> None: ... diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi new file mode 100644 index 000000000000..33f9739c6f59 --- /dev/null +++ b/stdlib/2and3/contextlib.pyi @@ -0,0 +1,57 @@ +# Stubs for contextlib (Python 3.4) +# +# NOTE: This dynamically typed stub was automatically generated by stubgen. + +from typing import Any +from typing import Callable, Generic, Iterator, TypeVar + +class ContextDecorator: + def __call__(self, func): ... + +class _GeneratorContextManager(ContextDecorator): + gen = ... # type: Any + __doc__ = ... # type: Any + def __init__(self, func, *args, **kwds) -> None: ... + def __enter__(self): ... + def __exit__(self, type, value, traceback): ... + +def contextmanager(func): ... + +class closing: + thing = ... # type: Any + def __init__(self, thing) -> None: ... + def __enter__(self): ... + def __exit__(self, *exc_info): ... + +class redirect_stdout: + def __init__(self, new_target) -> None: ... + def __enter__(self): ... + def __exit__(self, exctype, excinst, exctb): ... + +class suppress: + def __init__(self, *exceptions) -> None: ... + def __enter__(self): ... + def __exit__(self, exctype, excinst, exctb): ... + +class ExitStack: + def __init__(self) -> None: ... + def pop_all(self): ... + def push(self, exit): ... + def callback(self, callback, *args, **kwds): ... + def enter_context(self, cm): ... + def close(self): ... + def __enter__(self): ... + def __exit__(self, *exc_details): ... + + +#_T = TypeVar('_T') +# +#class ContextManager(Generic[_T]): +# def __enter__(self) -> _T: ... +# def __exit__(self, *exc_info) -> None: ... +# +## TODO this doesn't capture the relationship that the returned function's args are the same as func's. +#def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ... +# +#class closing(ContextManager[_T], Generic[_T]): +# def __init__(self, thing: _T) -> None: ... diff --git a/stdlib/3/contextlib.pyi b/stdlib/3/contextlib.pyi deleted file mode 100644 index 440009819473..000000000000 --- a/stdlib/3/contextlib.pyi +++ /dev/null @@ -1,17 +0,0 @@ -# Stubs for contextlib - -# NOTE: These are incomplete! - -from typing import Callable, Generic, Iterator, TypeVar - -_T = TypeVar('_T') - -class ContextManager(Generic[_T]): - def __enter__(self) -> _T: ... - def __exit__(self, *exc_info) -> None: ... - -# TODO this doesn't capture the relationship that the returned function's args are the same as func's. -def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ... - -class closing(ContextManager[_T], Generic[_T]): - def __init__(self, thing: _T) -> None: ... From 2701cfdbf89ddb46cfaba10c1b2eba26ad3c6242 Mon Sep 17 00:00:00 2001 From: tharvik Date: Tue, 26 Jul 2016 13:14:03 +0200 Subject: [PATCH 2/5] comment everything --- stdlib/2and3/contextlib.pyi | 111 ++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index 33f9739c6f59..ff915c0a7752 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -1,57 +1,54 @@ -# Stubs for contextlib (Python 3.4) -# -# NOTE: This dynamically typed stub was automatically generated by stubgen. - -from typing import Any -from typing import Callable, Generic, Iterator, TypeVar - -class ContextDecorator: - def __call__(self, func): ... - -class _GeneratorContextManager(ContextDecorator): - gen = ... # type: Any - __doc__ = ... # type: Any - def __init__(self, func, *args, **kwds) -> None: ... - def __enter__(self): ... - def __exit__(self, type, value, traceback): ... - -def contextmanager(func): ... - -class closing: - thing = ... # type: Any - def __init__(self, thing) -> None: ... - def __enter__(self): ... - def __exit__(self, *exc_info): ... - -class redirect_stdout: - def __init__(self, new_target) -> None: ... - def __enter__(self): ... - def __exit__(self, exctype, excinst, exctb): ... - -class suppress: - def __init__(self, *exceptions) -> None: ... - def __enter__(self): ... - def __exit__(self, exctype, excinst, exctb): ... - -class ExitStack: - def __init__(self) -> None: ... - def pop_all(self): ... - def push(self, exit): ... - def callback(self, callback, *args, **kwds): ... - def enter_context(self, cm): ... - def close(self): ... - def __enter__(self): ... - def __exit__(self, *exc_details): ... - - -#_T = TypeVar('_T') -# -#class ContextManager(Generic[_T]): -# def __enter__(self) -> _T: ... -# def __exit__(self, *exc_info) -> None: ... -# -## TODO this doesn't capture the relationship that the returned function's args are the same as func's. -#def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ... -# -#class closing(ContextManager[_T], Generic[_T]): -# def __init__(self, thing: _T) -> None: ... +# Stubs for contextlib + +from typing import Any, Callable, Generic, Iterator, TypeVar + +#class ContextDecorator: +# def __call__(self, func): ... + +#class _GeneratorContextManager(ContextDecorator): +# gen = ... # type: Any +# __doc__ = ... # type: Any +# def __init__(self, func, *args, **kwds) -> None: ... +# def __enter__(self): ... +# def __exit__(self, type, value, traceback): ... + +#def contextmanager(func): ... + +#class closing: +# thing = ... # type: Any +# def __init__(self, thing) -> None: ... +# def __enter__(self): ... +# def __exit__(self, *exc_info): ... + +#class redirect_stdout: +# def __init__(self, new_target) -> None: ... +# def __enter__(self): ... +# def __exit__(self, exctype, excinst, exctb): ... + +#class suppress: +# def __init__(self, *exceptions) -> None: ... +# def __enter__(self): ... +# def __exit__(self, exctype, excinst, exctb): ... + +#class ExitStack: +# def __init__(self) -> None: ... +# def pop_all(self): ... +# def push(self, exit): ... +# def callback(self, callback, *args, **kwds): ... +# def enter_context(self, cm): ... +# def close(self): ... +# def __enter__(self): ... +# def __exit__(self, *exc_details): ... + + +##_T = TypeVar('_T') +## +##class ContextManager(Generic[_T]): +## def __enter__(self) -> _T: ... +## def __exit__(self, *exc_info) -> None: ... +## +### TODO this doesn't capture the relationship that the returned function's args are the same as func's. +##def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ... +## +##class closing(ContextManager[_T], Generic[_T]): +## def __init__(self, thing: _T) -> None: ... From 961976b6638a3e28aed49d4dc7bf18b999f22867 Mon Sep 17 00:00:00 2001 From: tharvik Date: Tue, 26 Jul 2016 15:07:14 +0200 Subject: [PATCH 3/5] contextlib done --- stdlib/2and3/contextlib.pyi | 101 +++++++++++++++++------------------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index ff915c0a7752..d0dd04fe306d 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -1,54 +1,51 @@ # Stubs for contextlib -from typing import Any, Callable, Generic, Iterator, TypeVar - -#class ContextDecorator: -# def __call__(self, func): ... - -#class _GeneratorContextManager(ContextDecorator): -# gen = ... # type: Any -# __doc__ = ... # type: Any -# def __init__(self, func, *args, **kwds) -> None: ... -# def __enter__(self): ... -# def __exit__(self, type, value, traceback): ... - -#def contextmanager(func): ... - -#class closing: -# thing = ... # type: Any -# def __init__(self, thing) -> None: ... -# def __enter__(self): ... -# def __exit__(self, *exc_info): ... - -#class redirect_stdout: -# def __init__(self, new_target) -> None: ... -# def __enter__(self): ... -# def __exit__(self, exctype, excinst, exctb): ... - -#class suppress: -# def __init__(self, *exceptions) -> None: ... -# def __enter__(self): ... -# def __exit__(self, exctype, excinst, exctb): ... - -#class ExitStack: -# def __init__(self) -> None: ... -# def pop_all(self): ... -# def push(self, exit): ... -# def callback(self, callback, *args, **kwds): ... -# def enter_context(self, cm): ... -# def close(self): ... -# def __enter__(self): ... -# def __exit__(self, *exc_details): ... - - -##_T = TypeVar('_T') -## -##class ContextManager(Generic[_T]): -## def __enter__(self) -> _T: ... -## def __exit__(self, *exc_info) -> None: ... -## -### TODO this doesn't capture the relationship that the returned function's args are the same as func's. -##def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ... -## -##class closing(ContextManager[_T], Generic[_T]): -## def __init__(self, thing: _T) -> None: ... +from typing import ( + Any, Callable, Generator, IO, Optional, Type, + Generic, TypeVar, overload, +) +from types import TracebackType +import sys + +_T = TypeVar('_T') +_ExitFunc = Callable[[Optional[Type[BaseException]], + Optional[Exception], + Optional[TracebackType]], bool] + +# TODO already in PEP, have to get added to mypy +class ContextManager(Generic[_T]): + def __enter__(self) -> _T: ... + def __exit__(self, exc_type: Optional[Type[BaseException]], + exc_val: Optional[Exception], + exc_tb: Optional[TracebackType]) -> bool: ... + +def contextmanager(func: Callable[..., Generator[None, None, None]]) -> Callable[..., ContextManager[None]]: ... + +class closing(Generic[_T], ContextManager[_T]): + def __init__(self, thing: _T) -> None: ... + +if sys.version_info >= (3, 4): + class suppress(ContextManager[None]): + def __init__(self, *exceptions: Type[BaseException]) -> None: ... + + class redirect_stdout(ContextManager[None]): + def __init__(self, new_target: IO[str]) -> None: ... + +if sys.version_info >= (3, 5): + class redirect_stderr(ContextManager[None]): + def __init__(self, new_target: IO[str]) -> None: ... + +class ContextDecorator: + def __call__(self, func: Callable[..., None]) -> Callable[..., ContextManager[None]]: ... + +class ExitStack(ContextManager[ExitStack]): + def __init__(self) -> None: ... + def enter_context(self, cm: ContextManager[_T]) -> _T: ... + @overload + def push(self, exit: ContextManager[_T]) -> ContextManager[_T]: ... + @overload + def push(self, exit: _ExitFunc) -> _ExitFunc: ... + def callback(self, callback: Callable[..., None], + *args: Any, **kwds: Any) -> Callable[..., None]: ... + def pop_all(self) -> ExitStack: ... + def close(self) -> None: ... From 1382ad147a7a64735ec537c88b1969d24a607fa8 Mon Sep 17 00:00:00 2001 From: tharvik Date: Tue, 26 Jul 2016 18:15:48 +0200 Subject: [PATCH 4/5] use TypeVar instead of overload --- stdlib/2and3/contextlib.pyi | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index d0dd04fe306d..d7d1df3e785a 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -2,7 +2,7 @@ from typing import ( Any, Callable, Generator, IO, Optional, Type, - Generic, TypeVar, overload, + Generic, TypeVar, ) from types import TracebackType import sys @@ -11,6 +11,7 @@ _T = TypeVar('_T') _ExitFunc = Callable[[Optional[Type[BaseException]], Optional[Exception], Optional[TracebackType]], bool] +_CM_EF = TypeVar('_CM_EF', ContextManager, _ExitFunc) # TODO already in PEP, have to get added to mypy class ContextManager(Generic[_T]): @@ -41,10 +42,7 @@ class ContextDecorator: class ExitStack(ContextManager[ExitStack]): def __init__(self) -> None: ... def enter_context(self, cm: ContextManager[_T]) -> _T: ... - @overload - def push(self, exit: ContextManager[_T]) -> ContextManager[_T]: ... - @overload - def push(self, exit: _ExitFunc) -> _ExitFunc: ... + def push(self, exit: _CM_EF) -> _CM_EF: ... def callback(self, callback: Callable[..., None], *args: Any, **kwds: Any) -> Callable[..., None]: ... def pop_all(self) -> ExitStack: ... From 34a5478e5c0dff6baa5c4671a9e0630857a67709 Mon Sep 17 00:00:00 2001 From: tharvik Date: Wed, 27 Jul 2016 08:58:19 +0200 Subject: [PATCH 5/5] py2 done --- stdlib/2and3/contextlib.pyi | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index d7d1df3e785a..eaae3ff72121 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -22,6 +22,9 @@ class ContextManager(Generic[_T]): def contextmanager(func: Callable[..., Generator[None, None, None]]) -> Callable[..., ContextManager[None]]: ... +if sys.version_info < (3,): + def nested(*mgr: ContextManager[Any]) -> ContextManager[None]: ... + class closing(Generic[_T], ContextManager[_T]): def __init__(self, thing: _T) -> None: ... @@ -36,14 +39,15 @@ if sys.version_info >= (3, 5): class redirect_stderr(ContextManager[None]): def __init__(self, new_target: IO[str]) -> None: ... -class ContextDecorator: - def __call__(self, func: Callable[..., None]) -> Callable[..., ContextManager[None]]: ... - -class ExitStack(ContextManager[ExitStack]): - def __init__(self) -> None: ... - def enter_context(self, cm: ContextManager[_T]) -> _T: ... - def push(self, exit: _CM_EF) -> _CM_EF: ... - def callback(self, callback: Callable[..., None], - *args: Any, **kwds: Any) -> Callable[..., None]: ... - def pop_all(self) -> ExitStack: ... - def close(self) -> None: ... +if sys.version_info >= (3,): + class ContextDecorator: + def __call__(self, func: Callable[..., None]) -> Callable[..., ContextManager[None]]: ... + + class ExitStack(ContextManager[ExitStack]): + def __init__(self) -> None: ... + def enter_context(self, cm: ContextManager[_T]) -> _T: ... + def push(self, exit: _CM_EF) -> _CM_EF: ... + def callback(self, callback: Callable[..., None], + *args: Any, **kwds: Any) -> Callable[..., None]: ... + def pop_all(self) -> ExitStack: ... + def close(self) -> None: ...