From 9f4b2440080d8f79089e024445c00c56533814e6 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Fri, 21 Jul 2023 10:52:52 +0300 Subject: [PATCH] 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 :) --- mitmproxy/tools/console/grideditor/base.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mitmproxy/tools/console/grideditor/base.py b/mitmproxy/tools/console/grideditor/base.py index 843b932f94..a58bb463cd 100644 --- a/mitmproxy/tools/console/grideditor/base.py +++ b/mitmproxy/tools/console/grideditor/base.py @@ -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 @@ -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: