alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
537 stars 134 forks source link

[Bug]: noisy model_* pydantic attributes #365

Closed impredicative closed 10 months ago

impredicative commented 10 months ago

Is there an existing issue for this?

Current Behavior

When I get a snapshot using v0.12.0, there are noisy and unnecessary model_* attributes all over the place. This was not an issue with v0.10.0.

They can be listed by seeing:

[attr for attr in dir(snapshot) if attr.startswith('model_')]

['model_computed_fields', 'model_config', 'model_construct', 'model_copy', 'model_dump', 'model_dump_json', 'model_extra', 'model_fields', 'model_fields_set', 'model_json_schema', 'model_parametrized_name', 'model_post_init', 'model_rebuild', 'model_validate', 'model_validate_json', 'model_validate_strings']

This applies recursively to all pydantic objects.

Expected Behavior

These internal model attributes should ideally be hidden via a leading underscore if they need to exist at all.

SDK Version I encountered this issue in

0.12.0

Steps To Reproduce

snapshot = stock_historical_data_client.get_stock_snapshot(StockSnapshotRequest(symbol_or_symbols='TQQQ', feed=SIP))['TQQQ']

Filled out the Steps to Reproduce section?

Anything else?

No response

alessiocastrica commented 10 months ago

Hi @impredicative this is not a bug, it's expected since you're using the python built-in function dir() which is returning a list of all valid attributes in an object. Since snapshot is an instance of a sub-class of a pydantic BaseModel it inherits all the attributes of a standard BaseModel, including the ones you listed. There's no code on alpaca-py to add any of those fields, and this is just related to the fact that we use pydantic for data validation.

I'm not sure what's your use case, but if you don't want to see those attributes, you simply don't need to use the dir() function. The representation of the snapshot object won't include them, and they're not included in the model_fields as well. Screenshot 2023-10-17 at 14 19 13

impredicative commented 10 months ago

An IDE such as PyCharm visibly and graphically shows the distracting model attributes, which is why I opened this issue. I do not use dir for development purposes.