Skip to content

Commit 0647570

Browse files
authored
Delete open plugin (#9275)
Improvements to typeshed types using literals should have rendered this unnecessary.
1 parent 872c03d commit 0647570

File tree

4 files changed

+21
-78
lines changed

4 files changed

+21
-78
lines changed

mypy/plugins/default.py

+2-59
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
from typing import Callable, Optional, List
33

44
from mypy import message_registry
5-
from mypy.nodes import Expression, StrExpr, IntExpr, DictExpr, UnaryExpr
5+
from mypy.nodes import StrExpr, IntExpr, DictExpr, UnaryExpr
66
from mypy.plugin import (
7-
Plugin, FunctionContext, MethodContext, MethodSigContext, AttributeContext, ClassDefContext,
8-
CheckerPluginInterface,
7+
Plugin, FunctionContext, MethodContext, MethodSigContext, AttributeContext, ClassDefContext
98
)
109
from mypy.plugins.common import try_getting_str_literals
1110
from mypy.types import (
@@ -26,8 +25,6 @@ def get_function_hook(self, fullname: str
2625

2726
if fullname in ('contextlib.contextmanager', 'contextlib.asynccontextmanager'):
2827
return contextmanager_callback
29-
elif fullname == 'builtins.open' and self.python_version[0] == 3:
30-
return open_callback
3128
elif fullname == 'ctypes.Array':
3229
return ctypes.array_constructor_callback
3330
elif fullname == 'functools.singledispatch':
@@ -74,8 +71,6 @@ def get_method_hook(self, fullname: str
7471
return ctypes.array_getitem_callback
7572
elif fullname == 'ctypes.Array.__iter__':
7673
return ctypes.array_iter_callback
77-
elif fullname == 'pathlib.Path.open':
78-
return path_open_callback
7974
elif fullname == singledispatch.SINGLEDISPATCH_REGISTER_METHOD:
8075
return singledispatch.singledispatch_register_callback
8176
elif fullname == singledispatch.REGISTER_CALLABLE_CALL_METHOD:
@@ -129,58 +124,6 @@ def get_class_decorator_hook(self, fullname: str
129124
return None
130125

131126

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-
184127
def contextmanager_callback(ctx: FunctionContext) -> Type:
185128
"""Infer a better return type for 'contextlib.contextmanager'."""
186129
# Be defensive, just in case.

mypy/report.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ def _write_out_report(self,
245245
f.write(separator + '\n')
246246
for row_values in rows:
247247
r = ("{:>{}}" * len(widths)).format(*itertools.chain(*zip(row_values, widths)))
248-
f.writelines(r + '\n')
248+
f.write(r + '\n')
249249
f.write(separator + '\n')
250250
footer_str = ("{:>{}}" * len(widths)).format(*itertools.chain(*zip(footer, widths)))
251-
f.writelines(footer_str + '\n')
251+
f.write(footer_str + '\n')
252252

253253
def _report_any_exprs(self) -> None:
254254
total_any = sum(num_any for num_any, _ in self.counts.values())

mypyc/build.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ def write_file(path: str, contents: str) -> None:
308308
old_contents = None
309309
if old_contents != encoded_contents:
310310
os.makedirs(os.path.dirname(path), exist_ok=True)
311-
with open(path, 'wb') as f:
312-
f.write(encoded_contents)
311+
with open(path, 'wb') as g:
312+
g.write(encoded_contents)
313313

314314
# Fudge the mtime forward because otherwise when two builds happen close
315315
# together (like in a test) setuptools might not realize the source is newer

test-data/unit/pythoneval.test

+15-15
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ f.write('x')
271271
f.write(b'x')
272272
f.foobar()
273273
[out]
274-
_program.py:3: error: Argument 1 to "write" of "IO" has incompatible type "bytes"; expected "str"
275-
_program.py:4: error: "TextIO" has no attribute "foobar"
274+
_program.py:3: error: Argument 1 to "write" of "TextIOBase" has incompatible type "bytes"; expected "str"
275+
_program.py:4: error: "TextIOWrapper" has no attribute "foobar"
276276

277277
[case testOpenReturnTypeInference]
278278
reveal_type(open('x'))
@@ -281,9 +281,9 @@ reveal_type(open('x', 'rb'))
281281
mode = 'rb'
282282
reveal_type(open('x', mode))
283283
[out]
284-
_program.py:1: note: Revealed type is "typing.TextIO"
285-
_program.py:2: note: Revealed type is "typing.TextIO"
286-
_program.py:3: note: Revealed type is "typing.BinaryIO"
284+
_program.py:1: note: Revealed type is "io.TextIOWrapper"
285+
_program.py:2: note: Revealed type is "io.TextIOWrapper"
286+
_program.py:3: note: Revealed type is "io.BufferedReader"
287287
_program.py:5: note: Revealed type is "typing.IO[Any]"
288288

289289
[case testOpenReturnTypeInferenceSpecialCases]
@@ -292,8 +292,8 @@ reveal_type(open(file='x', mode='rb'))
292292
mode = 'rb'
293293
reveal_type(open(mode=mode, file='r'))
294294
[out]
295-
_testOpenReturnTypeInferenceSpecialCases.py:1: note: Revealed type is "typing.BinaryIO"
296-
_testOpenReturnTypeInferenceSpecialCases.py:2: note: Revealed type is "typing.BinaryIO"
295+
_testOpenReturnTypeInferenceSpecialCases.py:1: note: Revealed type is "io.BufferedReader"
296+
_testOpenReturnTypeInferenceSpecialCases.py:2: note: Revealed type is "io.BufferedReader"
297297
_testOpenReturnTypeInferenceSpecialCases.py:4: note: Revealed type is "typing.IO[Any]"
298298

299299
[case testPathOpenReturnTypeInference]
@@ -305,21 +305,21 @@ reveal_type(p.open('rb'))
305305
mode = 'rb'
306306
reveal_type(p.open(mode))
307307
[out]
308-
_program.py:3: note: Revealed type is "typing.TextIO"
309-
_program.py:4: note: Revealed type is "typing.TextIO"
310-
_program.py:5: note: Revealed type is "typing.BinaryIO"
308+
_program.py:3: note: Revealed type is "io.TextIOWrapper"
309+
_program.py:4: note: Revealed type is "io.TextIOWrapper"
310+
_program.py:5: note: Revealed type is "io.BufferedReader"
311311
_program.py:7: note: Revealed type is "typing.IO[Any]"
312312

313313
[case testPathOpenReturnTypeInferenceSpecialCases]
314314
from pathlib import Path
315315
p = Path("x")
316-
reveal_type(p.open(mode='rb', errors='replace'))
317-
reveal_type(p.open(errors='replace', mode='rb'))
318-
mode = 'rb'
316+
reveal_type(p.open(mode='r', errors='replace'))
317+
reveal_type(p.open(errors='replace', mode='r'))
318+
mode = 'r'
319319
reveal_type(p.open(mode=mode, errors='replace'))
320320
[out]
321-
_program.py:3: note: Revealed type is "typing.BinaryIO"
322-
_program.py:4: note: Revealed type is "typing.BinaryIO"
321+
_program.py:3: note: Revealed type is "io.TextIOWrapper"
322+
_program.py:4: note: Revealed type is "io.TextIOWrapper"
323323
_program.py:6: note: Revealed type is "typing.IO[Any]"
324324

325325
[case testGenericPatterns]

0 commit comments

Comments
 (0)