Skip to content

Commit

Permalink
Merge pull request #38 from ncoghlan/issue-37-mypy-stubtest
Browse files Browse the repository at this point in the history
Issue #37: Use mypy.stubcheck to validate stub file
  • Loading branch information
ncoghlan authored Jun 27, 2021
2 parents 190f6af + fffe1d9 commit 94174ba
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
19 changes: 13 additions & 6 deletions contextlib2/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ from typing import (
)
from typing_extensions import ParamSpec, Protocol

# Note: the various 'if True:' guards replace sys.version checks in the
# original typeshed file that don't apply to the contextlib2 backport API
# contextlib2 API adaptation notes:
# * the various 'if True:' guards replace sys.version checks in the original
# typeshed file (those APIs are available on all supported versions)
# * deliberately omitted APIs are listed in `dev/mypy.allowlist`
# (e.g. deprecated experimental APIs that never graduated to the stdlib)

AbstractContextManager = ContextManager
if True:
Expand Down Expand Up @@ -119,7 +122,11 @@ if True:
) -> Awaitable[bool]: ...

if True:
@overload
def nullcontext(enter_result: _T) -> ContextManager[_T]: ...
@overload
def nullcontext() -> ContextManager[None]: ...
class nullcontext(AbstractContextManager[_T]):
enter_result: _T
@overload
def __init__(self: nullcontext[None], enter_result: None = ...) -> None: ...
@overload
def __init__(self: nullcontext[_T], enter_result: _T) -> None: ...
def __enter__(self) -> _T: ...
def __exit__(self, *exctype: Any) -> bool: ...
3 changes: 3 additions & 0 deletions dev/mypy.allowlist
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Deprecated APIs that never graduated to the standard library
contextlib2.ContextDecorator.refresh_cm
contextlib2.ContextStack
27 changes: 15 additions & 12 deletions dev/py3_10_contextlib_pyi_to_contextlib2_pyi.patch
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
--- ../contextlib.pyi 2021-06-26 21:36:16.491964153 +1000
+++ contextlib2/__init__.pyi 2021-06-26 21:41:08.109598690 +1000
--- ../contextlib.pyi 2021-06-27 16:02:28.004872421 +1000
+++ contextlib2/__init__.pyi 2021-06-27 16:00:25.431733524 +1000
@@ -1,3 +1,6 @@
+# Type hints copied from the typeshed project under the Apache License 2.0
+# https://github.com/python/typeshed/blob/64c85cdd449ccaff90b546676220c9ecfa6e697f/LICENSE
+
import sys
from types import TracebackType
from typing import (
@@ -16,8 +19,11 @@
@@ -16,8 +19,14 @@
)
from typing_extensions import ParamSpec, Protocol

+# Note: the various 'if True:' guards replace sys.version checks in the
+# original typeshed file that don't apply to the contextlib2 backport API
+# contextlib2 API adaptation notes:
+# * the various 'if True:' guards replace sys.version checks in the original
+# typeshed file (those APIs are available on all supported versions)
+# * deliberately omitted APIs are listed in `dev/mypy.allowlist`
+# (e.g. deprecated experimental APIs that never graduated to the stdlib)
+
AbstractContextManager = ContextManager
-if sys.version_info >= (3, 7):
+if True:
AbstractAsyncContextManager = AsyncContextManager

_T = TypeVar("_T")
@@ -35,7 +41,7 @@
@@ -35,7 +44,7 @@
# type ignore to deal with incomplete ParamSpec support in mypy
def contextmanager(func: Callable[_P, Iterator[_T]]) -> Callable[_P, _GeneratorContextManager[_T]]: ... # type: ignore

Expand All @@ -29,7 +32,7 @@
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T]]) -> Callable[_P, AsyncContextManager[_T]]: ... # type: ignore

class _SupportsClose(Protocol):
@@ -46,7 +52,7 @@
@@ -46,7 +55,7 @@
class closing(ContextManager[_SupportsCloseT]):
def __init__(self, thing: _SupportsCloseT) -> None: ...

Expand All @@ -38,7 +41,7 @@
class _SupportsAclose(Protocol):
async def aclose(self) -> object: ...
_SupportsAcloseT = TypeVar("_SupportsAcloseT", bound=_SupportsAclose)
@@ -88,7 +94,7 @@
@@ -88,7 +97,7 @@
__traceback: Optional[TracebackType],
) -> bool: ...

Expand All @@ -47,12 +50,12 @@
_S = TypeVar("_S", bound=AsyncExitStack)

_ExitCoroFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], Awaitable[bool]]
@@ -112,7 +118,7 @@
@@ -112,7 +121,7 @@
__traceback: Optional[TracebackType],
) -> Awaitable[bool]: ...

-if sys.version_info >= (3, 7):
+if True:
@overload
def nullcontext(enter_result: _T) -> ContextManager[_T]: ...
@overload
class nullcontext(AbstractContextManager[_T]):
enter_result: _T
@overload
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ commands =
coverage report
coverage xml
# mypy won't install on PyPy, so only run the typechecking on CPython
!pypy3: mypy contextlib2
!pypy3: python -m mypy.stubtest --allowlist dev/mypy.allowlist contextlib2
deps =
coverage
!pypy3: mypy
Expand Down

0 comments on commit 94174ba

Please sign in to comment.