Skip to content

Commit 5db349b

Browse files
authored
fix(when): fix type overloads of then_enter_with on mypy v0.941 (#124)
1 parent e4987a2 commit 5db349b

File tree

6 files changed

+90
-90
lines changed

6 files changed

+90
-90
lines changed

decoy/__init__.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
"""Decoy stubbing and spying library."""
22
from warnings import warn
3-
from typing import Any, Callable, Generic, Optional, Union, cast, overload
3+
from typing import (
4+
TYPE_CHECKING,
5+
Any,
6+
Callable,
7+
Generic,
8+
Optional,
9+
Union,
10+
cast,
11+
overload,
12+
)
413

514
from . import errors, matchers, warnings
6-
from .context_managers import (
7-
AsyncContextManager,
8-
ContextManager,
9-
GeneratorContextManager,
10-
)
1115
from .core import DecoyCore, StubCore, PropCore
1216
from .types import ClassT, ContextValueT, FuncT, ReturnT
1317

18+
# TODO(mc, 2022-03-14): drop support for Python 3.6 in Decoy v2
19+
# Python 3.6 does not have async generator context managers
20+
if TYPE_CHECKING:
21+
from .context_managers import (
22+
ContextManager,
23+
AsyncContextManager,
24+
GeneratorContextManager,
25+
AsyncGeneratorContextManager,
26+
)
27+
1428
# ensure decoy does not pollute pytest tracebacks
1529
__tracebackhide__ = True
1630

@@ -294,11 +308,19 @@ def then_enter_with(
294308
) -> None:
295309
...
296310

311+
@overload
312+
def then_enter_with(
313+
self: "Stub[AsyncGeneratorContextManager[ContextValueT]]",
314+
value: ContextValueT,
315+
) -> None:
316+
...
317+
297318
def then_enter_with(
298319
self: Union[
299-
"Stub[GeneratorContextManager[ContextValueT]]",
300320
"Stub[ContextManager[ContextValueT]]",
321+
"Stub[GeneratorContextManager[ContextValueT]]",
301322
"Stub[AsyncContextManager[ContextValueT]]",
323+
"Stub[AsyncGeneratorContextManager[ContextValueT]]",
302324
],
303325
value: ContextValueT,
304326
) -> None:

decoy/context_managers.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
"""Wrappers around contextlib types and fallbacks."""
2-
import contextlib
3-
from typing import Any, AsyncContextManager, ContextManager, Generic, TypeVar
4-
5-
GeneratorContextManager = contextlib._GeneratorContextManager
2+
from typing import (
3+
TYPE_CHECKING,
4+
Any,
5+
AsyncContextManager,
6+
ContextManager,
7+
Generic,
8+
TypeVar,
9+
)
10+
11+
# TODO(mc, 2022-03-14): drop support for Python 3.6 in Decoy v2
12+
# Python 3.6 does not have async generator context managers
13+
if TYPE_CHECKING:
14+
from contextlib import ( # type: ignore[attr-defined]
15+
_GeneratorContextManager as GeneratorContextManager,
16+
_AsyncGeneratorContextManager as AsyncGeneratorContextManager,
17+
)
618

719
_EnterT = TypeVar("_EnterT")
820

@@ -40,7 +52,8 @@ async def __aexit__(self, *args: Any, **kwargs: Any) -> Any:
4052

4153
__all__ = [
4254
"AsyncContextManager",
43-
"GeneratorContextManager",
55+
"AsyncGeneratorContextManager",
4456
"ContextManager",
57+
"GeneratorContextManager",
4558
"ContextWrapper",
4659
]

0 commit comments

Comments
 (0)