summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoofar <toofar@spalge.com>2024-03-10 22:25:34 +1300
committertoofar <toofar@spalge.com>2024-03-10 22:42:36 +1300
commite4c2795c9fe2ae84f010659d9e775a5379c6d8f1 (patch)
tree47f4162d33f496c989014c0f72b78aa849b4174a
parent269c4fe3132b66ba4d09fbca5c249ac0a3359e9d (diff)
downloadqutebrowser-e4c2795c9fe2ae84f010659d9e775a5379c6d8f1.tar.gz
qutebrowser-e4c2795c9fe2ae84f010659d9e775a5379c6d8f1.zip
Make dataclass inheritance with defaults work with py<310
I've made TreeUndoEntry a subclass of UndoEntry. But UndoEntry sets a default value for the `created_at` attribute, which means if any of the extra attributes defined on the child class don't define a default an error is thrown complaining about non-keyword arguments after a keyword only one. Python 3.10 adds a kw_only argument you can pass to the dataclass decorator but it looks like we currently support python 3.8+. See https://www.trueblade.com/blogs/news/python-3-10-new-dataclass-features So a more compatible way to do it is to set `init=False` on the attribute with a default, if you don't need to pass it in the constructor. Which we don't because it's only set by the caller in tests. Hence changing the tests to set it a different way. A bit uglier but should be fine. Also fix my "retains_sort_order" test from #5964 that wasn't actually being run...
-rw-r--r--qutebrowser/mainwindow/tabbedbrowser.py6
-rw-r--r--qutebrowser/mainwindow/treetabbedbrowser.py2
-rw-r--r--tests/unit/completion/test_models.py32
3 files changed, 23 insertions, 17 deletions
diff --git a/qutebrowser/mainwindow/tabbedbrowser.py b/qutebrowser/mainwindow/tabbedbrowser.py
index 469c70c7a..b006ef892 100644
--- a/qutebrowser/mainwindow/tabbedbrowser.py
+++ b/qutebrowser/mainwindow/tabbedbrowser.py
@@ -24,7 +24,7 @@ from qutebrowser.utils import (log, usertypes, utils, qtutils,
from qutebrowser.misc import quitter, objects
-@dataclasses.dataclass(kw_only=True)
+@dataclasses.dataclass
class _UndoEntry:
"""Information needed for :undo."""
@@ -34,7 +34,9 @@ class _UndoEntry:
index: int
pinned: bool
created_at: datetime.datetime = dataclasses.field(
- default_factory=datetime.datetime.now)
+ default_factory=datetime.datetime.now,
+ init=False, # WORKAROUND until py3.10 with kw_only: https://www.trueblade.com/blogs/news/python-3-10-new-dataclass-features
+ )
def restore_into_tab(self, tab: browsertab.AbstractTab):
"""Set the url, history and state of `tab` from this undo entry."""
diff --git a/qutebrowser/mainwindow/treetabbedbrowser.py b/qutebrowser/mainwindow/treetabbedbrowser.py
index d078ff782..49d88d7a5 100644
--- a/qutebrowser/mainwindow/treetabbedbrowser.py
+++ b/qutebrowser/mainwindow/treetabbedbrowser.py
@@ -16,7 +16,7 @@ from qutebrowser.browser import browsertab
from qutebrowser.misc import notree
-@dataclasses.dataclass(kw_only=True)
+@dataclasses.dataclass
class _TreeUndoEntry(_UndoEntry):
"""Information needed for :undo."""
diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py
index e0bce8f04..7c51a6a62 100644
--- a/tests/unit/completion/test_models.py
+++ b/tests/unit/completion/test_models.py
@@ -1410,14 +1410,15 @@ def test_forward_completion(tab_with_history, info):
def test_undo_completion(tabbed_browser_stubs, info):
"""Test :undo completion."""
entry1 = tabbedbrowser._UndoEntry(url=QUrl('https://example.org/'),
- history=None, index=None, pinned=None,
- created_at=datetime(2020, 1, 1))
+ history=None, index=None, pinned=None)
entry2 = tabbedbrowser._UndoEntry(url=QUrl('https://example.com/'),
- history=None, index=None, pinned=None,
- created_at=datetime(2020, 1, 2))
+ history=None, index=None, pinned=None)
entry3 = tabbedbrowser._UndoEntry(url=QUrl('https://example.net/'),
- history=None, index=None, pinned=None,
- created_at=datetime(2020, 1, 2))
+ history=None, index=None, pinned=None)
+
+ entry1.created_at = datetime(2020, 1, 1)
+ for entry in [entry2, entry3]:
+ entry.created_at = datetime(2020, 1, 2)
# Most recently closed is at the end
tabbed_browser_stubs[0].undo_stack = [
@@ -1442,19 +1443,22 @@ def test_undo_completion(tabbed_browser_stubs, info):
})
-def undo_completion_retains_sort_order(tabbed_browser_stubs, info):
+def test_undo_completion_retains_sort_order(tabbed_browser_stubs, info):
"""Test :undo completion sort order with > 10 entries."""
created_dt = datetime(2020, 1, 1)
- created_str = "2020-01-02 00:00"
+ created_str = "2020-01-01 00:00"
tabbed_browser_stubs[0].undo_stack = [
- tabbedbrowser._UndoEntry(
- url=QUrl(f'https://example.org/{idx}'),
- history=None, index=None, pinned=None,
- created_at=created_dt,
- )
- for idx in range(1, 11)
+ [
+ tabbedbrowser._UndoEntry(
+ url=QUrl(f'https://example.org/{idx}'),
+ history=None, index=None, pinned=None,
+ )
+ ]
+ for idx in reversed(range(1, 11))
]
+ for entries in tabbed_browser_stubs[0].undo_stack:
+ entries[0].created_at = created_dt
model = miscmodels.undo(info=info)
model.set_pattern('')