Skip to content

WIP: ensure names are pinned correctly #24429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

import inspect
import re
import sys
from datetime import datetime, timedelta
Expand Down Expand Up @@ -1273,3 +1274,48 @@ def test_to_numpy_dtype(as_series):
expected = np.array(['2000-01-01T05', '2001-01-01T05'],
dtype='M8[ns]')
tm.assert_numpy_array_equal(result, expected)


def check_pinned_names(cls):
"""
Check that the any dynamically-defined methods have the correct
names, i.e. not 'wrapper'.
"""
special_cases = {
"isnull": "isna",
"notnull": "notna",
"take": "take_nd", # Categorical
"to_list": "tolist", # Categorical
"iteritems": "items",
"__bool__": "__nonzero__",
}
ignore = {
"_create_comparison_method",
}
if 'Subclassed' in cls.__name__:
# dummy classes defined in tests
return
for name in dir(cls):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This iteration looks heavy. What time does it take to run through all cases defined in the test_pinned_names parametrization? Should test_pinned_names below get a pytest slow marker?

Would it not be better to do specific tests for the cases where methods are generated dynamically instead of this broader test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What time does it take to run through all cases defined in the test_pinned_names parametrization? Should test_pinned_names below get a pytest slow marker?

Locally python -m pytest pandas/tests/test_base.py -k test_pinned_names takes 1.02 seconds.

Would it not be better to do specific tests for the cases where methods are generated dynamically instead of this broader test?

No, that's an invitation to have things fall through the cracks down the road.

try:
# e.g. properties may not be accessible on the class
attr = getattr(cls, name)
except Exception:
continue
if name in ignore:
continue
if inspect.ismethod(attr) or isinstance(attr, property):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Python3 methods on (non-instantiated) classes are functions, so for those ismethod will fail. You need to add inspect.isfunction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks

expected = special_cases.get(name, name)
result = attr.__name__
assert result == expected, (result, expected, name,
cls.__name__)


@pytest.mark.parametrize('klass',
PandasObject.__subclasses__() + [
pd.Timestamp,
pd.Period,
pd.Timedelta,
pd.Interval
])
def test_pinned_names(klass):
check_pinned_names(klass)