MCPcopy Index your code
hub / github.com/ipython/ipython / test_pandas_bad_series

Function test_pandas_bad_series

tests/test_guarded_eval.py:149–180  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

147
148@dec.skip_without("pandas")
149def test_pandas_bad_series():
150 import pandas as pd
151
152 class BadItemSeries(pd.Series):
153 def __getitem__(self, key):
154 return "CUSTOM_ITEM"
155
156 class BadAttrSeries(pd.Series):
157 def __getattr__(self, key):
158 return "CUSTOM_ATTR"
159
160 bad_series = BadItemSeries([1], index=["a"])
161 context = limited(data=bad_series)
162
163 with pytest.raises(GuardRejection):
164 guarded_eval('data["a"]', context)
165 with pytest.raises(GuardRejection):
166 guarded_eval('data["c"]', context)
167
168 # note: here result is a bit unexpected because
169 # pandas `__getattr__` calls `__getitem__`;
170 # FIXME - special case to handle it?
171 assert guarded_eval("data.a", context) == "CUSTOM_ITEM"
172
173 context = unsafe(data=bad_series)
174 assert guarded_eval('data["a"]', context) == "CUSTOM_ITEM"
175
176 bad_attr_series = BadAttrSeries([1], index=["a"])
177 context = limited(data=bad_attr_series)
178 assert guarded_eval('data["a"]', context) == 1
179 with pytest.raises(GuardRejection):
180 guarded_eval("data.a", context)
181
182
183@dec.skip_without("pandas")

Callers

nothing calls this directly

Calls 3

guarded_evalFunction · 0.90
BadItemSeriesClass · 0.85
BadAttrSeriesClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…