update to latest mypy_protobuf

The handling of enum types has improved - we no longer need to import
separate types at typechecking time.
This commit is contained in:
Damien Elmes 2021-02-03 13:31:46 +10:00
parent 86f8cba22c
commit 168963460f
12 changed files with 26 additions and 82 deletions

View File

@ -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

View File

@ -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():

View File

@ -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}")

View File

@ -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)

View File

@ -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"),

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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])

View File

@ -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:

View File

@ -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)