Skip to content

Commit 9f4b244

Browse files
authored
Remove incorrect usage of typing.AnyStr
Hi! While working on python/mypy#15732 our tools detected a misuse of AnyStr TypeVar (which is quite common). The proper way here is to use `@overload`s :)
1 parent b14f961 commit 9f4b244

File tree

1 file changed

+11
-2
lines changed
  • mitmproxy/tools/console/grideditor

1 file changed

+11
-2
lines changed

mitmproxy/tools/console/grideditor/base.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
from collections.abc import MutableSequence
88
from collections.abc import Sequence
99
from typing import Any
10-
from typing import AnyStr
1110
from typing import ClassVar
11+
from typing import Literal
12+
from typing import overload
1213

1314
import urwid
1415

@@ -19,7 +20,15 @@
1920
from mitmproxy.utils import strutils
2021

2122

22-
def read_file(filename: str, escaped: bool) -> AnyStr:
23+
@overload
24+
def read_file(filename: str, escaped: Literal[True]) -> bytes:
25+
...
26+
27+
@overload
28+
def read_file(filename: str, escaped: Literal[False]) -> str:
29+
...
30+
31+
def read_file(filename: str, escaped: bool) -> bytes | str:
2332
filename = os.path.expanduser(filename)
2433
try:
2534
with open(filename, "r" if escaped else "rb") as f:

0 commit comments

Comments
 (0)