alpacahq / alpacahq/alpaca-py

[Bug]: BarSet.__contains__ returns False even when data exists

Ouverte
#641 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Python
Étoiles
1.5k
Forks
398
Merge moyen
1 j 16 h
PR mergées (30 j)
6

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Current Behavior

The BarSet class returned by StockHistoricalDataClient.get_stock_bars() has a broken __contains__ implementation. Using symbol in bars always returns False even when the symbol's data exists in bars.data.

Actual Behavior

- 'SPY' in bars → False
- 'SPY' in bars.data → True
- bars['SPY'] → Works correctly (returns list of bars)

Environment

- alpaca-py version: 0.33.1, reconfirmed in 0.43.2
- Python version: 3.11

Suggested Fix

Add a __contains__ method to BarSet that delegates to self.data:

def __contains__(self, key):
return key in self.data

### Expected Behavior

When I call get_stock_bars() and check if a symbol exists using 'SPY' in bars, it should return True if bar data for that symbol was returned by the API.

### SDK Version I encountered this issue in

Bug found in 0.33.1
Reconfirmed that bug exists in 0.43.2

### Steps To Reproduce

```markdown
Reproduction Steps

from alpaca.data import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest
from alpaca.data.timeframe import TimeFrame
from datetime import datetime
import pytz

ET = pytz.timezone('America/New_York')

client = StockHistoricalDataClient('', '')

now = datetime.now(ET)
market_open = now.replace(hour=9, minute=30, second=0, microsecond=0)

req = StockBarsRequest(
symbol_or_symbols='SPY',
timeframe=TimeFrame.Minute,
start=market_open
)
bars = client.get_stock_bars(req)

# BUG: These produce inconsistent results
print(f"'SPY' in bars: {'SPY' in bars}") # False (WRONG!)
print(f"'SPY' in bars.data: {'SPY' in bars.data}") # True (correct)
print(f"len(bars['SPY']): {len(bars['SPY'])}") # Works, returns 80+ bars

Expected Behavior

'SPY' in bars should return True when bars['SPY'] contains data.
```

### Filled out the Steps to Reproduce section?

- [x] I have entered valid steps to reproduce my issue or have attached a minimally reproducible case in code that shows my issue happening; and understand that without this my issue will be flagged as invalid and closed after 30 days.

### Anything else?

_No response_

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.