Skip to content

Commit

Permalink
Remove contextlib plugin from default (#13923)
Browse files Browse the repository at this point in the history
It is no longer needed. `typeshed` defines `contextlib.contextmanager`
and `contextlib.asynccontextmanager` using `ParamSpec`, which works the
same way as this plugin.
  • Loading branch information
sobolevn authored Oct 19, 2022
1 parent 3cf7ea1 commit 3108669
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 106 deletions.
23 changes: 1 addition & 22 deletions mypy/plugins/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class DefaultPlugin(Plugin):
def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
from mypy.plugins import ctypes, singledispatch

if fullname in ("contextlib.contextmanager", "contextlib.asynccontextmanager"):
return contextmanager_callback
elif fullname == "ctypes.Array":
if fullname == "ctypes.Array":
return ctypes.array_constructor_callback
elif fullname == "functools.singledispatch":
return singledispatch.create_singledispatch_function_callback
Expand Down Expand Up @@ -148,25 +146,6 @@ def get_class_decorator_hook_2(
return None


def contextmanager_callback(ctx: FunctionContext) -> Type:
"""Infer a better return type for 'contextlib.contextmanager'."""
# Be defensive, just in case.
if ctx.arg_types and len(ctx.arg_types[0]) == 1:
arg_type = get_proper_type(ctx.arg_types[0][0])
default_return = get_proper_type(ctx.default_return_type)
if isinstance(arg_type, CallableType) and isinstance(default_return, CallableType):
# The stub signature doesn't preserve information about arguments so
# add them back here.
return default_return.copy_modified(
arg_types=arg_type.arg_types,
arg_kinds=arg_type.arg_kinds,
arg_names=arg_type.arg_names,
variables=arg_type.variables,
is_ellipsis_args=arg_type.is_ellipsis_args,
)
return ctx.default_return_type


def typed_dict_get_signature_callback(ctx: MethodSigContext) -> CallableType:
"""Try to infer a better signature type for TypedDict.get.
Expand Down
84 changes: 0 additions & 84 deletions test-data/unit/check-default-plugin.test

This file was deleted.

1 change: 1 addition & 0 deletions test-data/unit/lib-stub/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ _T = TypeVar('_T')
class GeneratorContextManager(ContextManager[_T], Generic[_T]):
def __call__(self, func: Callable[..., _T]) -> Callable[..., _T]: ...

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

Expand Down

0 comments on commit 3108669

Please sign in to comment.