alexgolec / schwab-py

Unofficial API wrapper for the Schwab HTTP API
MIT License
210 stars 49 forks source link

Pydantic support request #153

Closed punitarani closed 4 months ago

punitarani commented 4 months ago

Are there plans to add pydantic support?

We can easily generate the models from the OpenAPI spec. Models: https://gist.github.com/punitarani/5d12b575a7a07d850790e121fda8a65b

Pydantic model validation time is negligible.

Options Chain API call times

Mean: 0.69 seconds
Standard Deviation: 0.11 seconds

Min: 0.57 seconds
Max: 1.12 seconds

Options Chain Pydantic Validation Times:

Mean: 0.07 milliseconds
Standard Deviation: 0.02 milliseconds

Min: 0.04 milliseconds
Max: 0.16 milliseconds
alexgolec commented 4 months ago

Can you elaborate on what pydantic support would entail? Is the proposal to simply validate each piece of data as it comes in?

punitarani commented 4 months ago

Yes validation and the functions would return an object instead of the raw json response.

For example:

class OptionChain(BaseModel):
    symbol: Optional[str] = None
    status: Optional[str] = None
    underlying: Optional[Underlying] = None
    strategy: Optional[Strategy] = None
    interval: Optional[float] = None
    isDelayed: Optional[bool] = None
    isIndex: Optional[bool] = None
    daysToExpiration: Optional[float] = None
    interestRate: Optional[float] = None
    underlyingPrice: Optional[float] = None
    volatility: Optional[float] = None
    callExpDateMap: Optional[Dict[str, OptionContractMap]] = None
    putExpDateMap: Optional[Dict[str, OptionContractMap]] = None
def get_option_chain(...) -> OptionChain:
    ...
    path = '/marketdata/v1/chains'
    data = self._get_request(path, params)
    return OptionChain.model_validate(data.json())
alexgolec commented 4 months ago

I see the appeal but I'm closing this as WON'T FIX for a few reasons: