Skip to content

Commit

Permalink
Add basedmypy 2.5 compatibility (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Jun 21, 2024
1 parent 2d6540e commit 4f4e092
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 30 deletions.
2 changes: 1 addition & 1 deletion betty/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ async def _shutdown_default_executor(
raise


BaseEventLoop.shutdown_default_executor = _shutdown_default_executor # type: ignore[assignment, method-assign]
BaseEventLoop.shutdown_default_executor = _shutdown_default_executor # type: ignore[assignment, callable-functiontype, method-assign, misc]
2 changes: 1 addition & 1 deletion betty/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def __eq__(self, other: Any) -> bool:
return False
return True

@override
@override # type: ignore[callable-functiontype]
@recursive_repr()
def __repr__(self) -> str:
return repr_instance(self, configurations=list(self.values()))
Expand Down
8 changes: 5 additions & 3 deletions betty/extension/cotton_candy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
from collections import defaultdict
from pathlib import Path
from typing import Any, Callable, Iterable, Self, cast, TYPE_CHECKING
from typing import Iterable, Self, cast, TYPE_CHECKING

from jinja2 import pass_context
from typing_extensions import override
Expand All @@ -26,6 +26,8 @@
context_app,
context_localizer,
context_job_context,
Globals,
Filters,
)
from betty.locale import Date, Str, Datey
from betty.model import Entity, UserFacingEntity, GeneratedEntityId
Expand Down Expand Up @@ -353,14 +355,14 @@ def gui_build(self) -> QWidget:

@override
@property
def globals(self) -> dict[str, Any]:
def globals(self) -> Globals:
return {
"search_index": _global_search_index,
}

@override
@property
def filters(self) -> dict[str, Callable[..., Any]]:
def filters(self) -> Filters:
return {
"person_timeline_events": lambda person: person_timeline_events(
person, self.app.project.configuration.lifetime_threshold
Expand Down
10 changes: 5 additions & 5 deletions betty/extension/webpack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from asyncio import to_thread
from pathlib import Path
from shutil import copytree
from typing import Any, TYPE_CHECKING
from typing import TYPE_CHECKING

from aiofiles.tempfile import TemporaryDirectory
from typing_extensions import override
Expand All @@ -23,7 +23,7 @@
from betty.extension.webpack.jinja2.filter import FILTERS
from betty.generate import Generator, GenerationContext
from betty.html import CssProvider
from betty.jinja2 import Jinja2Provider
from betty.jinja2 import Jinja2Provider, Filters, ContextVars
from betty.job import Context
from betty.locale import Str
from betty.requirement import (
Expand All @@ -34,7 +34,7 @@
)

if TYPE_CHECKING:
from collections.abc import Callable, Sequence
from collections.abc import Sequence


def _prebuilt_webpack_build_directory_path(
Expand Down Expand Up @@ -147,14 +147,14 @@ def public_css_paths(self) -> list[str]:
]

@override
def new_context_vars(self) -> dict[str, Any]:
def new_context_vars(self) -> ContextVars:
return {
"webpack_js_entry_points": set(),
}

@override
@property
def filters(self) -> dict[str, Callable[..., Any]]:
def filters(self) -> Filters:
return FILTERS

@property
Expand Down
3 changes: 2 additions & 1 deletion betty/extension/webpack/jinja2/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from betty.jinja2 import Filters
from jinja2.runtime import Context


Expand All @@ -23,6 +24,6 @@ def filter_webpack_entry_point_js(context: Context, entry_point_name: str) -> No
_context_js_entry_points(context).add(entry_point_name)


FILTERS = {
FILTERS: Filters = {
"webpack_entry_point_js": filter_webpack_entry_point_js,
}
6 changes: 3 additions & 3 deletions betty/extension/wikipedia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
from pathlib import Path
from typing import Callable, Iterable, Any, TYPE_CHECKING
from typing import Iterable, Any, TYPE_CHECKING

from jinja2 import pass_context
from typing_extensions import override
Expand All @@ -14,7 +14,7 @@
from betty.extension.wikipedia.config import WikipediaConfiguration
from betty.extension.wikipedia.gui import _WikipediaGuiWidget
from betty.gui import GuiBuilder
from betty.jinja2 import Jinja2Provider, context_localizer
from betty.jinja2 import Jinja2Provider, context_localizer, Filters
from betty.load import PostLoader
from betty.locale import negotiate_locale, Str
from betty.wikipedia import (
Expand Down Expand Up @@ -69,7 +69,7 @@ def _retriever(self) -> None:

@override
@property
def filters(self) -> dict[str, Callable[..., Any]]:
def filters(self) -> Filters:
return {
"wikipedia": self._filter_wikipedia_links,
}
Expand Down
18 changes: 12 additions & 6 deletions betty/jinja2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import datetime
from collections import defaultdict
from threading import Lock
from typing import Callable, Any, cast, TypeVar, TYPE_CHECKING
from typing import Callable, Any, cast, TypeVar, TYPE_CHECKING, TypeAlias

import aiofiles
from aiofiles import os as aiofiles_os
Expand Down Expand Up @@ -161,13 +161,19 @@ def __call__(self, *entities: Entity) -> EntityContexts:
return updated_contexts


Globals: TypeAlias = dict[str, Any]
Filters: TypeAlias = dict[str, Callable[..., Any]]
Tests: TypeAlias = dict[str, Callable[..., bool]]
ContextVars: TypeAlias = dict[str, Any]


class Jinja2Provider:
"""
Integrate an :py:class:`betty.app.extension.Extension` with the Jinja2 API.
"""

@property
def globals(self) -> dict[str, Any]:
def globals(self) -> Globals:
"""
Jinja2 globals provided by this extension.
Expand All @@ -176,7 +182,7 @@ def globals(self) -> dict[str, Any]:
return {}

@property
def filters(self) -> dict[str, Callable[..., Any]]:
def filters(self) -> Filters:
"""
Jinja2 filters provided by this extension.
Expand All @@ -185,15 +191,15 @@ def filters(self) -> dict[str, Callable[..., Any]]:
return {}

@property
def tests(self) -> dict[str, Callable[..., bool]]:
def tests(self) -> Tests:
"""
Jinja2 tests provided by this extension.
Keys are test names, and values are the tests themselves.
"""
return {}

def new_context_vars(self) -> dict[str, Any]:
def new_context_vars(self) -> ContextVars:
"""
Create new variables for a new :py:class:`jinja2.runtime.Context`.
Expand All @@ -209,7 +215,7 @@ class Environment(Jinja2Environment):

globals: dict[str, Any]
filters: dict[str, Callable[..., Any]]
tests: dict[str, Callable[..., bool]]
tests: dict[str, Callable[..., bool]] # type: ignore[assignment]

def __init__(self, app: App):
template_directory_paths = [
Expand Down
6 changes: 3 additions & 3 deletions betty/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def entity_type_label_plural(cls) -> Str:
"""
raise NotImplementedError(repr(cls))

@override
@override # type: ignore[callable-functiontype]
@recursive_repr()
def __repr__(self) -> str:
return repr_instance(self, id=self._id)
Expand Down Expand Up @@ -945,7 +945,7 @@ def __init__(
self._entities: list[TargetT & Entity] = []
self._target_type = target_type

@override
@override # type: ignore[callable-functiontype]
@recursive_repr()
def __repr__(self) -> str:
return repr_instance(self, target_type=self._target_type, length=len(self))
Expand Down Expand Up @@ -1062,7 +1062,7 @@ def __init__(self):
super().__init__()
self._collections: dict[type[Entity], SingleTypeEntityCollection[Entity]] = {}

@override
@override # type: ignore[callable-functiontype]
@recursive_repr()
def __repr__(self) -> str:
return repr_instance(
Expand Down
4 changes: 2 additions & 2 deletions betty/model/ancestry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ def __eq__(self, other: Any) -> bool:
return NotImplemented # pragma: no cover
return self._name == other._name and self.locale == other.locale

@override
@override # type: ignore[callable-functiontype]
@recursive_repr()
def __repr__(self) -> str:
return repr_instance(self, name=self.name, locale=self.locale)
Expand Down Expand Up @@ -1838,7 +1838,7 @@ def label(self) -> Str:
else:
return Str._("{event_type} ({event_description})", **format_kwargs)

@override
@override # type: ignore[callable-functiontype]
@recursive_repr()
def __repr__(self) -> str:
return repr_instance(self, id=self._id, type=self._event_type)
Expand Down
2 changes: 1 addition & 1 deletion betty/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def __init__(
raise AssertionFailed(Str._("Locale aliases must not contain slashes."))
self._alias = alias

@override
@override # type: ignore[callable-functiontype]
@recursive_repr()
def __repr__(self) -> str:
return repr_instance(self, locale=self.locale, alias=self.alias)
Expand Down
2 changes: 1 addition & 1 deletion betty/tests/coverage/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ def _get_members(
class _ModuleFunctionCoverageTester:
def __init__(
self,
src_function: Callable[..., Any],
src_function: Callable[..., Any] & _Importable,
test_classes: Sequence[type],
src_module_name: str,
test_module_name: str,
Expand Down
4 changes: 2 additions & 2 deletions betty/tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ async def test_call_decorated_callable_coroutinemethod_should_return_result(
with pytest.warns(BettyDeprecationWarning):

class _Sync:
@sync
@sync # type: ignore[callable-functiontype]
async def __call__(self, *args: Any, **kwargs: Any) -> str:
return expected

actual = _Sync()()
actual = _Sync()() # type: ignore[call-arg]
assert expected == actual

async def test_call_wrapped_coroutinecallable_object_should_return_result(
Expand Down
2 changes: 1 addition & 1 deletion betty/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def showEvent(window_self: BettyPrimaryWindow, a0: QShowEvent | None) -> None:
timer.timeout.connect(window_self.close)
timer.start(0)

BettyPrimaryWindow.showEvent = showEvent # type: ignore[assignment, method-assign]
BettyPrimaryWindow.showEvent = showEvent # type: ignore[assignment, callable-functiontype, method-assign, misc]
run(*args)

async def test_without_project(
Expand Down

0 comments on commit 4f4e092

Please sign in to comment.