Skip to content

Commit 284eb5e

Browse files
committed
Rework get_pkg_funcs() to include subpackages
submodules were not being parsed properly
1 parent 929a050 commit 284eb5e

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

sphinx_github_style/meth_lexer.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
import types
2+
from pathlib import Path
23
from typing import Type, Optional
34
from pygments.lexers.python import NumPyLexer
45
from inspect import getmembers, isfunction, ismethod, ismodule, isclass
56

67

7-
def get_pkg_funcs(pkg: types.ModuleType):
8-
# Get funcs/meths defined in pkg.__init__
9-
funcs_meths = get_funcs(pkg)
10-
modules = getmembers(pkg, ismodule)
11-
# Get funcs/meths of each module in pkg
12-
for name, module in modules:
13-
funcs_meths += get_funcs(module)
14-
classes = getmembers(module, isclass)
15-
for class_name, _class in classes:
16-
funcs_meths += get_funcs(_class)
17-
# Set of all funcs/meths contained in modules used by package
18-
return set(funcs_meths)
8+
def get_pkg_funcs(pkg_module: types.ModuleType, funcs_meths=set(), processed_modules=set()):
9+
funcs_meths.update(get_funcs(pkg_module))
10+
processed_modules.add(pkg_module)
11+
12+
for class_name, _class in getmembers(pkg_module, isclass):
13+
funcs_meths.update(get_funcs(_class))
14+
15+
for mod_name, mod in getmembers(pkg_module, ismodule):
16+
if mod in processed_modules:
17+
continue
18+
19+
try:
20+
is_pkg = Path(mod.__file__).name == '__init__.py'
21+
except AttributeError: # It's a built-in module
22+
is_pkg = False
23+
24+
if not is_pkg: # If it's not a subpackage, get all funcs/meths defined in it
25+
funcs_meths.update(get_funcs(mod))
26+
processed_modules.add(mod)
27+
28+
else: # If it's a subpackage, call recursively to process all submodules
29+
get_pkg_funcs(mod, funcs_meths, processed_modules)
30+
31+
return funcs_meths
1932

2033

2134
def get_funcs(of):

0 commit comments

Comments
 (0)