From 168963460f12e3696e76667e801d3ada95c1f323 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 3 Feb 2021 13:31:46 +1000 Subject: [PATCH] update to latest mypy_protobuf The handling of enum types has improved - we no longer need to import separate types at typechecking time. --- pip/requirements.txt | 4 ++-- pylib/anki/_backend/__init__.py | 13 +++++-------- pylib/anki/_backend/genbackend.py | 2 +- pylib/anki/collection.py | 20 +++++++------------- pylib/anki/lang.py | 7 +------ pylib/anki/rsbackend.py | 5 ----- pylib/anki/schedv2.py | 21 ++------------------- pylib/anki/stdmodels.py | 9 ++------- pylib/anki/syncserver/__init__.py | 2 +- pylib/tools/protoc-gen-mypy.py | 2 +- qt/aqt/sidebar.py | 19 +++---------------- qt/aqt/utils.py | 4 +--- 12 files changed, 26 insertions(+), 82 deletions(-) diff --git a/pip/requirements.txt b/pip/requirements.txt index b11261fa8..cbcf1aee2 100644 --- a/pip/requirements.txt +++ b/pip/requirements.txt @@ -72,7 +72,7 @@ mypy-extensions==0.4.3 # via # black # mypy -mypy-protobuf==1.24 +mypy-protobuf==2.1 # via -r requirements.in mypy==0.800 # via -r requirements.in @@ -156,7 +156,7 @@ wrapt==1.12.1 # The following packages are considered to be unsafe in a requirements file: pip==21.0.1 # via pip-tools -setuptools==52.0.0 +setuptools==53.0.0 # via jsonschema # manually added for now; ensure it and the earlier winrt are not removed on update diff --git a/pylib/anki/_backend/__init__.py b/pylib/anki/_backend/__init__.py index 981a5c5f5..445d031e7 100644 --- a/pylib/anki/_backend/__init__.py +++ b/pylib/anki/_backend/__init__.py @@ -4,22 +4,19 @@ from __future__ import annotations import os -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Union +from typing import Any, Dict, List, Optional, Sequence, Union import anki.buildinfo from anki._backend.generated import RustBackendGenerated from anki.dbproxy import Row as DBRow from anki.dbproxy import ValueForDB from anki.errors import backend_exception_to_pylib -from anki.lang import FormatTimeSpanContext +from anki.lang import TR, FormatTimeSpanContext from anki.utils import from_json_bytes, to_json_bytes from . import backend_pb2 as pb from . import rsbridge -if TYPE_CHECKING: - from anki.lang import FormatTimeSpanContextValue, TRValue - # pylint: disable=c-extension-no-member assert rsbridge.buildhash() == anki.buildinfo.buildhash @@ -85,13 +82,13 @@ class RustBackend(RustBackendGenerated): err.ParseFromString(err_bytes) raise backend_exception_to_pylib(err) - def translate(self, key: TRValue, **kwargs: Union[str, int, float]) -> str: + def translate(self, key: TR.V, **kwargs: Union[str, int, float]) -> str: return self.translate_string(translate_string_in(key, **kwargs)) def format_time_span( self, seconds: float, - context: FormatTimeSpanContextValue = FormatTimeSpanContext.INTERVALS, + context: FormatTimeSpanContext.V = FormatTimeSpanContext.INTERVALS, ) -> str: print( "please use col.format_timespan() instead of col.backend.format_time_span()" @@ -110,7 +107,7 @@ class RustBackend(RustBackendGenerated): def translate_string_in( - key: TRValue, **kwargs: Union[str, int, float] + key: TR.V, **kwargs: Union[str, int, float] ) -> pb.TranslateStringIn: args = {} for (k, v) in kwargs.items(): diff --git a/pylib/anki/_backend/genbackend.py b/pylib/anki/_backend/genbackend.py index 55ce2b76a..f8d60ee51 100755 --- a/pylib/anki/_backend/genbackend.py +++ b/pylib/anki/_backend/genbackend.py @@ -61,7 +61,7 @@ def python_type_inner(field): elif type == TYPE_MESSAGE: return fullname(field.message_type.full_name) elif type == TYPE_ENUM: - return fullname(field.enum_type.full_name) + "Value" + return fullname(field.enum_type.full_name) + ".V" else: raise Exception(f"unknown type: {type}") diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index d227391f2..9eb31b35e 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -13,7 +13,7 @@ import time import traceback import weakref from dataclasses import dataclass -from typing import TYPE_CHECKING, Any, List, Optional, Sequence, Tuple, Union +from typing import Any, List, Optional, Sequence, Tuple, Union import anki._backend.backend_pb2 as _pb import anki.find @@ -57,13 +57,6 @@ GraphPreferences = _pb.GraphPreferences BuiltinSortKind = _pb.SortOrder.Builtin.Kind # pylint: disable=no-member Preferences = _pb.Preferences -# pylint: disable=no-member -if TYPE_CHECKING: - from anki.lang import FormatTimeSpanContextValue, TRValue - - ConfigBoolKeyValue = _pb.ConfigBool.KeyValue - BuiltinSortKindValue = _pb.SortOrder.Builtin.KindValue - class Collection: sched: Union[V1Scheduler, V2Scheduler] @@ -118,13 +111,13 @@ class Collection: # I18n/messages ########################################################################## - def tr(self, key: TRValue, **kwargs: Union[str, int, float]) -> str: + def tr(self, key: TR.V, **kwargs: Union[str, int, float]) -> str: return self._backend.translate(key, **kwargs) def format_timespan( self, seconds: float, - context: FormatTimeSpanContextValue = FormatTimeSpanContext.INTERVALS, + context: FormatTimeSpanContext.V = FormatTimeSpanContext.INTERVALS, ) -> str: return self._backend.format_timespan(seconds=seconds, context=context) @@ -468,7 +461,7 @@ class Collection: def find_cards( self, query: str, - order: Union[bool, str, BuiltinSortKindValue] = False, + order: Union[bool, str, BuiltinSortKind.V] = False, reverse: bool = False, ) -> Sequence[int]: if isinstance(order, str): @@ -539,6 +532,7 @@ class Collection: # Search Strings ########################################################################## + # pylint: disable=no-member def build_search_string( self, *terms: Union[str, SearchTerm], @@ -592,10 +586,10 @@ class Collection: "This is a debugging aid. Prefer .get_config() when you know the key you need." return from_json_bytes(self._backend.get_all_config()) - def get_config_bool(self, key: ConfigBoolKeyValue) -> bool: + def get_config_bool(self, key: ConfigBoolKey.V) -> bool: return self._backend.get_config_bool(key) - def set_config_bool(self, key: ConfigBoolKeyValue, value: bool) -> None: + def set_config_bool(self, key: ConfigBoolKey.V, value: bool) -> None: self.setMod() self._backend.set_config_bool(key=key, value=value) diff --git a/pylib/anki/lang.py b/pylib/anki/lang.py index 6be31bbfd..0528e1682 100644 --- a/pylib/anki/lang.py +++ b/pylib/anki/lang.py @@ -5,7 +5,7 @@ from __future__ import annotations import locale import re -from typing import TYPE_CHECKING, Any, Optional, Tuple +from typing import Any, Optional, Tuple import anki import anki._backend.backend_pb2 as _pb @@ -15,11 +15,6 @@ import anki._backend.fluent_pb2 as _fluent_pb TR = _fluent_pb.FluentString FormatTimeSpanContext = _pb.FormatTimespanIn.Context # pylint: disable=no-member -# pylint: disable=no-member -if TYPE_CHECKING: - TRValue = _fluent_pb.FluentStringValue - FormatTimeSpanContextValue = _pb.FormatTimespanIn.ContextValue - langs = sorted( [ ("Afrikaans", "af_ZA"), diff --git a/pylib/anki/rsbackend.py b/pylib/anki/rsbackend.py index 75d33f695..9657a7731 100644 --- a/pylib/anki/rsbackend.py +++ b/pylib/anki/rsbackend.py @@ -7,11 +7,6 @@ # # pylint: disable=unused-import -from typing import TYPE_CHECKING - from anki.decks import DeckTreeNode from anki.errors import InvalidInput, NotFoundError from anki.lang import FormatTimeSpanContext - -if TYPE_CHECKING: - from anki.lang import FormatTimeSpanContextValue, TRValue diff --git a/pylib/anki/schedv2.py b/pylib/anki/schedv2.py index b470eb1dc..38b1276ce 100644 --- a/pylib/anki/schedv2.py +++ b/pylib/anki/schedv2.py @@ -7,17 +7,7 @@ import pprint import random import time from heapq import * -from typing import ( - TYPE_CHECKING, - Any, - Callable, - Dict, - List, - Optional, - Sequence, - Tuple, - Union, -) +from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union import anki # pylint: disable=unused-import import anki._backend.backend_pb2 as _pb @@ -35,13 +25,6 @@ SchedTimingToday = _pb.SchedTimingTodayOut UnburyCurrentDeckMode = _pb.UnburyCardsInCurrentDeckIn.Mode # pylint:disable=no-member BuryOrSuspendMode = _pb.BuryOrSuspendCardsIn.Mode # pylint:disable=no-member -if TYPE_CHECKING: - UnburyCurrentDeckModeValue = ( - _pb.UnburyCardsInCurrentDeckIn.ModeValue # pylint:disable=no-member - ) - BuryOrSuspendModeValue = ( - _pb.BuryOrSuspendCardsIn.ModeValue # pylint:disable=no-member - ) # card types: 0=new, 1=lrn, 2=rev, 3=relrn # queue types: 0=new, 1=(re)lrn, 2=rev, 3=day (re)lrn, @@ -1332,7 +1315,7 @@ due = (case when odue>0 then odue else due end), odue = 0, odid = 0, usn = ? whe def unbury_cards_in_current_deck( self, - mode: UnburyCurrentDeckModeValue = UnburyCurrentDeckMode.ALL, + mode: UnburyCurrentDeckMode.V = UnburyCurrentDeckMode.ALL, ) -> None: self.col._backend.unbury_cards_in_current_deck(mode) diff --git a/pylib/anki/stdmodels.py b/pylib/anki/stdmodels.py index f51dab968..8fcecdb72 100644 --- a/pylib/anki/stdmodels.py +++ b/pylib/anki/stdmodels.py @@ -3,7 +3,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Callable, List, Tuple +from typing import Callable, List, Tuple import anki import anki._backend.backend_pb2 as _pb @@ -12,18 +12,13 @@ from anki.utils import from_json_bytes # pylint: disable=no-member StockNotetypeKind = _pb.StockNoteType.Kind -# pylint: disable=no-member -if TYPE_CHECKING: - StockNotetypeKindValue = _pb.StockNoteType.KindValue - - # add-on authors can add ("note type name", function_like_addBasicModel) # to this list to have it shown in the add/clone note type screen models: List[Tuple] = [] def _add_stock_notetype( - col: anki.collection.Collection, kind: StockNotetypeKindValue + col: anki.collection.Collection, kind: StockNotetypeKind.V ) -> anki.models.NoteType: m = from_json_bytes(col._backend.get_stock_notetype_legacy(kind)) col.models.add(m) diff --git a/pylib/anki/syncserver/__init__.py b/pylib/anki/syncserver/__init__.py index aff0a35e0..dc16a2cde 100644 --- a/pylib/anki/syncserver/__init__.py +++ b/pylib/anki/syncserver/__init__.py @@ -116,7 +116,7 @@ def after_full_sync() -> None: def get_method( method_str: str, -) -> Optional[SyncServerMethodIn.MethodValue]: # pylint: disable=no-member +) -> Optional[SyncServerMethodIn.Method.V]: # pylint: disable=no-member s = method_str if s == "hostKey": return Method.HOST_KEY diff --git a/pylib/tools/protoc-gen-mypy.py b/pylib/tools/protoc-gen-mypy.py index 1ab0d9408..ba2374a5d 100755 --- a/pylib/tools/protoc-gen-mypy.py +++ b/pylib/tools/protoc-gen-mypy.py @@ -2,7 +2,7 @@ import re import sys -from mypy_protobuf import main +from mypy_protobuf.main import main if __name__ == "__main__": sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) diff --git a/qt/aqt/sidebar.py b/qt/aqt/sidebar.py index 73d013513..8c36e5411 100644 --- a/qt/aqt/sidebar.py +++ b/qt/aqt/sidebar.py @@ -6,17 +6,7 @@ from __future__ import annotations from concurrent.futures import Future from enum import Enum -from typing import ( - TYPE_CHECKING, - Any, - Dict, - Iterable, - List, - Optional, - Sequence, - Tuple, - cast, -) +from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, cast import aqt from anki.collection import ConfigBoolKey, SearchTerm @@ -38,9 +28,6 @@ from aqt.utils import ( tr, ) -if TYPE_CHECKING: - from anki.collection import ConfigBoolKeyValue, TRValue - class SidebarItemType(Enum): ROOT = 0 @@ -512,9 +499,9 @@ class SidebarTreeView(QTreeView): self, *, root: SidebarItem, - name: TRValue, + name: TR.V, icon: str, - collapse_key: ConfigBoolKeyValue, + collapse_key: ConfigBoolKey.V, type: Optional[SidebarItemType] = None, ) -> SidebarItem: def update(expanded: bool) -> None: diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index fe855fd6f..fd7dc6f47 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -44,8 +44,6 @@ from aqt.qt import * from aqt.theme import theme_manager if TYPE_CHECKING: - from anki.lang import TRValue - TextFormat = Union[Literal["plain", "rich"]] @@ -68,7 +66,7 @@ def locale_dir() -> str: return os.path.join(aqt_data_folder(), "locale") -def tr(key: TRValue, **kwargs: Union[str, int, float]) -> str: +def tr(key: TR.V, **kwargs: Union[str, int, float]) -> str: "Shortcut to access Fluent translations." return anki.lang.current_i18n.translate(key, **kwargs)