Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes+silences type errors in models #155

Merged
merged 22 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion xrpl/models/amounts/issued_currency_amount.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class IssuedCurrencyAmount(IssuedCurrency):
See https://xrpl.org/currency-formats.html#issued-currency-amounts.
"""

value: str = REQUIRED
value: str = REQUIRED # type: ignore
Copy link
Contributor Author

@tedkalaw tedkalaw Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this causes a type error because the type of value is a str, but the default value is REQUIRED (an object). i tried a couple patterns but ran into the issue that the generated getter has a type of Union[<intended type>, object] which means that at call sites, you have to narrow the type...and we don't want to expose this to callers anyhow

this will likely require a deeper refactor - i could imagine this being implemented as a list of required attributes or a few other ways, although my understanding is that this is a tricky part of dataclass subclassing. as such, i opted for # type: ignore, as this preserves the type of the getter. not ideal, but don't think i'll have the time to go deeper so i did this everywhere in models

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i think this is the optimal solution. value = REQUIRED should be a type error in all cases because if a value = REQUIRED then an error will be thrown

4 changes: 2 additions & 2 deletions xrpl/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from abc import ABC
from typing import Any, Dict
from typing import Any, Dict, Type

from typing_extensions import Final

Expand All @@ -19,7 +19,7 @@ class BaseModel(ABC):
"""The base class for all model types."""

@classmethod
def from_dict(cls: BaseModel, value: Dict[str, Any]) -> BaseModel:
def from_dict(cls: Type[BaseModel], value: Dict[str, Any]) -> BaseModel:
"""
Construct a new BaseModel from a dictionary of parameters.

Expand Down
6 changes: 3 additions & 3 deletions xrpl/models/currencies/issued_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
_CHAR: Final[str] = r"[A-Za-z\d\?!@#\$%\^&\*<>\(\){}\[\]\|]"
_CURRENCY_CODE: Final[str] = f"{_CHAR}{{3}}"
_HEX: Final[str] = f"{_CHAR}{{40}}"
_VALIDATOR: Final[re.Pattern] = re.compile(
_VALIDATOR: Final[re.Pattern[str]] = re.compile(
f"(?:{_CURRENCY_CODE}|{_HEX})",
)

Expand All @@ -33,8 +33,8 @@ class IssuedCurrency(BaseModel):
See https://xrpl.org/currency-formats.html#specifying-currency-amounts
"""

currency: str = REQUIRED
issuer: str = REQUIRED
currency: str = REQUIRED # type: ignore
issuer: str = REQUIRED # type: ignore

def _get_errors(self: IssuedCurrency) -> Dict[str, str]:
errors = super()._get_errors()
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/accounts/account_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AccountChannels(Request):
"""

method: RequestMethod = field(default=RequestMethod.ACCOUNT_CHANNELS, init=False)
account: str = REQUIRED
account: str = REQUIRED # type: ignore
destination_account: Optional[str] = None
limit: int = 200
# marker data shape is actually undefined in the spec, up to the
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/accounts/account_currencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AccountCurrencies(Request):
`See account_currencies <https://xrpl.org/account_currencies.html>`_
"""

account: str = REQUIRED
account: str = REQUIRED # type: ignore
ledger_hash: Optional[str] = None
ledger_index: Optional[Union[str, int]] = None
method: RequestMethod = field(default=RequestMethod.ACCOUNT_CURRENCIES, init=False)
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/accounts/account_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AccountInfo(Request):
`See account_info <https://xrpl.org/account_info.html>`_
"""

account: str = REQUIRED
account: str = REQUIRED # type: ignore
ledger_hash: Optional[str] = None
ledger_index: Optional[Union[str, int]] = None
method: RequestMethod = field(default=RequestMethod.ACCOUNT_INFO, init=False)
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/accounts/account_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AccountLines(Request):
`See account_lines <https://xrpl.org/account_lines.html>`_
"""

account: str = REQUIRED
account: str = REQUIRED # type: ignore
ledger_hash: Optional[str] = None
ledger_index: Optional[Union[str, int]] = None
method: RequestMethod = field(default=RequestMethod.ACCOUNT_LINES, init=False)
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/accounts/account_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AccountObjects(Request):
`See account_objects <https://xrpl.org/account_objects.html>`_
"""

account: str = REQUIRED
account: str = REQUIRED # type: ignore
ledger_hash: Optional[str] = None
ledger_index: Optional[Union[str, int]] = None
method: RequestMethod = field(default=RequestMethod.ACCOUNT_OBJECTS, init=False)
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/accounts/account_offers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AccountOffers(Request):
`See account_offers <https://xrpl.org/account_offers.html>`_
"""

account: str = REQUIRED
account: str = REQUIRED # type: ignore
ledger_hash: Optional[str] = None
ledger_index: Optional[Union[str, int]] = None
method: RequestMethod = field(default=RequestMethod.ACCOUNT_OFFERS, init=False)
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/accounts/account_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AccountTx(Request):
`See account_tx <https://xrpl.org/account_tx.html>`_
"""

account: str = REQUIRED
account: str = REQUIRED # type: ignore
ledger_hash: Optional[str] = None
ledger_index: Optional[Union[str, int]] = None
method: RequestMethod = field(default=RequestMethod.ACCOUNT_TX, init=False)
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/accounts/gateway_balances.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GatewayBalances(Request):
`See gateway_balances <https://xrpl.org/gateway_balances.html>`_
"""

account: str = REQUIRED
account: str = REQUIRED # type: ignore
ledger_hash: Optional[str] = None
ledger_index: Optional[Union[str, int]] = None
method: RequestMethod = field(default=RequestMethod.GATEWAY_BALANCES, init=False)
Expand Down
4 changes: 2 additions & 2 deletions xrpl/models/requests/accounts/no_ripple_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class NoRippleCheck(Request):
`See noripple_check <https://xrpl.org/noripple_check.html>`_
"""

account: str = REQUIRED
account: str = REQUIRED # type: ignore
ledger_hash: Optional[str] = None
ledger_index: Optional[Union[str, int]] = None
method: RequestMethod = field(default=RequestMethod.NO_RIPPLE_CHECK, init=False)
role: NoRippleCheckRole = REQUIRED
role: NoRippleCheckRole = REQUIRED # type: ignore
transactions: Optional[bool] = False
limit: Optional[int] = 300
4 changes: 2 additions & 2 deletions xrpl/models/requests/channel_authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ChannelAuthorize(Request):
"""

method: RequestMethod = field(default=RequestMethod.CHANNEL_AUTHORIZE, init=False)
channel_id: str = REQUIRED
amount: str = REQUIRED
channel_id: str = REQUIRED # type: ignore
amount: str = REQUIRED # type: ignore
secret: Optional[str] = None
seed: Optional[str] = None
seed_hex: Optional[str] = None
Expand Down
8 changes: 4 additions & 4 deletions xrpl/models/requests/channel_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ChannelVerify(Request):
"""

method: RequestMethod = field(default=RequestMethod.CHANNEL_VERIFY, init=False)
channel_id: str = REQUIRED
amount: str = REQUIRED
public_key: str = REQUIRED
signature: str = REQUIRED
channel_id: str = REQUIRED # type: ignore
amount: str = REQUIRED # type: ignore
public_key: str = REQUIRED # type: ignore
signature: str = REQUIRED # type: ignore
4 changes: 2 additions & 2 deletions xrpl/models/requests/ledger_closed.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ class LedgerClosed(Request):
"""

method: RequestMethod = field(default=RequestMethod.LEDGER_CLOSED, init=False)
ledger_hash: str = REQUIRED
ledger_index: int = REQUIRED
ledger_hash: str = REQUIRED # type: ignore
ledger_index: int = REQUIRED # type: ignore
24 changes: 12 additions & 12 deletions xrpl/models/requests/ledger_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class DepositPreauth(BaseModel):
object ID.
"""

owner: str = REQUIRED
authorized: str = REQUIRED
owner: str = REQUIRED # type: ignore
authorized: str = REQUIRED # type: ignore


@require_kwargs_on_init
Expand All @@ -34,8 +34,8 @@ class Directory(BaseModel):
object ID.
"""

owner: str = REQUIRED
dir_root: str = REQUIRED
owner: str = REQUIRED # type: ignore
dir_root: str = REQUIRED # type: ignore
sub_index: Optional[int] = None


Expand All @@ -47,8 +47,8 @@ class Escrow(BaseModel):
object ID.
"""

owner: str = REQUIRED
seq: int = REQUIRED
owner: str = REQUIRED # type: ignore
seq: int = REQUIRED # type: ignore


@require_kwargs_on_init
Expand All @@ -59,17 +59,17 @@ class Offer(BaseModel):
object ID.
"""

account: str = REQUIRED
seq: int = REQUIRED
account: str = REQUIRED # type: ignore
seq: int = REQUIRED # type: ignore


@require_kwargs_on_init
@dataclass(frozen=True)
class RippleState(BaseModel):
"""Required fields for requesting a RippleState."""

accounts: List[str] = REQUIRED
currency: str = REQUIRED
accounts: List[str] = REQUIRED # type: ignore
currency: str = REQUIRED # type: ignore


@require_kwargs_on_init
Expand All @@ -80,8 +80,8 @@ class Ticket(BaseModel):
object ID.
"""

owner: str = REQUIRED
ticket_sequence: int = REQUIRED
owner: str = REQUIRED # type: ignore
ticket_sequence: int = REQUIRED # type: ignore


@require_kwargs_on_init
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ class Manifest(Request):
"""

method: RequestMethod = field(default=RequestMethod.MANIFEST, init=False)
public_key: str = REQUIRED
public_key: str = REQUIRED # type: ignore
4 changes: 2 additions & 2 deletions xrpl/models/requests/paths/book_offers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class BookOffers(Request):
as the order book, between two currencies.
"""

taker_gets: Currency = REQUIRED
taker_pays: Currency = REQUIRED
taker_gets: Currency = REQUIRED # type: ignore
taker_pays: Currency = REQUIRED # type: ignore
method: RequestMethod = field(default=RequestMethod.BOOK_OFFERS, init=False)
ledger_hash: Optional[str] = None
ledger_index: Optional[Union[str, int]] = None
Expand Down
4 changes: 2 additions & 2 deletions xrpl/models/requests/paths/deposit_authorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class DepositAuthorized(Request):
authorization to deliver money to your account.
"""

source_account: str = REQUIRED
destination_account: str = REQUIRED
source_account: str = REQUIRED # type: ignore
destination_account: str = REQUIRED # type: ignore
method: RequestMethod = field(default=RequestMethod.DEPOSIT_AUTHORIZED, init=False)
ledger_hash: Optional[str] = None
ledger_index: Optional[str] = None
20 changes: 10 additions & 10 deletions xrpl/models/requests/paths/path_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,28 @@ def _get_errors(self: PathStep) -> Dict[str, str]:

def _get_account_error(self: PathStep) -> Optional[str]:
if self.account is None:
return
return None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for Optional return types, mypy by default requires you to return None instead of having an empty return. this seems like a stylistic choice to me, but opted to just make it pass the default settings to simplify configuration

if we want, we can enable a flag (warn_no_return) in the config to make this work

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine w/ this

if self.currency is not None or self.issuer is not None:
return "Cannot set account if currency or issuer are set"
return
return None

def _get_currency_error(self: PathStep) -> Optional[str]:
if self.currency is None:
return
return None
if self.account is not None:
return "Cannot set currency if account is set"
if self.issuer is not None and self.currency.upper() == "XRP":
return "Cannot set issuer if currency is XRP"
return
return None

def _get_issuer_error(self: PathStep) -> Optional[str]:
if self.issuer is None:
return
return None
if self.account is not None:
return "Cannot set issuer if account is set"
if self.currency is not None and self.currency.upper() == "XRP":
return "Cannot set issuer if currency is XRP"
return
return None


@require_kwargs_on_init
Expand Down Expand Up @@ -132,10 +132,10 @@ class PathFind(Request):
a symptom of heavy server load.)
"""

subcommand: PathFindSubcommand = REQUIRED
source_account: str = REQUIRED
destination_account: str = REQUIRED
destination_amount: Amount = REQUIRED
subcommand: PathFindSubcommand = REQUIRED # type: ignore
source_account: str = REQUIRED # type: ignore
destination_account: str = REQUIRED # type: ignore
destination_amount: Amount = REQUIRED # type: ignore
method: RequestMethod = field(default=RequestMethod.PATH_FIND, init=False)
send_max: Optional[Amount] = None
paths: Optional[List[List[PathStep]]] = None
6 changes: 3 additions & 3 deletions xrpl/models/requests/paths/ripple_path_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class RipplePathFind(Request):
the paths returned by this method are, in fact, the best paths.
"""

source_account: str = REQUIRED
destination_account: str = REQUIRED
destination_amount: Amount = REQUIRED
source_account: str = REQUIRED # type: ignore
destination_account: str = REQUIRED # type: ignore
destination_amount: Amount = REQUIRED # type: ignore
method: RequestMethod = field(default=RequestMethod.RIPPLE_PATH_FIND, init=False)
send_max: Optional[Amount] = None
source_currencies: Optional[List[Currency]] = None
Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/requests/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Request(BaseModel):
Represents fields common to all request types.
"""

method: RequestMethod = REQUIRED
method: RequestMethod = REQUIRED # type: ignore
id: Optional[int] = None

def to_dict(self: Request) -> Dict[str, Any]:
Expand Down
6 changes: 3 additions & 3 deletions xrpl/models/requests/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class StreamParameter(str, Enum):
class SubscribeBook(BaseModel):
"""Format for elements in the `books` array for Subscribe only."""

taker_gets: Currency = REQUIRED
taker_pays: Currency = REQUIRED
taker: str = REQUIRED
taker_gets: Currency = REQUIRED # type: ignore
taker_pays: Currency = REQUIRED # type: ignore
taker: str = REQUIRED # type: ignore
snapshot: Optional[bool] = False
both: Optional[bool] = False

Expand Down
4 changes: 2 additions & 2 deletions xrpl/models/requests/unsubscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
class UnsubscribeBook(BaseModel):
"""Format for elements in the `books` array for Unsubscribe only."""

taker_gets: Currency = REQUIRED
taker_pays: Currency = REQUIRED
taker_gets: Currency = REQUIRED # type: ignore
taker_pays: Currency = REQUIRED # type: ignore
both: Optional[bool] = False


Expand Down
4 changes: 2 additions & 2 deletions xrpl/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class Response(BaseModel):
Represents fields common to all response types.
"""

status: ResponseStatus = REQUIRED
result: Union[List[Any], Dict[Any]] = REQUIRED
status: ResponseStatus = REQUIRED # type: ignore
result: Union[List[Any], Dict[Any]] = REQUIRED # type: ignore
id: Optional[Union[int, str]] = None
type: Optional[ResponseType] = None

Expand Down
2 changes: 1 addition & 1 deletion xrpl/models/transactions/account_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ class AccountDelete(Transaction):
`See Deletion of Accounts <https://xrpl.org/accounts.html#deletion-of-accounts>`_
"""

destination: str = REQUIRED
destination: str = REQUIRED # type: ignore
destination_tag: Optional[int] = None
transaction_type: TransactionType = TransactionType.ACCOUNT_DELETE
8 changes: 4 additions & 4 deletions xrpl/models/transactions/account_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ def _get_errors(self: AccountSet) -> Dict[str, str]:

def _tick_size_error(self: AccountSet) -> Optional[str]:
if self.tick_size is None:
return
return None
if self.tick_size > _MAX_TICK_SIZE:
return f"`tick_size` is above {_MAX_TICK_SIZE}."
if self.tick_size < _MIN_TICK_SIZE and self.tick_size != _DISABLE_TICK_SIZE:
return f"`tick_size` is below {_MIN_TICK_SIZE}."
return
return None

def _transfer_rate_error(self: AccountSet) -> Optional[str]:
if self.transfer_rate is None:
return
return None
if self.transfer_rate > _MAX_TRANSFER_RATE:
return f"`transfer_rate` is above {_MAX_TRANSFER_RATE}."
if (
self.transfer_rate < _MIN_TRANSFER_RATE
and self.transfer_rate != _SPECIAL_CASE_TRANFER_RATE
):
return f"`transfer_rate` is below {_MIN_TRANSFER_RATE}."
return
return None
Loading