Changeset View
Standalone View
swh/core/api/classes.py
- This file was added.
# Copyright (C) 2020 The Software Heritage developers | |||||
# See the AUTHORS file at the top-level directory of this distribution | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
import attr | |||||
from typing import ( | |||||
Generic, | |||||
List, | |||||
Optional, | |||||
TypeVar, | |||||
) | |||||
from attrs_strict import type_validator | |||||
ResultType = TypeVar("ResultType") | |||||
ardumont: ok, ResultType it is then. | |||||
Done Inline ActionsCompromising, TResult for shorter as expressed by val below. ardumont: Compromising, `TResult` for shorter as expressed by val below. | |||||
@attr.s | |||||
class PagedResult(Generic[ResultType]): | |||||
"""Represents a page of results; with a token to get the next page""" | |||||
Done Inline ActionsI'd prefer to use a default_factory (dataclasses.field(default_factory=list) when you'll switch to dataclasses). As lists are mutable, it's a footgun to have a mutable object as default. vlorentz: I'd prefer to use a default_factory (`dataclasses.field(default_factory=list)` when you'll… | |||||
Done Inline ActionsDo we want to have a List here or an Iterable or a Sequence? ardumont: Do we want to have a List here or an Iterable or a Sequence? | |||||
Done Inline Actionsall three are fine, but I don't see any reason not to use List. vlorentz: all three are fine, but I don't see any reason not to use List. | |||||
Done Inline ActionsIn my tryouts, in storage, at some point, I tried a specific type involving PagedResult[BaseModel, str]. Still, mypy tried to lure me into the variance/co-variance/invariance things. Keeping List then ;) ardumont: In my tryouts, in storage, at some point, I tried a specific type involving `PagedResult… | |||||
results = attr.ib(type=List[ResultType], default=[]) | |||||
Done Inline ActionsOtherwise: swh/core/api/classes.py:25: error: 'TokenType' is a type variable and only valid in type context ardumont: Otherwise:
```
swh/core/api/classes.py:25: error: 'TokenType' is a type variable and only… | |||||
Done Inline Actionswtf... vlorentz: wtf... | |||||
next_page_token = attr.ib( | |||||
type=Optional[str], default=None, validator=type_validator() | |||||
) |
ok, ResultType it is then.