summaryrefslogtreecommitdiff
path: root/qutebrowser/utils/utils.py
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2020-11-20 11:32:38 +0100
committerFlorian Bruhin <me@the-compiler.org>2020-11-20 11:32:38 +0100
commite07003ee5f99be9928475ab194e6eb9e36ccc8d0 (patch)
treeb22cba55ae75960cd0324f8a76b18ad796232cb9 /qutebrowser/utils/utils.py
parentf083e3801b3aa4cefd70e1a61c034b3fc7684271 (diff)
downloadqutebrowser-e07003ee5f99be9928475ab194e6eb9e36ccc8d0.tar.gz
qutebrowser-e07003ee5f99be9928475ab194e6eb9e36ccc8d0.zip
Simplify compat code around typing.Protocol
Protocol is unavailable in Python < 3.8 - but since it doesn't do anything at runtime, we can just stub it by using an empty class instead. That way, we need less conditionals and string type annotations because we can just pretend it exists at runtime.
Diffstat (limited to 'qutebrowser/utils/utils.py')
-rw-r--r--qutebrowser/utils/utils.py23
1 files changed, 14 insertions, 9 deletions
diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py
index be4c35b0a..1652567af 100644
--- a/qutebrowser/utils/utils.py
+++ b/qutebrowser/utils/utils.py
@@ -67,20 +67,25 @@ is_windows = sys.platform.startswith('win')
is_posix = os.name == 'posix'
-if TYPE_CHECKING:
+try:
# Protocol was added in Python 3.8
from typing import Protocol
+except ImportError:
+ if not TYPE_CHECKING:
+ class Protocol:
+ pass
+
- class SupportsLessThan(Protocol):
+class SupportsLessThan(Protocol):
- """Protocol for the _T TypeVar below."""
+ """Protocol for the _T TypeVar below."""
- def __lt__(self, other: Any) -> bool:
- ...
+ def __lt__(self, other: Any) -> bool:
+ ...
- class VersionNumber(SupportsLessThan, QVersionNumber):
+class VersionNumber(SupportsLessThan, QVersionNumber):
- """WORKAROUND for incorrect PyQt stubs."""
+ """WORKAROUND for incorrect PyQt stubs."""
class Unreachable(Exception):
@@ -227,10 +232,10 @@ def resource_filename(filename: str) -> str:
return pkg_resources.resource_filename(qutebrowser.__name__, filename)
-def parse_version(version: str) -> 'VersionNumber':
+def parse_version(version: str) -> VersionNumber:
"""Parse a version string."""
v_q, _suffix = QVersionNumber.fromString(version)
- return cast('VersionNumber', v_q.normalized())
+ return cast(VersionNumber, v_q.normalized())
def format_seconds(total_seconds: int) -> str: