Skip to content

Commit 3108669

Browse files
authored
Remove contextlib plugin from default (#13923)
It is no longer needed. `typeshed` defines `contextlib.contextmanager` and `contextlib.asynccontextmanager` using `ParamSpec`, which works the same way as this plugin.
1 parent 3cf7ea1 commit 3108669

File tree

3 files changed

+2
-106
lines changed

3 files changed

+2
-106
lines changed

mypy/plugins/default.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ class DefaultPlugin(Plugin):
3939
def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
4040
from mypy.plugins import ctypes, singledispatch
4141

42-
if fullname in ("contextlib.contextmanager", "contextlib.asynccontextmanager"):
43-
return contextmanager_callback
44-
elif fullname == "ctypes.Array":
42+
if fullname == "ctypes.Array":
4543
return ctypes.array_constructor_callback
4644
elif fullname == "functools.singledispatch":
4745
return singledispatch.create_singledispatch_function_callback
@@ -148,25 +146,6 @@ def get_class_decorator_hook_2(
148146
return None
149147

150148

151-
def contextmanager_callback(ctx: FunctionContext) -> Type:
152-
"""Infer a better return type for 'contextlib.contextmanager'."""
153-
# Be defensive, just in case.
154-
if ctx.arg_types and len(ctx.arg_types[0]) == 1:
155-
arg_type = get_proper_type(ctx.arg_types[0][0])
156-
default_return = get_proper_type(ctx.default_return_type)
157-
if isinstance(arg_type, CallableType) and isinstance(default_return, CallableType):
158-
# The stub signature doesn't preserve information about arguments so
159-
# add them back here.
160-
return default_return.copy_modified(
161-
arg_types=arg_type.arg_types,
162-
arg_kinds=arg_type.arg_kinds,
163-
arg_names=arg_type.arg_names,
164-
variables=arg_type.variables,
165-
is_ellipsis_args=arg_type.is_ellipsis_args,
166-
)
167-
return ctx.default_return_type
168-
169-
170149
def typed_dict_get_signature_callback(ctx: MethodSigContext) -> CallableType:
171150
"""Try to infer a better signature type for TypedDict.get.
172151

test-data/unit/check-default-plugin.test

Lines changed: 0 additions & 84 deletions
This file was deleted.

test-data/unit/lib-stub/contextlib.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ _T = TypeVar('_T')
77
class GeneratorContextManager(ContextManager[_T], Generic[_T]):
88
def __call__(self, func: Callable[..., _T]) -> Callable[..., _T]: ...
99

10+
# This does not match `typeshed` definition, needs `ParamSpec`:
1011
def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., GeneratorContextManager[_T]]:
1112
...
1213

0 commit comments

Comments
 (0)