Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/interface.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import datetime | import datetime | ||||
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union | from typing import Dict, Iterable, List, Optional, Tuple, TypeVar, Union | ||||
from swh.core.api import remote_api_endpoint | from swh.core.api import remote_api_endpoint | ||||
from swh.core.api.classes import PagedResult as CorePagedResult | |||||
from swh.model.identifiers import SWHID | from swh.model.identifiers import SWHID | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Content, | Content, | ||||
Directory, | Directory, | ||||
Origin, | Origin, | ||||
OriginVisit, | OriginVisit, | ||||
OriginVisitStatus, | OriginVisitStatus, | ||||
Revision, | Revision, | ||||
Release, | Release, | ||||
Snapshot, | Snapshot, | ||||
SkippedContent, | SkippedContent, | ||||
MetadataAuthority, | MetadataAuthority, | ||||
MetadataAuthorityType, | MetadataAuthorityType, | ||||
MetadataFetcher, | MetadataFetcher, | ||||
MetadataTargetType, | MetadataTargetType, | ||||
RawExtrinsicMetadata, | RawExtrinsicMetadata, | ||||
) | ) | ||||
def deprecated(f): | def deprecated(f): | ||||
f.deprecated_endpoint = True | f.deprecated_endpoint = True | ||||
return f | return f | ||||
TResult = TypeVar("TResult") | |||||
vlorentz: PagedResult is not an instance of `CorePagedResult[TResult, str]`, it's `CorePagedResult… | |||||
Done Inline ActionsI tried that as well... swh/storage/interface.py:36: error: Type variable "swh.core.api.classes.TResult" is unbound swh/storage/interface.py:36: note: (Hint: Use "Generic[TResult]" or "Protocol[TResult]" base class to bind "TResult" inside a class) swh/storage/interface.py:36: note: (Hint: Use "TResult" in function signature to bind "TResult" inside a function) swh/storage/interface.py:810: error: Variable "swh.storage.interface.PagedResult" is not valid as a type swh/storage/interface.py:810: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases swh/storage/in_memory.py:869: error: Variable "swh.storage.interface.PagedResult" is not valid as a type swh/storage/in_memory.py:869: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases swh/storage/cassandra/storage.py:851: error: Variable "swh.storage.interface.PagedResult" is not valid as a type swh/storage/cassandra/storage.py:851: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases swh/storage/storage.py:890: error: Variable "swh.storage.interface.PagedResult" is not valid as a type swh/storage/storage.py:890: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases Found 5 errors in 4 files (checked 68 source files) ardumont: I tried that as well...
```
swh/storage/interface.py:36: error: Type variable "swh.core.api. | |||||
Done Inline Actionsah yes, that makes sense. vlorentz: ah yes, that makes sense. | |||||
PagedResult = CorePagedResult[TResult, str] | |||||
class StorageInterface: | class StorageInterface: | ||||
@remote_api_endpoint("check_config") | @remote_api_endpoint("check_config") | ||||
Done Inline Actionsif you want this one to work, don't reuse the same TypeVar: TResult = TypeVar("TResult") class PagedResult(CorePagedResult[TResult, str]): pass vlorentz: if you want this one to work, don't reuse the same TypeVar:
```
TResult = TypeVar("TResult")… | |||||
Done Inline Actions
why? I recall having tested that (without redefining TResult though) that would not work... ardumont: > if you want this one to work, don't reuse the same TypeVar:
why?
----
I recall having… | |||||
Done Inline ActionsI'm guessing it makes mypy glitchy to use the same typevar in both a generic class in a subclass of that class vlorentz: I'm guessing it makes mypy glitchy to use the same typevar in both a generic class in a… | |||||
def check_config(self, *, check_write): | def check_config(self, *, check_write): | ||||
"""Check that the storage is configured and ready to go.""" | """Check that the storage is configured and ready to go.""" | ||||
... | ... | ||||
@remote_api_endpoint("content/add") | @remote_api_endpoint("content/add") | ||||
def content_add(self, content: Iterable[Content]) -> Dict: | def content_add(self, content: Iterable[Content]) -> Dict: | ||||
"""Add content blobs to the storage | """Add content blobs to the storage | ||||
▲ Show 20 Lines • Show All 743 Lines • ▼ Show 20 Lines | ) -> None: | ||||
""" | """ | ||||
... | ... | ||||
@remote_api_endpoint("origin/visit/get") | @remote_api_endpoint("origin/visit/get") | ||||
def origin_visit_get( | def origin_visit_get( | ||||
self, | self, | ||||
origin: str, | origin: str, | ||||
last_visit: Optional[int] = None, | page_token: Optional[str] = None, | ||||
limit: Optional[int] = None, | |||||
order: str = "asc", | order: str = "asc", | ||||
) -> Iterable[Dict[str, Any]]: | limit: int = 10, | ||||
"""Retrieve all the origin's visit's information. | ) -> PagedResult[OriginVisit]: | ||||
"""Retrieve page of OriginVisit information. | |||||
Args: | Args: | ||||
origin: The visited origin | origin: The visited origin | ||||
last_visit: Starting point from which listing the next visits | page_token: opaque string used to get the next results of a search | ||||
Default to None | |||||
limit: Number of results to return from the last visit. | |||||
Default to None | |||||
order: Order on visit id fields to list origin visits (default to asc) | order: Order on visit id fields to list origin visits (default to asc) | ||||
limit: Number of visits to return | |||||
Yields: | Raises: | ||||
List of visits. | StorageArgumentException if the order is wrong or the page_token type is | ||||
mistyped. | |||||
Returns: Page of OriginVisit data model objects. if next_page_token is None, | |||||
there is no longer data to retrieve. | |||||
""" | """ | ||||
... | ... | ||||
@remote_api_endpoint("origin/visit/find_by_date") | @remote_api_endpoint("origin/visit/find_by_date") | ||||
def origin_visit_find_by_date( | def origin_visit_find_by_date( | ||||
self, origin: str, visit_date: datetime.datetime | self, origin: str, visit_date: datetime.datetime | ||||
) -> Optional[OriginVisit]: | ) -> Optional[OriginVisit]: | ||||
▲ Show 20 Lines • Show All 439 Lines • Show Last 20 Lines |
PagedResult is not an instance of CorePagedResult[TResult, str], it's CorePagedResult[TResult, str] itself. Therefore: