warn on ambiguous -p plugin module usage#8
Conversation
⚡ Risk Assessment —
|
| Files | Summary |
|---|---|
Plugin Loading Ambiguity Detectionsrc/_pytest/config/__init__.pytesting/test_config.pychangelog/14135.bugfix.rst |
Detects and warns when -p loads a module with no pytest hooks but a pytest11 entry-point exists for a submodule, helping users identify the correct plugin to load. |
Sequence Diagram
sequenceDiagram
participant User
participant PytestConfig
participant PluginManager
participant EntryPointScanner
User->>PytestConfig: pytest -p pytest_recording
PytestConfig->>PluginManager: import_plugin("pytest_recording", consider_entry_points=True)
PluginManager->>PluginManager: load_setuptools_entrypoints("pytest11", name="pytest_recording")
PluginManager->>PluginManager: loaded = False (no direct entry-point)
PluginManager->>PluginManager: import pytest_recording module
PluginManager->>PluginManager: register(mod, "pytest_recording")
PluginManager->>EntryPointScanner: _warn_about_submodule_entrypoint_plugin("pytest_recording", mod)
EntryPointScanner->>EntryPointScanner: _plugin_has_pytest_hooks(mod) = False
EntryPointScanner->>EntryPointScanner: scan distributions for pytest11 entry-points
EntryPointScanner->>EntryPointScanner: find entry-points with value="pytest_recording.*"
EntryPointScanner->>User: warn with suggestion: -p recording
Dig Deeper With Commands
/review <file-path> <function-optional>/chat <file-path> "<question>"/roast <file-path>
Runs only when explicitly triggered.
| if ep.group == "pytest11" | ||
| and ep.name != modname | ||
| and isinstance(getattr(ep, "value", None), str) | ||
| and getattr(ep, "value").split(":", 1)[0].startswith(modname_prefix) |
There was a problem hiding this comment.
Iterates all installed distributions on every ambiguous check
importlib.metadata.distributions() scans all installed packages. This runs every time a -p loaded module has no hooks. Consider caching the result or using importlib.metadata.entry_points(group='pytest11') which is more targeted and faster on Python 3.12+.
| if ep.group == "pytest11" | ||
| and ep.name != modname | ||
| and isinstance(getattr(ep, "value", None), str) | ||
| and getattr(ep, "value").split(":", 1)[0].startswith(modname_prefix) |
There was a problem hiding this comment.
Double getattr call risks AttributeError on race
Line 920 checks getattr(ep, 'value', None) but line 921 calls getattr(ep, 'value') without default. If value is a property that could change or raise, this could throw. Use a single assignment: val = getattr(ep, 'value', None) then check isinstance(val, str) and val.split(...)....
Actionable Comments Posted: 2🧹 Nitpick comments (1)\`\_plugin\_has\_pytest\_hooks\` has no explicit return type path - src/_pytest/config/__init__.py (940)🧾 Coverage Summary✔️ Covered (3 files) |
|
/review --force |
Actionable Comments Posted: 0👀 Worth a Look (1)[MEDIUM] Hook detection can still raise on exotic plugin attributes - src/_pytest/config/__init__.py (938)🧹 Nitpick comments (1)Entry-point \`value\` is read twice in the new suggestion filter - src/_pytest/config/__init__.py (920)🧾 Coverage Summary✔️ Covered (3 files) |
|
/review --force |
Mirror of pytest-dev#14270
Summary by MergeMonkey