Skip to content

Commit

Permalink
Remove incorrect usage of typing.AnyStr
Browse files Browse the repository at this point in the history
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 :)
  • Loading branch information
sobolevn committed Jul 21, 2023
1 parent b14f961 commit 9f4b244
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mitmproxy/tools/console/grideditor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from collections.abc import MutableSequence
from collections.abc import Sequence
from typing import Any
from typing import AnyStr
from typing import ClassVar
from typing import Literal
from typing import overload

import urwid

Expand All @@ -19,7 +20,15 @@
from mitmproxy.utils import strutils


def read_file(filename: str, escaped: bool) -> AnyStr:
@overload
def read_file(filename: str, escaped: Literal[True]) -> bytes:
...

@overload
def read_file(filename: str, escaped: Literal[False]) -> str:
...

def read_file(filename: str, escaped: bool) -> bytes | str:
filename = os.path.expanduser(filename)
try:
with open(filename, "r" if escaped else "rb") as f:
Expand Down

0 comments on commit 9f4b244

Please sign in to comment.