Skip to content

Commit 5bc025e

Browse files
authored
FIX-#7576: Fix ambiguous AttributeError message (#7577)
Fixes an ambiguous error message raised on AttributeErrors due to calling a missing __getattr__ implementation. Signed-off-by: Jonathan Shi <jonathan.shi@snowflake.com>
1 parent f200245 commit 5bc025e

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

modin/pandas/dataframe.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,12 +2647,9 @@ def __getattr__(self, key) -> Any:
26472647
"""
26482648
# NOTE that to get an attribute, python calls __getattribute__() first and
26492649
# then falls back to __getattr__() if the former raises an AttributeError.
2650-
try:
2651-
return super().__getattr__(key)
2652-
except AttributeError as err:
2653-
if key not in _ATTRS_NO_LOOKUP and key in self.columns:
2654-
return self[key]
2655-
raise err
2650+
if key not in _ATTRS_NO_LOOKUP and key in self.columns:
2651+
return self[key]
2652+
raise AttributeError(f"'DataFrame' object has no attribute '{key}'")
26562653

26572654
def __setattr__(self, key, value) -> None:
26582655
"""

modin/pandas/series.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,9 @@ def __getattr__(self, key: Hashable) -> Any:
390390
"""
391391
# NOTE that to get an attribute, python calls __getattribute__() first and
392392
# then falls back to __getattr__() if the former raises an AttributeError.
393-
try:
394-
return super().__getattr__(key)
395-
except AttributeError as err:
396-
if key not in _ATTRS_NO_LOOKUP and key in self._query_compiler.index:
397-
return self[key]
398-
raise err
393+
if key not in _ATTRS_NO_LOOKUP and key in self._query_compiler.index:
394+
return self[key]
395+
raise AttributeError(f"'Series' object has no attribute '{key}'")
399396

400397
__float__ = _coerce_method(float)
401398
__int__ = _coerce_method(int)

modin/tests/pandas/extensions/test_dataframe_extensions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ def test_search_for_missing_attribute_in_overridden_columns(self, Backend1):
224224

225225
df = pd.DataFrame({column_name: ["a"]}).set_backend(Backend1)
226226

227-
with pytest.raises(AttributeError):
227+
with pytest.raises(
228+
AttributeError,
229+
match="'DataFrame' object has no attribute 'non_existent_column'",
230+
):
228231
getattr(df, "non_existent_column")
229232
column_getter.assert_called_once_with(df)
230233

0 commit comments

Comments
 (0)