mirror of
https://github.com/beetbox/beets.git
synced 2026-02-23 07:44:38 +01:00
Add typing to dbcore types modules
This commit is contained in:
parent
ffc114ef67
commit
0ee3342257
1 changed files with 21 additions and 17 deletions
|
|
@ -15,6 +15,7 @@
|
|||
"""Representation of type information for DBCore model fields.
|
||||
"""
|
||||
|
||||
from typing import Union, Any
|
||||
from . import query
|
||||
from beets.util import str2bool
|
||||
|
||||
|
|
@ -44,12 +45,12 @@ class Type:
|
|||
"""
|
||||
|
||||
@property
|
||||
def null(self):
|
||||
def null(self) -> model_type:
|
||||
"""The value to be exposed when the underlying value is None.
|
||||
"""
|
||||
return self.model_type()
|
||||
|
||||
def format(self, value):
|
||||
def format(self, value: model_type) -> str:
|
||||
"""Given a value of this type, produce a Unicode string
|
||||
representing the value. This is used in template evaluation.
|
||||
"""
|
||||
|
|
@ -63,7 +64,7 @@ class Type:
|
|||
|
||||
return str(value)
|
||||
|
||||
def parse(self, string):
|
||||
def parse(self, string: str) -> model_type:
|
||||
"""Parse a (possibly human-written) string and return the
|
||||
indicated value of this type.
|
||||
"""
|
||||
|
|
@ -72,7 +73,7 @@ class Type:
|
|||
except ValueError:
|
||||
return self.null
|
||||
|
||||
def normalize(self, value):
|
||||
def normalize(self, value: model_type) -> model_type:
|
||||
"""Given a value that will be assigned into a field of this
|
||||
type, normalize the value to have the appropriate type. This
|
||||
base implementation only reinterprets `None`.
|
||||
|
|
@ -84,7 +85,10 @@ class Type:
|
|||
# `self.model_type(value)`
|
||||
return value
|
||||
|
||||
def from_sql(self, sql_value):
|
||||
def from_sql(
|
||||
self,
|
||||
sql_value: Union[None, int, float, str, bytes],
|
||||
) -> model_type:
|
||||
"""Receives the value stored in the SQL backend and return the
|
||||
value to be stored in the model.
|
||||
|
||||
|
|
@ -105,7 +109,7 @@ class Type:
|
|||
else:
|
||||
return self.normalize(sql_value)
|
||||
|
||||
def to_sql(self, model_value):
|
||||
def to_sql(self, model_value: Any) -> Union[None, int, float, str, bytes]:
|
||||
"""Convert a value as stored in the model object to a value used
|
||||
by the database adapter.
|
||||
"""
|
||||
|
|
@ -125,7 +129,7 @@ class Integer(Type):
|
|||
query = query.NumericQuery
|
||||
model_type = int
|
||||
|
||||
def normalize(self, value):
|
||||
def normalize(self, value) -> Union[int, str]:
|
||||
try:
|
||||
return self.model_type(round(float(value)))
|
||||
except ValueError:
|
||||
|
|
@ -138,10 +142,10 @@ class PaddedInt(Integer):
|
|||
"""An integer field that is formatted with a given number of digits,
|
||||
padded with zeroes.
|
||||
"""
|
||||
def __init__(self, digits):
|
||||
def __init__(self, digits: int):
|
||||
self.digits = digits
|
||||
|
||||
def format(self, value):
|
||||
def format(self, value: int) -> str:
|
||||
return '{0:0{1}d}'.format(value or 0, self.digits)
|
||||
|
||||
|
||||
|
|
@ -155,11 +159,11 @@ class ScaledInt(Integer):
|
|||
"""An integer whose formatting operation scales the number by a
|
||||
constant and adds a suffix. Good for units with large magnitudes.
|
||||
"""
|
||||
def __init__(self, unit, suffix=''):
|
||||
def __init__(self, unit: int, suffix: str = ''):
|
||||
self.unit = unit
|
||||
self.suffix = suffix
|
||||
|
||||
def format(self, value):
|
||||
def format(self, value: int) -> str:
|
||||
return '{}{}'.format((value or 0) // self.unit, self.suffix)
|
||||
|
||||
|
||||
|
|
@ -169,7 +173,7 @@ class Id(Integer):
|
|||
"""
|
||||
null = None
|
||||
|
||||
def __init__(self, primary=True):
|
||||
def __init__(self, primary: bool = True):
|
||||
if primary:
|
||||
self.sql = 'INTEGER PRIMARY KEY'
|
||||
|
||||
|
|
@ -182,10 +186,10 @@ class Float(Type):
|
|||
query = query.NumericQuery
|
||||
model_type = float
|
||||
|
||||
def __init__(self, digits=1):
|
||||
def __init__(self, digits: int = 1):
|
||||
self.digits = digits
|
||||
|
||||
def format(self, value):
|
||||
def format(self, value: float) -> str:
|
||||
return '{0:.{1}f}'.format(value or 0, self.digits)
|
||||
|
||||
|
||||
|
|
@ -201,7 +205,7 @@ class String(Type):
|
|||
sql = 'TEXT'
|
||||
query = query.SubstringQuery
|
||||
|
||||
def normalize(self, value):
|
||||
def normalize(self, value: str) -> str:
|
||||
if value is None:
|
||||
return self.null
|
||||
else:
|
||||
|
|
@ -236,10 +240,10 @@ class Boolean(Type):
|
|||
query = query.BooleanQuery
|
||||
model_type = bool
|
||||
|
||||
def format(self, value):
|
||||
def format(self, value: bool) -> str:
|
||||
return str(bool(value))
|
||||
|
||||
def parse(self, string):
|
||||
def parse(self, string: str) -> bool:
|
||||
return str2bool(string)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue