"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .calculatecashbalanceresponse_openordersummary import (
    CalculateCashBalanceResponseOpenOrderSummary,
    CalculateCashBalanceResponseOpenOrderSummaryTypedDict,
)
from .calculatecashbalanceresponse_tradesummary import (
    CalculateCashBalanceResponseTradeSummary,
    CalculateCashBalanceResponseTradeSummaryTypedDict,
)
from .calculatecashbalanceresponse_transfersummary import (
    CalculateCashBalanceResponseTransferSummary,
    CalculateCashBalanceResponseTransferSummaryTypedDict,
)
from ascend_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from pydantic import model_serializer
from typing import List, Optional
from typing_extensions import NotRequired, TypedDict


class AvailableCashToWithdrawAmountTypedDict(TypedDict):
    r"""The account's available cash to withdraw in USD. It is calculated based on the `open_balance_amount` and account activity. This value is always non-negative. If `unadjusted_available_cash_to_withdraw_amount` is negative, this value will be `0`."""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class AvailableCashToWithdrawAmount(BaseModel):
    r"""The account's available cash to withdraw in USD. It is calculated based on the `open_balance_amount` and account activity. This value is always non-negative. If `unadjusted_available_cash_to_withdraw_amount` is negative, this value will be `0`."""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class AvailableLiquidityAmountTypedDict(TypedDict):
    r"""The account's available liquidity in USD including the value of cash and equity positions. It is calculated based on the `open_liquidity_amount` and account activity. This value can be negative."""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class AvailableLiquidityAmount(BaseModel):
    r"""The account's available liquidity in USD including the value of cash and equity positions. It is calculated based on the `open_liquidity_amount` and account activity. This value can be negative."""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class FullyPaidUnsettledFundsAmountTypedDict(TypedDict):
    r"""The account's fully paid unsettled funds in USD. Fully paid unsettled funds are proceeds from sales of assets paid for with cash and not bought on margin. If `include_fully_paid_unsettled_funds` is `false`, this value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class FullyPaidUnsettledFundsAmount(BaseModel):
    r"""The account's fully paid unsettled funds in USD. Fully paid unsettled funds are proceeds from sales of assets paid for with cash and not bought on margin. If `include_fully_paid_unsettled_funds` is `false`, this value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class OpenBalanceAmountTypedDict(TypedDict):
    r"""The account's starting cash balance for the day in USD as calculated by the margin system. It is used as the base to calculate `available_cash_to_withdraw_amount`. This value is always non-negative."""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class OpenBalanceAmount(BaseModel):
    r"""The account's starting cash balance for the day in USD as calculated by the margin system. It is used as the base to calculate `available_cash_to_withdraw_amount`. This value is always non-negative."""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class OpenLiquidityAmountTypedDict(TypedDict):
    r"""The account's starting liquidity for the day in USD as calculated by the margin system. It includes the value of all asset positions (e.g. cash, equities, fixed income, etc). It is used as the base to calculate `available_liquidity_amount`. This value can be negative."""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class OpenLiquidityAmount(BaseModel):
    r"""The account's starting liquidity for the day in USD as calculated by the margin system. It includes the value of all asset positions (e.g. cash, equities, fixed income, etc). It is used as the base to calculate `available_liquidity_amount`. This value can be negative."""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PendingDebitDividendsAmountTypedDict(TypedDict):
    r"""The account's pending debit dividends in USD. The value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PendingDebitDividendsAmount(BaseModel):
    r"""The account's pending debit dividends in USD. The value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PendingDebitInterestAmountTypedDict(TypedDict):
    r"""The account's pending debit interest in USD. The value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PendingDebitInterestAmount(BaseModel):
    r"""The account's pending debit interest in USD. The value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class UnadjustedAvailableCashToWithdrawAmountTypedDict(TypedDict):
    r"""The account's unadjusted available cash to withdraw in USD. It is calculated based on the `open_balance_amount` and account activity. This value can be negative."""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class UnadjustedAvailableCashToWithdrawAmount(BaseModel):
    r"""The account's unadjusted available cash to withdraw in USD. It is calculated based on the `open_balance_amount` and account activity. This value can be negative."""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class WithheldDepositThresholdAmountTypedDict(TypedDict):
    r"""The account's withheld deposit threshold. Amounts in `withheld_deposits` up to this threshold will be not be subtracted to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class WithheldDepositThresholdAmount(BaseModel):
    r"""The account's withheld deposit threshold. Amounts in `withheld_deposits` up to this threshold will be not be subtracted to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class CalculateCashBalanceResponseTypedDict(TypedDict):
    r"""The cash balance of an account."""

    account: NotRequired[str]
    r"""The account."""
    available_cash_to_withdraw_amount: NotRequired[
        Nullable[AvailableCashToWithdrawAmountTypedDict]
    ]
    r"""The account's available cash to withdraw in USD. It is calculated based on the `open_balance_amount` and account activity. This value is always non-negative. If `unadjusted_available_cash_to_withdraw_amount` is negative, this value will be `0`."""
    available_liquidity_amount: NotRequired[Nullable[AvailableLiquidityAmountTypedDict]]
    r"""The account's available liquidity in USD including the value of cash and equity positions. It is calculated based on the `open_liquidity_amount` and account activity. This value can be negative."""
    fully_paid_unsettled_funds_amount: NotRequired[
        Nullable[FullyPaidUnsettledFundsAmountTypedDict]
    ]
    r"""The account's fully paid unsettled funds in USD. Fully paid unsettled funds are proceeds from sales of assets paid for with cash and not bought on margin. If `include_fully_paid_unsettled_funds` is `false`, this value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""
    include_fully_paid_unsettled_funds: NotRequired[bool]
    r"""Whether fully paid unsettled funds are included in balances."""
    intraday_buy_trades: NotRequired[
        List[CalculateCashBalanceResponseTradeSummaryTypedDict]
    ]
    r"""The account's intraday buy trades. The net amounts are **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since trades have positive amounts, the calculated values will **decrease**."""
    intraday_deposits: NotRequired[
        List[CalculateCashBalanceResponseTransferSummaryTypedDict]
    ]
    r"""The account's eligible intraday deposits that have posted to the ledger. Intraday deposits may be eligible based on mechanism (e.g. intraday ICT deposits are eligible for ICT withdrawals, but not ACH withdrawals). The amounts are **added** to `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since deposits have positive amounts, the calculated values will **increase**."""
    intraday_returns: NotRequired[
        List[CalculateCashBalanceResponseTransferSummaryTypedDict]
    ]
    r"""The account's intraday returns. The amounts are **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Returned deposits will **decrease** calculated values because they have positive amounts and returned withdrawals will **increase** calculated values because they have negative amounts."""
    intraday_withdrawals: NotRequired[
        List[CalculateCashBalanceResponseTransferSummaryTypedDict]
    ]
    r"""The account's intraday withdrawals that have posted to the ledger. The amounts are **added** to `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since withdrawals have negative amounts, the calculated values will **decrease**."""
    open_balance_amount: NotRequired[Nullable[OpenBalanceAmountTypedDict]]
    r"""The account's starting cash balance for the day in USD as calculated by the margin system. It is used as the base to calculate `available_cash_to_withdraw_amount`. This value is always non-negative."""
    open_buy_orders: NotRequired[
        List[CalculateCashBalanceResponseOpenOrderSummaryTypedDict]
    ]
    r"""The account's open buy orders. The expected notional ceilings are **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since open orders have positive expected notional ceilings, the calculated values will **decrease**."""
    open_liquidity_amount: NotRequired[Nullable[OpenLiquidityAmountTypedDict]]
    r"""The account's starting liquidity for the day in USD as calculated by the margin system. It includes the value of all asset positions (e.g. cash, equities, fixed income, etc). It is used as the base to calculate `available_liquidity_amount`. This value can be negative."""
    pending_debit_dividends_amount: NotRequired[
        Nullable[PendingDebitDividendsAmountTypedDict]
    ]
    r"""The account's pending debit dividends in USD. The value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""
    pending_debit_interest_amount: NotRequired[
        Nullable[PendingDebitInterestAmountTypedDict]
    ]
    r"""The account's pending debit interest in USD. The value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""
    pending_withdrawals: NotRequired[
        List[CalculateCashBalanceResponseTransferSummaryTypedDict]
    ]
    r"""The account's pending withdrawals. Pending withdrawals may need review and have yet to post to the ledger. The amounts are **added** to `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since withdrawals have negative amounts, the calculated values will **decrease**."""
    unadjusted_available_cash_to_withdraw_amount: NotRequired[
        Nullable[UnadjustedAvailableCashToWithdrawAmountTypedDict]
    ]
    r"""The account's unadjusted available cash to withdraw in USD. It is calculated based on the `open_balance_amount` and account activity. This value can be negative."""
    withheld_deposit_threshold_amount: NotRequired[
        Nullable[WithheldDepositThresholdAmountTypedDict]
    ]
    r"""The account's withheld deposit threshold. Amounts in `withheld_deposits` up to this threshold will be not be subtracted to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""
    withheld_deposits: NotRequired[
        List[CalculateCashBalanceResponseTransferSummaryTypedDict]
    ]
    r"""The account's withheld deposits. Deposits are withheld if there is a risk of a return (e.g. ACH deposits). Typically, they are withheld for 5 business days after completing. The amounts are **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since deposits have positive amounts, the calculated values will **decrease**. However, withheld deposits will not be subtracted up to the `withheld_deposit_threshold_amount`."""


class CalculateCashBalanceResponse(BaseModel):
    r"""The cash balance of an account."""

    account: Optional[str] = None
    r"""The account."""

    available_cash_to_withdraw_amount: OptionalNullable[
        AvailableCashToWithdrawAmount
    ] = UNSET
    r"""The account's available cash to withdraw in USD. It is calculated based on the `open_balance_amount` and account activity. This value is always non-negative. If `unadjusted_available_cash_to_withdraw_amount` is negative, this value will be `0`."""

    available_liquidity_amount: OptionalNullable[AvailableLiquidityAmount] = UNSET
    r"""The account's available liquidity in USD including the value of cash and equity positions. It is calculated based on the `open_liquidity_amount` and account activity. This value can be negative."""

    fully_paid_unsettled_funds_amount: OptionalNullable[
        FullyPaidUnsettledFundsAmount
    ] = UNSET
    r"""The account's fully paid unsettled funds in USD. Fully paid unsettled funds are proceeds from sales of assets paid for with cash and not bought on margin. If `include_fully_paid_unsettled_funds` is `false`, this value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    include_fully_paid_unsettled_funds: Optional[bool] = None
    r"""Whether fully paid unsettled funds are included in balances."""

    intraday_buy_trades: Optional[List[CalculateCashBalanceResponseTradeSummary]] = None
    r"""The account's intraday buy trades. The net amounts are **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since trades have positive amounts, the calculated values will **decrease**."""

    intraday_deposits: Optional[
        List[CalculateCashBalanceResponseTransferSummary]
    ] = None
    r"""The account's eligible intraday deposits that have posted to the ledger. Intraday deposits may be eligible based on mechanism (e.g. intraday ICT deposits are eligible for ICT withdrawals, but not ACH withdrawals). The amounts are **added** to `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since deposits have positive amounts, the calculated values will **increase**."""

    intraday_returns: Optional[List[CalculateCashBalanceResponseTransferSummary]] = None
    r"""The account's intraday returns. The amounts are **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Returned deposits will **decrease** calculated values because they have positive amounts and returned withdrawals will **increase** calculated values because they have negative amounts."""

    intraday_withdrawals: Optional[
        List[CalculateCashBalanceResponseTransferSummary]
    ] = None
    r"""The account's intraday withdrawals that have posted to the ledger. The amounts are **added** to `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since withdrawals have negative amounts, the calculated values will **decrease**."""

    open_balance_amount: OptionalNullable[OpenBalanceAmount] = UNSET
    r"""The account's starting cash balance for the day in USD as calculated by the margin system. It is used as the base to calculate `available_cash_to_withdraw_amount`. This value is always non-negative."""

    open_buy_orders: Optional[List[CalculateCashBalanceResponseOpenOrderSummary]] = None
    r"""The account's open buy orders. The expected notional ceilings are **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since open orders have positive expected notional ceilings, the calculated values will **decrease**."""

    open_liquidity_amount: OptionalNullable[OpenLiquidityAmount] = UNSET
    r"""The account's starting liquidity for the day in USD as calculated by the margin system. It includes the value of all asset positions (e.g. cash, equities, fixed income, etc). It is used as the base to calculate `available_liquidity_amount`. This value can be negative."""

    pending_debit_dividends_amount: OptionalNullable[
        PendingDebitDividendsAmount
    ] = UNSET
    r"""The account's pending debit dividends in USD. The value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    pending_debit_interest_amount: OptionalNullable[PendingDebitInterestAmount] = UNSET
    r"""The account's pending debit interest in USD. The value is **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    pending_withdrawals: Optional[
        List[CalculateCashBalanceResponseTransferSummary]
    ] = None
    r"""The account's pending withdrawals. Pending withdrawals may need review and have yet to post to the ledger. The amounts are **added** to `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since withdrawals have negative amounts, the calculated values will **decrease**."""

    unadjusted_available_cash_to_withdraw_amount: OptionalNullable[
        UnadjustedAvailableCashToWithdrawAmount
    ] = UNSET
    r"""The account's unadjusted available cash to withdraw in USD. It is calculated based on the `open_balance_amount` and account activity. This value can be negative."""

    withheld_deposit_threshold_amount: OptionalNullable[
        WithheldDepositThresholdAmount
    ] = UNSET
    r"""The account's withheld deposit threshold. Amounts in `withheld_deposits` up to this threshold will be not be subtracted to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. This value is always non-negative."""

    withheld_deposits: Optional[
        List[CalculateCashBalanceResponseTransferSummary]
    ] = None
    r"""The account's withheld deposits. Deposits are withheld if there is a risk of a return (e.g. ACH deposits). Typically, they are withheld for 5 business days after completing. The amounts are **subtracted** from `open_balance_amount` and `open_liquidity_amount` to calculate `available_cash_to_withdraw_amount` and `available_liquidity_amount`. Since deposits have positive amounts, the calculated values will **decrease**. However, withheld deposits will not be subtracted up to the `withheld_deposit_threshold_amount`."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "account",
            "available_cash_to_withdraw_amount",
            "available_liquidity_amount",
            "fully_paid_unsettled_funds_amount",
            "include_fully_paid_unsettled_funds",
            "intraday_buy_trades",
            "intraday_deposits",
            "intraday_returns",
            "intraday_withdrawals",
            "open_balance_amount",
            "open_buy_orders",
            "open_liquidity_amount",
            "pending_debit_dividends_amount",
            "pending_debit_interest_amount",
            "pending_withdrawals",
            "unadjusted_available_cash_to_withdraw_amount",
            "withheld_deposit_threshold_amount",
            "withheld_deposits",
        ]
        nullable_fields = [
            "available_cash_to_withdraw_amount",
            "available_liquidity_amount",
            "fully_paid_unsettled_funds_amount",
            "open_balance_amount",
            "open_liquidity_amount",
            "pending_debit_dividends_amount",
            "pending_debit_interest_amount",
            "unadjusted_available_cash_to_withdraw_amount",
            "withheld_deposit_threshold_amount",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in self.model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
