Skip to content

Make return type of open() more precise #3477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions mypy/funcplugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def open_callback(
named_generic_type: Callable[[str, List[Type]], Type]) -> Type:
"""Infer a better return type for 'open'.

Infer IO[str] or IO[bytes] as the return value if the mode argument is not
Infer TextIO or BinaryIO as the return value if the mode argument is not
given or is a literal.
"""
mode = None
Expand All @@ -55,10 +55,9 @@ def open_callback(
if mode is not None:
assert isinstance(inferred_return_type, Instance)
if 'b' in mode:
arg = named_generic_type('builtins.bytes', [])
return named_generic_type('typing.BinaryIO', [])
else:
arg = named_generic_type('builtins.str', [])
return Instance(inferred_return_type.type, [arg])
return named_generic_type('typing.TextIO', [])
return inferred_return_type


Expand Down
14 changes: 7 additions & 7 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ f.write(b'x')
f.foobar()
[out]
_program.py:3: error: Argument 1 to "write" of "IO" has incompatible type "bytes"; expected "str"
_program.py:4: error: IO[str] has no attribute "foobar"
_program.py:4: error: "TextIO" has no attribute "foobar"

[case testOpenReturnTypeInference]
reveal_type(open('x'))
Expand All @@ -409,9 +409,9 @@ reveal_type(open('x', 'rb'))
mode = 'rb'
reveal_type(open('x', mode))
[out]
_program.py:1: error: Revealed type is 'typing.IO[builtins.str]'
_program.py:2: error: Revealed type is 'typing.IO[builtins.str]'
_program.py:3: error: Revealed type is 'typing.IO[builtins.bytes]'
_program.py:1: error: Revealed type is 'typing.TextIO'
_program.py:2: error: Revealed type is 'typing.TextIO'
_program.py:3: error: Revealed type is 'typing.BinaryIO'
_program.py:5: error: Revealed type is 'typing.IO[Any]'

[case testOpenReturnTypeInferenceSpecialCases]
Expand All @@ -421,10 +421,10 @@ reveal_type(open(file='x', mode='rb'))
mode = 'rb'
reveal_type(open(mode=mode, file='r'))
[out]
_testOpenReturnTypeInferenceSpecialCases.py:1: error: Revealed type is 'typing.IO[builtins.str]'
_testOpenReturnTypeInferenceSpecialCases.py:1: error: Revealed type is 'typing.TextIO'
_testOpenReturnTypeInferenceSpecialCases.py:1: error: Too few arguments for "open"
_testOpenReturnTypeInferenceSpecialCases.py:2: error: Revealed type is 'typing.IO[builtins.bytes]'
_testOpenReturnTypeInferenceSpecialCases.py:3: error: Revealed type is 'typing.IO[builtins.bytes]'
_testOpenReturnTypeInferenceSpecialCases.py:2: error: Revealed type is 'typing.BinaryIO'
_testOpenReturnTypeInferenceSpecialCases.py:3: error: Revealed type is 'typing.BinaryIO'
_testOpenReturnTypeInferenceSpecialCases.py:5: error: Revealed type is 'typing.IO[Any]'

[case testGenericPatterns]
Expand Down