Skip to content

Commit 2807683

Browse files
committed
Improve warning message
1 parent 01a1bfc commit 2807683

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

pandas/core/accessor.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -406,23 +406,29 @@ class AccessorEntryPointLoader: # is this a good name for the class?
406406
@classmethod
407407
def load(cls) -> None:
408408
"""loads and registers accessors defined by 'pandas_accessor'."""
409-
packages = entry_points(group=cls.ENTRY_POINT_GROUP)
410-
unique_packages_names: set[str] = set()
411-
412-
for package in packages:
413-
# Verifies duplicated package names
414-
if package.name in unique_packages_names:
409+
accessors = entry_points(group=cls.ENTRY_POINT_GROUP)
410+
unique_accessors_names: set[str] = set()
411+
412+
for accessor in accessors:
413+
# Verifies duplicated accessor names
414+
if accessor.name in unique_accessors_names:
415+
try:
416+
pkg_name: str = accessor.dist.name
417+
except Exception:
418+
pkg_name = "unknown"
415419
warnings.warn(
416-
"Warning: you have two packages with the same name:"
417-
f" '{package.name}'\n"
418-
"Uninstall the package you don't want to use "
419-
"in order to remove this warning.\n",
420+
"Warning: you have two accessors with the same name:"
421+
f" '{accessor.name}' has already been registered"
422+
f" by the package '{pkg_name}'. So the '{accessor.name}' "
423+
f"provided by the package '{pkg_name}' is not "
424+
f"being used. Uninstall the package you don't want"
425+
"to use if you want to get rid of this warning.\n",
420426
UserWarning,
421427
stacklevel=2,
422428
)
423429

424430
else:
425-
unique_packages_names.add(package.name)
431+
unique_accessors_names.add(accessor.name)
426432

427433
def make_property(ep):
428434
def accessor(self) -> Any:
@@ -431,4 +437,5 @@ def accessor(self) -> Any:
431437

432438
return accessor
433439

434-
register_dataframe_accessor(package.name)(make_property(package))
440+
# _register_accessor()
441+
register_dataframe_accessor(accessor.name)(make_property(accessor))

pandas/tests/test_plugis_entrypoint_loader.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pandas._testing as tm
33
from pandas.core.accessor import AccessorEntryPointLoader
44

5+
# TODO: test for pkg names
6+
57

68
def test_no_accessors(monkeypatch):
79
# GH29076
@@ -91,7 +93,7 @@ def mock_entry_points(*, group):
9193
AccessorEntryPointLoader.load()
9294

9395
messages = [str(w.message) for w in record]
94-
assert any("you have two packages with the same name:" in msg for msg in messages)
96+
assert any("you have two accessors with the same name:" in msg for msg in messages)
9597

9698
df = pd.DataFrame({"x": [1, 2, 3]})
9799
assert hasattr(df, "duplicate_accessor")
@@ -203,16 +205,16 @@ def mock_entry_points(*, group):
203205

204206
messages = [str(w.message) for w in record]
205207

206-
# Filter warnings for the specific message about duplicate packages
208+
# Filter warnings for the specific message about duplicate accessors
207209
duplicate_package_warnings = [
208210
msg
209211
for msg in messages
210-
if "you have two packages with the same name: 'duplicate_accessor'" in msg
212+
if "you have two accessors with the same name: 'duplicate_accessor'" in msg
211213
]
212214

213-
# Assert one warning about duplicate packages
215+
# Assert one warning about duplicate accessors
214216
assert len(duplicate_package_warnings) == 1, (
215-
f"Expected exactly one warning about duplicate packages, "
217+
f"Expected exactly one warning about duplicate accessors, "
216218
f"got {len(duplicate_package_warnings)}: {duplicate_package_warnings}"
217219
)
218220

0 commit comments

Comments
 (0)