2
2
from typing import Callable , Optional , List
3
3
4
4
from mypy import message_registry
5
- from mypy .nodes import Expression , StrExpr , IntExpr , DictExpr , UnaryExpr
5
+ from mypy .nodes import StrExpr , IntExpr , DictExpr , UnaryExpr
6
6
from mypy .plugin import (
7
- Plugin , FunctionContext , MethodContext , MethodSigContext , AttributeContext , ClassDefContext ,
8
- CheckerPluginInterface ,
7
+ Plugin , FunctionContext , MethodContext , MethodSigContext , AttributeContext , ClassDefContext
9
8
)
10
9
from mypy .plugins .common import try_getting_str_literals
11
10
from mypy .types import (
@@ -26,8 +25,6 @@ def get_function_hook(self, fullname: str
26
25
27
26
if fullname in ('contextlib.contextmanager' , 'contextlib.asynccontextmanager' ):
28
27
return contextmanager_callback
29
- elif fullname == 'builtins.open' and self .python_version [0 ] == 3 :
30
- return open_callback
31
28
elif fullname == 'ctypes.Array' :
32
29
return ctypes .array_constructor_callback
33
30
elif fullname == 'functools.singledispatch' :
@@ -74,8 +71,6 @@ def get_method_hook(self, fullname: str
74
71
return ctypes .array_getitem_callback
75
72
elif fullname == 'ctypes.Array.__iter__' :
76
73
return ctypes .array_iter_callback
77
- elif fullname == 'pathlib.Path.open' :
78
- return path_open_callback
79
74
elif fullname == singledispatch .SINGLEDISPATCH_REGISTER_METHOD :
80
75
return singledispatch .singledispatch_register_callback
81
76
elif fullname == singledispatch .REGISTER_CALLABLE_CALL_METHOD :
@@ -129,58 +124,6 @@ def get_class_decorator_hook(self, fullname: str
129
124
return None
130
125
131
126
132
- def open_callback (ctx : FunctionContext ) -> Type :
133
- """Infer a better return type for 'open'."""
134
- return _analyze_open_signature (
135
- arg_types = ctx .arg_types ,
136
- args = ctx .args ,
137
- mode_arg_index = 1 ,
138
- default_return_type = ctx .default_return_type ,
139
- api = ctx .api ,
140
- )
141
-
142
-
143
- def path_open_callback (ctx : MethodContext ) -> Type :
144
- """Infer a better return type for 'pathlib.Path.open'."""
145
- return _analyze_open_signature (
146
- arg_types = ctx .arg_types ,
147
- args = ctx .args ,
148
- mode_arg_index = 0 ,
149
- default_return_type = ctx .default_return_type ,
150
- api = ctx .api ,
151
- )
152
-
153
-
154
- def _analyze_open_signature (arg_types : List [List [Type ]],
155
- args : List [List [Expression ]],
156
- mode_arg_index : int ,
157
- default_return_type : Type ,
158
- api : CheckerPluginInterface ,
159
- ) -> Type :
160
- """A helper for analyzing any function that has approximately
161
- the same signature as the builtin 'open(...)' function.
162
-
163
- Currently, the only thing the caller can customize is the index
164
- of the 'mode' argument. If the mode argument is omitted or is a
165
- string literal, we refine the return type to either 'TextIO' or
166
- 'BinaryIO' as appropriate.
167
- """
168
- mode = None
169
- if not arg_types or len (arg_types [mode_arg_index ]) != 1 :
170
- mode = 'r'
171
- else :
172
- mode_expr = args [mode_arg_index ][0 ]
173
- if isinstance (mode_expr , StrExpr ):
174
- mode = mode_expr .value
175
- if mode is not None :
176
- assert isinstance (default_return_type , Instance ) # type: ignore
177
- if 'b' in mode :
178
- return api .named_generic_type ('typing.BinaryIO' , [])
179
- else :
180
- return api .named_generic_type ('typing.TextIO' , [])
181
- return default_return_type
182
-
183
-
184
127
def contextmanager_callback (ctx : FunctionContext ) -> Type :
185
128
"""Infer a better return type for 'contextlib.contextmanager'."""
186
129
# Be defensive, just in case.
0 commit comments