Skip to content

Commit

Permalink
Remove incorrect usage of typing.AnyStr (mitmproxy#6271)
Browse files Browse the repository at this point in the history
* 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 :)

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and lasting-yang committed Aug 2, 2023
1 parent 37b118a commit 5f49273
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 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,17 @@
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 5f49273

Please sign in to comment.