ba_data update

This commit is contained in:
Ayush Saini 2024-01-27 21:25:16 +05:30
parent 2174ae566d
commit 7ba24ecbcf
146 changed files with 1756 additions and 347 deletions

View file

@ -70,6 +70,13 @@ class IOExtendedData:
Can be overridden to migrate old data formats to new, etc.
"""
def did_input(self) -> None:
"""Called on a class instance after created from data.
Can be useful to correct values from the db, etc. in the
type-safe form.
"""
def _is_valid_for_codec(obj: Any, codec: Codec) -> bool:
"""Return whether a value consists solely of json-supported types.

View file

@ -64,11 +64,23 @@ class _Inputter(Generic[T]):
# For special extended data types, call their 'will_output' callback.
tcls = self._cls
if issubclass(tcls, IOExtendedData):
is_ext = True
tcls.will_input(values)
else:
is_ext = False
out = self._dataclass_from_input(self._cls, '', values)
assert isinstance(out, self._cls)
if is_ext:
# mypy complains that we're no longer returning a T
# if we operate on out directly.
out2 = out
assert isinstance(out2, IOExtendedData)
out2.did_input()
return out
def _value_from_input(

View file

@ -7,6 +7,8 @@ from __future__ import annotations
import dataclasses
from typing import TYPE_CHECKING
from typing_extensions import override
if TYPE_CHECKING:
from typing import Any
@ -32,6 +34,7 @@ class DataclassDiff:
self._obj1 = obj1
self._obj2 = obj2
@override
def __repr__(self) -> str:
return dataclass_diff(self._obj1, self._obj2)

View file

@ -6,6 +6,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING
import errno
from typing_extensions import override
if TYPE_CHECKING:
from typing import Any
@ -82,6 +84,7 @@ class RemoteError(Exception):
super().__init__(msg)
self._peer_desc = peer_desc
@override
def __str__(self) -> str:
s = ''.join(str(arg) for arg in self.args)
# Indent so we can more easily tell what is the remote part when

View file

@ -15,6 +15,7 @@ from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Annotated
from threading import Thread, current_thread, Lock
from typing_extensions import override
from efro.util import utc_now
from efro.call import tpartial
from efro.terminal import Clr
@ -306,6 +307,7 @@ class LogHandler(logging.Handler):
"""Submit a call to be run in the logging background thread."""
self._event_loop.call_soon_threadsafe(call)
@override
def emit(self, record: logging.LogRecord) -> None:
# pylint: disable=too-many-branches
if __debug__:

View file

@ -386,6 +386,7 @@ class MessageProtocol:
f'\n'
f'from typing import TYPE_CHECKING{ovld}{ovld2}\n'
f'\n'
# f'from typing_extensions import override\n'
f'{import_lines}'
f'\n'
f'if TYPE_CHECKING:\n'

View file

@ -174,17 +174,26 @@ def empty_weakref(objtype: type[T]) -> weakref.ref[T]:
# Just create an object and let it die. Is there a cleaner way to do this?
# return weakref.ref(_EmptyObj()) # type: ignore
# Sharing a single ones seems at least a bit better.
return _g_empty_weak_ref # type: ignore
def data_size_str(bytecount: int) -> str:
def data_size_str(bytecount: int, compact: bool = False) -> str:
"""Given a size in bytes, returns a short human readable string.
This should be 6 or fewer chars for most all sane file sizes.
In compact mode this should be 6 or fewer chars for most all
sane file sizes.
"""
# pylint: disable=too-many-return-statements
# Special case: handle negatives.
if bytecount < 0:
val = data_size_str(-bytecount, compact=compact)
return f'-{val}'
if bytecount <= 999:
return f'{bytecount} B'
suffix = 'B' if compact else 'bytes'
return f'{bytecount} {suffix}'
kbytecount = bytecount / 1024
if round(kbytecount, 1) < 10.0:
return f'{kbytecount:.1f} KB'
@ -197,7 +206,7 @@ def data_size_str(bytecount: int) -> str:
return f'{mbytecount:.0f} MB'
gbytecount = bytecount / (1024 * 1024 * 1024)
if round(gbytecount, 1) < 10.0:
return f'{mbytecount:.1f} GB'
return f'{gbytecount:.1f} GB'
return f'{gbytecount:.0f} GB'
@ -623,7 +632,7 @@ def check_non_optional(obj: T | None) -> T:
Use assert_non_optional for a more efficient (but less safe) equivalent.
"""
if obj is None:
raise TypeError('Got None value in check_non_optional.')
raise ValueError('Got None value in check_non_optional.')
return obj