From c870665be02e18b21205fdb678eaecd7a763e327 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 6 Jul 2022 12:54:03 -0400 Subject: [PATCH] Remove `BaseModel` use from all dataclass-like uses --- piker/brokers/kraken/broker.py | 4 ++-- piker/clearing/_ems.py | 8 ++------ piker/data/feed.py | 8 ++------ piker/ui/_event.py | 15 +++++---------- piker/ui/_orm.py | 10 ++++------ piker/ui/order_mode.py | 12 +++++------- 6 files changed, 20 insertions(+), 37 deletions(-) diff --git a/piker/brokers/kraken/broker.py b/piker/brokers/kraken/broker.py index 4e2e02f6..bc89af84 100644 --- a/piker/brokers/kraken/broker.py +++ b/piker/brokers/kraken/broker.py @@ -32,7 +32,6 @@ from typing import ( ) import pendulum -from pydantic import BaseModel import trio import tractor import wsproto @@ -47,6 +46,7 @@ from piker.clearing._messages import ( BrokerdPosition, BrokerdStatus, ) +from pikerd.data.types import Struct from . import log from .api import ( Client, @@ -62,7 +62,7 @@ from .feed import ( ) -class Trade(BaseModel): +class Trade(Struct): ''' Trade class that helps parse and validate ownTrades stream diff --git a/piker/clearing/_ems.py b/piker/clearing/_ems.py index 051ad30e..c935da92 100644 --- a/piker/clearing/_ems.py +++ b/piker/clearing/_ems.py @@ -26,7 +26,6 @@ import time from typing import AsyncIterator, Callable from bidict import bidict -from pydantic import BaseModel import trio from trio_typing import TaskStatus import tractor @@ -34,6 +33,7 @@ import tractor from ..log import get_logger from ..data._normalize import iterticks from ..data.feed import Feed, maybe_open_feed +from ..data.types import Struct from .._daemon import maybe_spawn_brokerd from . import _paper_engine as paper from ._messages import ( @@ -303,7 +303,7 @@ class TradesRelay: consumers: int = 0 -class Router(BaseModel): +class Router(Struct): ''' Order router which manages and tracks per-broker dark book, alerts, clearing and related data feed management. @@ -324,10 +324,6 @@ class Router(BaseModel): # brokername to trades-dialogues streams with ``brokerd`` actors relays: dict[str, TradesRelay] = {} - class Config: - arbitrary_types_allowed = True - underscore_attrs_are_private = False - def get_dark_book( self, brokername: str, diff --git a/piker/data/feed.py b/piker/data/feed.py index 66ced72f..2795535d 100644 --- a/piker/data/feed.py +++ b/piker/data/feed.py @@ -42,7 +42,6 @@ from trio_typing import TaskStatus import trimeter import tractor from tractor.trionics import maybe_open_context -from pydantic import BaseModel import pendulum import numpy as np @@ -59,6 +58,7 @@ from ._sharedmem import ( ShmArray, ) from .ingest import get_ingestormod +from .types import Struct from ._source import ( base_iohlc_dtype, Symbol, @@ -84,7 +84,7 @@ if TYPE_CHECKING: log = get_logger(__name__) -class _FeedsBus(BaseModel): +class _FeedsBus(Struct): ''' Data feeds broadcaster and persistence management. @@ -100,10 +100,6 @@ class _FeedsBus(BaseModel): a dedicated cancel scope. ''' - class Config: - arbitrary_types_allowed = True - underscore_attrs_are_private = False - brokername: str nursery: trio.Nursery feeds: dict[str, tuple[dict, dict]] = {} diff --git a/piker/ui/_event.py b/piker/ui/_event.py index f9982843..9c006dc8 100644 --- a/piker/ui/_event.py +++ b/piker/ui/_event.py @@ -21,7 +21,6 @@ Qt event proxying and processing using ``trio`` mem chans. from contextlib import asynccontextmanager, AsyncExitStack from typing import Callable -from pydantic import BaseModel import trio from PyQt5 import QtCore from PyQt5.QtCore import QEvent, pyqtBoundSignal @@ -30,6 +29,8 @@ from PyQt5.QtWidgets import ( QGraphicsSceneMouseEvent as gs_mouse, ) +from ..data.types import Struct + MOUSE_EVENTS = { gs_mouse.GraphicsSceneMousePress, @@ -43,13 +44,10 @@ MOUSE_EVENTS = { # TODO: maybe consider some constrained ints down the road? # https://pydantic-docs.helpmanual.io/usage/types/#constrained-types -class KeyboardMsg(BaseModel): +class KeyboardMsg(Struct): '''Unpacked Qt keyboard event data. ''' - class Config: - arbitrary_types_allowed = True - event: QEvent etype: int key: int @@ -57,16 +55,13 @@ class KeyboardMsg(BaseModel): txt: str def to_tuple(self) -> tuple: - return tuple(self.dict().values()) + return tuple(self.to_dict().values()) -class MouseMsg(BaseModel): +class MouseMsg(Struct): '''Unpacked Qt keyboard event data. ''' - class Config: - arbitrary_types_allowed = True - event: QEvent etype: int button: int diff --git a/piker/ui/_orm.py b/piker/ui/_orm.py index 67050e95..8dea0b6d 100644 --- a/piker/ui/_orm.py +++ b/piker/ui/_orm.py @@ -22,12 +22,9 @@ from __future__ import annotations from typing import ( Optional, Generic, TypeVar, Callable, - Literal, ) -import enum -import sys -from pydantic import BaseModel, validator +# from pydantic import BaseModel, validator from pydantic.generics import GenericModel from PyQt5.QtWidgets import ( QWidget, @@ -38,6 +35,7 @@ from ._forms import ( # FontScaledDelegate, Edit, ) +from ..data.types import Struct DataType = TypeVar('DataType') @@ -62,7 +60,7 @@ class Selection(Field[DataType], Generic[DataType]): options: dict[str, DataType] # value: DataType = None - @validator('value') # , always=True) + # @validator('value') # , always=True) def set_value_first( cls, @@ -100,7 +98,7 @@ class Edit(Field[DataType], Generic[DataType]): widget_factory = Edit -class AllocatorPane(BaseModel): +class AllocatorPane(Struct): account = Selection[str]( options=dict.fromkeys( diff --git a/piker/ui/order_mode.py b/piker/ui/order_mode.py index e9ed3499..89e7b2d8 100644 --- a/piker/ui/order_mode.py +++ b/piker/ui/order_mode.py @@ -27,7 +27,6 @@ import time from typing import Optional, Dict, Callable, Any import uuid -from pydantic import BaseModel import tractor import trio from PyQt5.QtCore import Qt @@ -41,6 +40,7 @@ from ..clearing._allocate import ( from ._style import _font from ..data._source import Symbol from ..data.feed import Feed +from ..data.types import Struct from ..log import get_logger from ._editors import LineEditor, ArrowEditor from ._lines import order_line, LevelLine @@ -58,7 +58,7 @@ from ._forms import open_form_input_handling log = get_logger(__name__) -class OrderDialog(BaseModel): +class OrderDialog(Struct): ''' Trade dialogue meta-data describing the lifetime of an order submission to ``emsd`` from a chart. @@ -73,10 +73,6 @@ class OrderDialog(BaseModel): msgs: dict[str, dict] = {} fills: Dict[str, Any] = {} - class Config: - arbitrary_types_allowed = True - underscore_attrs_are_private = False - def on_level_change_update_next_order_info( @@ -858,7 +854,9 @@ async def process_trades_and_update_ui( # delete level line from view mode.on_cancel(oid) broker_msg = msg['brokerd_msg'] - log.warning(f'Order {oid} failed with:\n{pformat(broker_msg)}') + log.warning( + f'Order {oid} failed with:\n{pformat(broker_msg)}' + ) elif resp in ( 'dark_triggered'