improve some type annotations
This commit is contained in:
parent
1413ba896f
commit
14f2395fa6
2 changed files with 19 additions and 4 deletions
|
|
@ -4,7 +4,7 @@ import logging
|
||||||
from dataclasses import dataclass, fields
|
from dataclasses import dataclass, fields
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, cast
|
from typing import Generator, Literal, Optional, Type, TypeVar, overload
|
||||||
|
|
||||||
from . import config, db, request
|
from . import config, db, request
|
||||||
from .db import add_or_update_many_movies
|
from .db import add_or_update_many_movies
|
||||||
|
|
@ -13,6 +13,7 @@ from .models import Movie
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
# See
|
# See
|
||||||
# - https://www.imdb.com/interfaces/
|
# - https://www.imdb.com/interfaces/
|
||||||
|
|
@ -120,6 +121,20 @@ def count_lines(path) -> int:
|
||||||
return i
|
return i
|
||||||
|
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def read_imdb_tsv(
|
||||||
|
path, row_type, *, unpack: Literal[False]
|
||||||
|
) -> Generator[list[str], None, None]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def read_imdb_tsv(
|
||||||
|
path, row_type: Type[T], *, unpack: Literal[True] = True
|
||||||
|
) -> Generator[T, None, None]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
def read_imdb_tsv(path, row_type, *, unpack=True):
|
def read_imdb_tsv(path, row_type, *, unpack=True):
|
||||||
with gzip.open(path, "rt", newline="") as f:
|
with gzip.open(path, "rt", newline="") as f:
|
||||||
rows = csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
|
rows = csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
|
||||||
|
|
@ -158,7 +173,6 @@ def read_ratings(path):
|
||||||
def read_ratings_as_mapping(path):
|
def read_ratings_as_mapping(path):
|
||||||
"""Optimized function to quickly load all ratings."""
|
"""Optimized function to quickly load all ratings."""
|
||||||
rows = read_imdb_tsv(path, RatingRow, unpack=False)
|
rows = read_imdb_tsv(path, RatingRow, unpack=False)
|
||||||
rows = cast(list[list[str]], rows)
|
|
||||||
return {r[0]: (round(100 * (float(r[1]) - 1) / 9), int(r[2])) for r in rows}
|
return {r[0]: (round(100 * (float(r[1]) - 1) / 9), int(r[2])) for r in rows}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ from typing import (
|
||||||
|
|
||||||
from .types import ULID
|
from .types import ULID
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
def annotations(tp: Type) -> Optional[tuple]:
|
def annotations(tp: Type) -> Optional[tuple]:
|
||||||
return tp.__metadata__ if hasattr(tp, "__metadata__") else None
|
return tp.__metadata__ if hasattr(tp, "__metadata__") else None
|
||||||
|
|
@ -102,7 +104,7 @@ def asplain(o, *, fields_: set = None) -> dict[str, Any]:
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
def fromplain(cls, d: dict[str, Any]):
|
def fromplain(cls: Type[T], d: dict[str, Any]) -> T:
|
||||||
dd = {}
|
dd = {}
|
||||||
for f in fields(cls):
|
for f in fields(cls):
|
||||||
|
|
||||||
|
|
@ -232,7 +234,6 @@ class Movie:
|
||||||
self._is_lazy = False
|
self._is_lazy = False
|
||||||
|
|
||||||
|
|
||||||
T = TypeVar("T")
|
|
||||||
_RelationSentinel = object()
|
_RelationSentinel = object()
|
||||||
"""Mark a model field as containing external data.
|
"""Mark a model field as containing external data.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue