summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoofar <toofar@spalge.com>2023-12-03 11:58:59 +1300
committertoofar <toofar@spalge.com>2023-12-03 12:04:10 +1300
commit88c35c9df09534f482a0f10f9bf60e3d2c964d07 (patch)
tree2b854c31f75eeebb6714d2d67588d39ae278ea34
parent75c78cadc4c7391df4654798b4b6de0c96007597 (diff)
downloadqutebrowser-88c35c9df09534f482a0f10f9bf60e3d2c964d07.tar.gz
qutebrowser-88c35c9df09534f482a0f10f9bf60e3d2c964d07.zip
tweak savefile_open type hints
This was fixed up in https://github.com/qutebrowser/qutebrowser/pull/8006 after a mypy update caused us to examine the typing.AnyStr thing a bit more. But both overloads got set to have a `str` return type, so the possible bytes return type got lost. Mypy didn't pick that up because `binary=True` is only used in `qutebrowser/browser/webkit/cookies.py` which probably isn't being type checked. I had to remove the default from the binary arg of the bytes version (the ` = ...` bit) because if both overloads have the kwarg as optional mypy doesn't know which to match a call with just one positional argument against. Eg `savefile_open('some_file')` would match both. I checked with reveal_type: diff --git i/qutebrowser/utils/qtutils.py w/qutebrowser/utils/qtutils.py index 89175ca4ee60..5b86e441a1bc 100644 --- i/qutebrowser/utils/qtutils.py +++ w/qutebrowser/utils/qtutils.py @@ -290,6 +290,20 @@ def savefile_open( raise QtOSError(f, msg="Commit failed!") +def test_savefile_types() -> None: + from typing_extensions import reveal_type + + maybe_str_default = savefile_open("/tmp/string_file") + # note: Revealed type is "contextlib._GeneratorContextManager[typing.IO[builtins.str]]" + reveal_type(maybe_str_default) + maybe_str_explicit = savefile_open("/tmp/string_file", binary=False) + # note: Revealed type is "contextlib._GeneratorContextManager[typing.IO[builtins.str]]" + reveal_type(maybe_str_explicit) + maybe_bytes = savefile_open("/tmp/bytes_file", binary=True) + # note: Revealed type is "contextlib._GeneratorContextManager[typing.IO[builtins.bytes]]" + reveal_type(maybe_bytes) + + def qcolor_to_qsscolor(c: QColor) -> str: """Convert a QColor to a string that can be used in a QStyleSheet.""" ensure_valid(c)
-rw-r--r--qutebrowser/utils/qtutils.py4
1 files changed, 2 insertions, 2 deletions
diff --git a/qutebrowser/utils/qtutils.py b/qutebrowser/utils/qtutils.py
index 363d5607a..89175ca4e 100644
--- a/qutebrowser/utils/qtutils.py
+++ b/qutebrowser/utils/qtutils.py
@@ -250,9 +250,9 @@ def savefile_open(
@contextlib.contextmanager
def savefile_open(
filename: str,
- binary: Literal[True] = ...,
+ binary: Literal[True],
encoding: str = 'utf-8'
-) -> Iterator[IO[str]]:
+) -> Iterator[IO[bytes]]:
...