Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit 4d4c544

Browse files
committed
Make checkers extensible via entry point
This commit adds an entry point called pydocstyle_styles which allows checks to be loaded dynamically.
1 parent e5ec1a8 commit 4d4c544

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@
4141
'console_scripts': [
4242
'pydocstyle = pydocstyle.cli:main',
4343
],
44+
'pydocstyle_styles': [
45+
'pydocstyle.base = pydocstyle.checkers.style.base',
46+
'pydocstyle.numpy = pydocstyle.checkers.style.numpy',
47+
'pydocstyle.other = pydocstyle.checkers.style.other',
48+
]
4449
},
4550
)

src/pydocstyle/checkers/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
from pydocstyle.checkers.hooks import get_checkers
2-
# These imports below are required for registering the
3-
# checkers so that `get_checkers` captures them.
4-
from pydocstyle.checkers.style import numpy, base, other
52

6-
__all__ = ('get_checkers', 'numpy', 'base', 'other')
3+
__all__ = ('get_checkers',)

src/pydocstyle/checkers/hooks.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
import pkg_resources
2+
3+
from typing import Set
4+
5+
from pydocstyle.utils import log
6+
17
__registered_checkers = []
8+
__loaded_styles = set() # type: Set[str]
9+
10+
ENTRY_POINT_NAME = 'pydocstyle_styles'
11+
212

313
def check_for(kind, terminal=False):
414
def decorator(f):
@@ -9,5 +19,18 @@ def decorator(f):
919
return decorator
1020

1121

22+
def _load_styles():
23+
for entry_point in pkg_resources.iter_entry_points(ENTRY_POINT_NAME):
24+
if entry_point.name not in __loaded_styles:
25+
try:
26+
entry_point.load()
27+
except Exception as error:
28+
log.exception("Unable to load plugin %s.\nError occurred: %s",
29+
entry_point.name, error)
30+
else:
31+
__loaded_styles.add(entry_point.name)
32+
33+
1234
def get_checkers():
35+
_load_styles()
1336
return iter(__registered_checkers)

0 commit comments

Comments
 (0)