Skip to content

Commit

Permalink
Fix typing violations.
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra committed Jun 19, 2022
1 parent 9760255 commit b27fd8c
Show file tree
Hide file tree
Showing 49 changed files with 990 additions and 707 deletions.
60 changes: 36 additions & 24 deletions betty/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from betty.resource import Releaser, Acquirer

try:
from typing import Self
from typing import Self # type: ignore
except ImportError:
from typing_extensions import Self

Expand All @@ -30,13 +30,10 @@
Occupation, Retirement, Correspondence, Confirmation
from betty.project import Project

if TYPE_CHECKING:
from betty.url import StaticUrlGenerator, ContentNegotiationUrlGenerator

try:
from graphlib import TopologicalSorter, CycleError
except ImportError:
from graphlib_backport import TopologicalSorter
from graphlib_backport import TopologicalSorter # type: ignore

import aiohttp
from jinja2 import Environment as Jinja2Environment
Expand All @@ -55,6 +52,11 @@
from betty.locale import negotiate_locale, TranslationsRepository, Translations, rfc_1766_to_bcp_47, bcp_47_to_rfc_1766, \
getdefaultlocale


if TYPE_CHECKING:
from betty.builtins import _
from betty.url import StaticUrlGenerator, ContentNegotiationUrlGenerator

CONFIGURATION_DIRECTORY_PATH = HOME_DIRECTORY_PATH / 'configuration'


Expand All @@ -77,9 +79,17 @@ def __init__(self):
def configuration_file_path(self) -> Path:
return CONFIGURATION_DIRECTORY_PATH / 'app.json'

@configuration_file_path.setter
def configuration_file_path(self, __) -> None:
pass

@configuration_file_path.deleter
def configuration_file_path(self) -> None:
pass

@reactive # type: ignore
@property
def locale(self) -> str:
def locale(self) -> Optional[str]:
if self._locale is None:
return getdefaultlocale()
return self._locale
Expand Down Expand Up @@ -120,7 +130,8 @@ def __init__(self, *args, **kwargs):
self.configuration.read()

self._acquired = False
self._extensions = None
self._extensions = _AppExtensions()
self._extensions_initialized = False
self._project = Project()
self._assets = FileSystem()
self._dispatcher = None
Expand Down Expand Up @@ -165,7 +176,7 @@ def acquire(self) -> None:
if isinstance(extension, Acquirer):
extension.acquire()
if isinstance(extension, Releaser):
self._activation_exit_stack.push(extension.release)
self._acquire_contexts.callback(extension.release)
except BaseException:
self.release()
raise
Expand Down Expand Up @@ -231,8 +242,8 @@ def project(self) -> Project:

@property
def extensions(self) -> Extensions:
if self._extensions is None:
self._extensions = _AppExtensions()
if not self._extensions_initialized:
self._extensions_initialized = True
self._update_extensions()
self.project.configuration.extensions.react(self._update_extensions)

Expand All @@ -259,15 +270,15 @@ def _update_extensions(self) -> None:
extensions_batch = []
for extension_type in extension_types_batch:
if issubclass(extension_type, ConfigurableExtension) and extension_type in self.project.configuration.extensions:
extension = extension_type(self, configuration=self.project.configuration.extensions[extension_type].extension_configuration)
extension: Extension = extension_type(self, configuration=self.project.configuration.extensions[extension_type].extension_configuration)
else:
extension = extension_type(self)
extensions_batch.append(extension)
extension_types_sorter.done(extension_type)
extensions.append(extensions_batch)
self._extensions._update(extensions)

@reactive
@reactive # type: ignore
@property
def assets(self) -> FileSystem:
if len(self._assets) == 0:
Expand All @@ -284,8 +295,9 @@ def assets(self) -> None:
def _build_assets(self) -> None:
self._assets.prepend(ASSETS_DIRECTORY_PATH, 'utf-8')
for extension in self.extensions.flatten():
if extension.assets_directory_path() is not None:
self._assets.prepend(extension.assets_directory_path(), 'utf-8')
extension_assets_directory_path = extension.assets_directory_path()
if extension_assets_directory_path is not None:
self._assets.prepend(extension_assets_directory_path, 'utf-8')
if self.project.configuration.assets_directory_path:
self._assets.prepend(self.project.configuration.assets_directory_path)

Expand All @@ -308,7 +320,7 @@ def static_url_generator(self) -> StaticUrlGenerator:
def translations(self) -> TranslationsRepository:
return self._translations

@reactive
@reactive # type: ignore
@property
def jinja2_environment(self) -> Jinja2Environment:
if not self._jinja2_environment:
Expand All @@ -321,7 +333,7 @@ def jinja2_environment(self) -> Jinja2Environment:
def jinja2_environment(self) -> None:
self._jinja2_environment = None

@reactive
@reactive # type: ignore
@property
def renderer(self) -> Renderer:
if not self._renderer:
Expand All @@ -347,22 +359,22 @@ def executor(self) -> Executor:
def locks(self) -> Locks:
return self._locks

@reactive
@reactive # type: ignore
@property
def http_client(self) -> aiohttp.ClientSession:
if not self._http_client:
self._http_client = aiohttp.ClientSession(connector=aiohttp.TCPConnector(limit_per_host=5))
weakref.finalize(self, sync(self._http_client.close))
return self._http_client

@http_client.deleter
@http_client.deleter # type: ignore
@sync
async def http_client(self) -> None:
if self._http_client is not None:
await self._http_client.close()
self._http_client = None

@reactive
@reactive # type: ignore
@property
@sync
async def entity_types(self) -> Set[Type[Entity]]:
Expand All @@ -381,7 +393,11 @@ async def entity_types(self) -> Set[Type[Entity]]:
}
return self._entity_types

@reactive
@entity_types.deleter
def entity_types(self) -> None:
self._entity_types = None

@reactive # type: ignore
@property
@sync
async def event_types(self) -> Set[Type[EventType]]:
Expand Down Expand Up @@ -409,7 +425,3 @@ async def event_types(self) -> Set[Type[EventType]]:
Confirmation,
}
return self._event_types

@entity_types.deleter
def entity_types(self) -> None:
self._entity_types = None
29 changes: 18 additions & 11 deletions betty/app/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from collections import defaultdict
from importlib.metadata import entry_points
from pathlib import Path
from typing import Type, Set, Optional, Any, List, Dict, Sequence, TypeVar, Union, Iterable, TYPE_CHECKING, Generic
from typing import Type, Set, Optional, Any, List, Dict, TypeVar, Union, Iterable, TYPE_CHECKING, Generic, \
Iterator

from reactives.factory.type import ReactiveInstance

Expand All @@ -30,7 +31,7 @@ class Dependencies(AllRequirements):
def __init__(self, extension_type: Type[Extension]):
for dependency in extension_type.depends_on():
try:
dependency_requirements = [dependency.requires() for dependency in extension_type.depends_on()]
dependency_requirements = tuple(dependency.requires() for dependency in extension_type.depends_on())
except RecursionError:
raise CyclicDependencyError([dependency])
super().__init__(dependency_requirements)
Expand All @@ -54,7 +55,7 @@ def __init__(self, app: App, *args, **kwargs):

@classmethod
def requires(cls) -> AllRequirements:
return AllRequirements([Dependencies(cls)] if cls.depends_on() else [])
return AllRequirements((Dependencies(cls),) if cls.depends_on() else ())

@classmethod
def name(cls) -> str:
Expand Down Expand Up @@ -105,14 +106,13 @@ class Extensions(ReactiveInstance):
def __getitem__(self, extension_type: Union[Type[ExtensionT], str]) -> ExtensionT:
raise NotImplementedError

def __iter__(self) -> Sequence[Sequence[Extension]]:
def __iter__(self) -> Iterator[Iterator[Extension]]:
raise NotImplementedError

def flatten(self) -> Sequence[Extension]:
for batch in self:
yield from batch
def flatten(self) -> Iterator[Extension]:
raise NotImplementedError

def __contains__(self, extension_type: Union[Type[Extension], str]) -> bool:
def __contains__(self, extension_type: Union[Type[Extension], str, Any]) -> bool:
raise NotImplementedError


Expand All @@ -131,11 +131,15 @@ def __getitem__(self, extension_type: Union[Type[Extension], str]) -> Extension:
raise KeyError(f'Unknown extension of type "{extension_type}"')

@scope.register_self
def __iter__(self) -> Sequence[Sequence[Extension]]:
def __iter__(self) -> Iterator[Iterator[Extension]]:
# Use a generator so we discourage calling code from storing the result.
for batch in self._extensions:
yield (extension for extension in batch)

def flatten(self) -> Iterator[Extension]:
for batch in self:
yield from batch

@scope.register_self
def __contains__(self, extension_type: Union[Type[Extension], str]) -> bool:
if isinstance(extension_type, str):
Expand Down Expand Up @@ -174,8 +178,11 @@ async def _dispatch(*args, **kwargs) -> List[Any]:
return _dispatch


def build_extension_type_graph(extension_types: Iterable[Type[Extension]]) -> Dict:
extension_types_graph = defaultdict(set)
ExtensionTypeGraph = Dict[Type[Extension], Set[Type[Extension]]]


def build_extension_type_graph(extension_types: Iterable[Type[Extension]]) -> ExtensionTypeGraph:
extension_types_graph: ExtensionTypeGraph = defaultdict(set)
# Add dependencies to the extension graph.
for extension_type in extension_types:
_extend_extension_type_graph(extension_types_graph, extension_type)
Expand Down
5 changes: 4 additions & 1 deletion betty/assets/betty.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Betty VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2022-05-16 23:56+0100\n"
"POT-Creation-Date: 2022-06-01 16:02+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -461,6 +461,9 @@ msgstr ""
msgid "Locale"
msgstr ""

msgid "Locale aliases must not contain slashes."
msgstr ""

msgid "Locales configuration must be a list."
msgstr ""

Expand Down
5 changes: 4 additions & 1 deletion betty/assets/locale/fr_FR/LC_MESSAGES/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2022-05-16 23:56+0100\n"
"POT-Creation-Date: 2022-06-01 16:02+0100\n"
"PO-Revision-Date: 2020-11-27 19:49+0100\n"
"Last-Translator: \n"
"Language: fr\n"
Expand Down Expand Up @@ -550,6 +550,9 @@ msgstr ""
msgid "Locale"
msgstr "Un nom de language."

msgid "Locale aliases must not contain slashes."
msgstr ""

msgid "Locales configuration must be a list."
msgstr ""

Expand Down
23 changes: 13 additions & 10 deletions betty/assets/locale/nl_NL/LC_MESSAGES/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2022-05-16 23:56+0100\n"
"POT-Creation-Date: 2022-06-01 16:02+0100\n"
"PO-Revision-Date: 2022-04-08 01:58+0100\n"
"Last-Translator: \n"
"Language: nl\n"
Expand All @@ -27,9 +27,11 @@ msgid ""
" Betty can add to your site, such as media galleries, "
"maps, and browsable family trees.\n"
" "
msgstr "Betty is een programma dat van een stamboom een website bouwt zoals de site die je nu aan het bezoeken bent."
"Des te meer informatie je genealogisch onderzoek bevat, des te meer interactiviteit Betty aan je site toe kan voegen,"
"zoals mediagalerijen, kaarten, en visuele stambomen."
msgstr ""
"Betty is een programma dat van een stamboom een website bouwt zoals de "
"site die je nu aan het bezoeken bent.Des te meer informatie je "
"genealogisch onderzoek bevat, des te meer interactiviteit Betty aan je "
"site toe kan voegen,zoals mediagalerijen, kaarten, en visuele stambomen."

#, python-format
msgid ""
Expand All @@ -42,12 +44,10 @@ msgid ""
" "
msgstr ""
"\n"
" Betty is vernoemd naar "
"%(liberta_lankester_label)s, en deze website bevat een uittreksel van haar familiegeschiedenis. Je kan de "
"pagina's over haar en haar familie "
"bekijken om een idee te krijgen "
"van hoe een Betty-site eruit ziet."
"\n"
" Betty is vernoemd naar %(liberta_lankester_label)s, en "
"deze website bevat een uittreksel van haar familiegeschiedenis. Je kan de"
" pagina's over haar en haar familie bekijken om een idee te krijgen van "
"hoe een Betty-site eruit ziet.\n"
" "

#, python-format
Expand Down Expand Up @@ -567,6 +567,9 @@ msgstr "Laden..."
msgid "Locale"
msgstr "Taalregio"

msgid "Locale aliases must not contain slashes."
msgstr "Taalregioaliassen mogen geen schuine streep bevatten."

msgid "Locales configuration must be a list."
msgstr "Taalregioconfiguratie moet een lijst zijn."

Expand Down
5 changes: 4 additions & 1 deletion betty/assets/locale/uk/LC_MESSAGES/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Betty VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2022-05-16 23:56+0100\n"
"POT-Creation-Date: 2022-06-01 16:02+0100\n"
"PO-Revision-Date: 2020-05-02 22:29+0100\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: uk\n"
Expand Down Expand Up @@ -534,6 +534,9 @@ msgstr ""
msgid "Locale"
msgstr ""

msgid "Locale aliases must not contain slashes."
msgstr ""

msgid "Locales configuration must be a list."
msgstr ""

Expand Down
Loading

0 comments on commit b27fd8c

Please sign in to comment.