Skip to content

Commit b547449

Browse files
committed
Support running docstub on a single file
This helps with testing and with creating simple examples for the docs.
1 parent f673339 commit b547449

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

src/docstub/_cli.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def report_execution_time():
132132

133133
@click.command()
134134
@click.version_option(__version__)
135-
@click.argument("source_dir", type=click.Path(exists=True, file_okay=False))
135+
@click.argument("root_path", type=click.Path(exists=True))
136136
@click.option(
137137
"-o",
138138
"--out-dir",
@@ -154,7 +154,7 @@ def report_execution_time():
154154
@click.option("-v", "--verbose", count=True, help="Log more details.")
155155
@click.help_option("-h", "--help")
156156
@report_execution_time()
157-
def main(source_dir, out_dir, config_path, group_errors, verbose):
157+
def main(root_path, out_dir, config_path, group_errors, verbose):
158158
"""Generate Python stub files from docstrings.
159159
\f
160160
@@ -170,26 +170,29 @@ def main(source_dir, out_dir, config_path, group_errors, verbose):
170170

171171
_setup_logging(verbose=verbose)
172172

173-
source_dir = Path(source_dir)
173+
root_path = Path(root_path)
174174
config = _load_configuration(config_path)
175-
known_imports = _build_import_map(config, source_dir)
175+
known_imports = _build_import_map(config, root_path)
176176

177177
reporter = GroupedErrorReporter() if group_errors else ErrorReporter()
178178
types_db = TypesDatabase(
179-
source_pkgs=[source_dir.parent.resolve()], known_imports=known_imports
179+
source_pkgs=[root_path.parent.resolve()], known_imports=known_imports
180180
)
181181
stub_transformer = Py2StubTransformer(
182182
types_db=types_db, replace_doctypes=config.replace_doctypes, reporter=reporter
183183
)
184184

185185
if not out_dir:
186-
out_dir = source_dir.parent / (source_dir.name + "-stubs")
186+
if root_path.is_file():
187+
out_dir = root_path.parent
188+
else:
189+
out_dir = root_path.parent / (root_path.name + "-stubs")
187190
out_dir = Path(out_dir)
188191
out_dir.mkdir(parents=True, exist_ok=True)
189192

190193
# Stub generation ---------------------------------------------------------
191194

192-
for source_path, stub_path in walk_source_and_targets(source_dir, out_dir):
195+
for source_path, stub_path in walk_source_and_targets(root_path, out_dir):
193196
if source_path.suffix.lower() == ".pyi":
194197
logger.debug("using existing stub file %s", source_path)
195198
with source_path.open() as fo:

src/docstub/_stubs.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ def walk_source(root_dir):
6565
yield path
6666

6767

68-
def walk_source_and_targets(root_dir, target_dir):
68+
def walk_source_and_targets(root_path, target_dir):
6969
"""Iterate modules in a Python package and its target stub files.
7070
7171
Parameters
7272
----------
73-
root_dir : Path
74-
Root directory of a Python package.
73+
root_path : Path
74+
Root directory of a Python package or a single Python file.
7575
target_dir : Path
7676
Root directory in which a matching stub package will be created.
7777
@@ -81,13 +81,14 @@ def walk_source_and_targets(root_dir, target_dir):
8181
Either a Python file or a stub file that takes precedence.
8282
stub_path : Path
8383
Target stub file.
84-
85-
Notes
86-
-----
87-
Files starting with "test_" are skipped entirely for now.
8884
"""
89-
for source_path in walk_source(root_dir):
90-
stub_path = target_dir / source_path.with_suffix(".pyi").relative_to(root_dir)
85+
if root_path.is_file():
86+
stub_path = target_dir / root_path.with_suffix(".pyi").name
87+
yield root_path, stub_path
88+
return
89+
90+
for source_path in walk_source(root_path):
91+
stub_path = target_dir / source_path.with_suffix(".pyi").relative_to(root_path)
9192
yield source_path, stub_path
9293

9394

0 commit comments

Comments
 (0)