forked from python/typeshed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve open overloads when mode is a literal union
As pointed out by @gvanrossum in python/typing#1096 Improves type inference in cases when we know that mode is OpenBinaryMode, but don't know anything more specific: ``` def my_open(name: str, write: bool): mode: Literal['rb', 'wb'] = 'wb' if write else 'rb' with open(name, mode) as f: reveal_type(f) # previously typing.IO[Any], now typing.BinaryIO ``` You may be tempted into thinking this is some limitation of type checkers. mypy does in fact have logic for detecting if we match multiple overloads and union-ing up the return types of matched overloads. The problem is the last overload interferes with this logic. That is, if you remove the fallback overload (prior to this PR), you'd get "Union[io.BufferedReader, io.BufferedWriter]" in the above example.
- Loading branch information
hauntsaninja
committed
Mar 2, 2022
1 parent
a5ec3c6
commit 43223af
Showing
5 changed files
with
5 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters