16
16
from ._cache import FileCache
17
17
from ._config import Config
18
18
from ._stubs import (
19
+ STUB_HEADER_COMMENT ,
19
20
Py2StubTransformer ,
20
21
try_format_stub ,
21
- walk_source ,
22
+ walk_python_package ,
22
23
walk_source_and_targets ,
23
24
)
24
25
from ._utils import ErrorReporter , GroupedErrorReporter
27
28
logger = logging .getLogger (__name__ )
28
29
29
30
30
- STUB_HEADER_COMMENT = "# File generated with docstub"
31
-
32
-
33
31
def _load_configuration (config_path = None ):
34
32
"""Load and merge configuration from CWD and optional files.
35
33
@@ -78,13 +76,13 @@ def _setup_logging(*, verbose):
78
76
)
79
77
80
78
81
- def _build_import_map (config , source_dir ):
79
+ def _build_import_map (config , root_path ):
82
80
"""Build a map of known imports.
83
81
84
82
Parameters
85
83
----------
86
84
config : ~.Config
87
- source_dir : Path
85
+ root_path : Path
88
86
89
87
Returns
90
88
-------
@@ -98,10 +96,11 @@ def _build_import_map(config, source_dir):
98
96
cache_dir = Path .cwd () / ".docstub_cache" ,
99
97
name = f"{ __version__ } /collected_types" ,
100
98
)
101
- for source_path in walk_source (source_dir ):
102
- logger .info ("collecting types in %s" , source_path )
103
- known_imports_in_source = collect_cached_types (source_path )
104
- known_imports .update (known_imports_in_source )
99
+ if root_path .is_dir ():
100
+ for source_path in walk_python_package (root_path ):
101
+ logger .info ("collecting types in %s" , source_path )
102
+ known_imports_in_source = collect_cached_types (source_path )
103
+ known_imports .update (known_imports_in_source )
105
104
106
105
known_imports .update (KnownImport .many_from_config (config .known_imports ))
107
106
@@ -138,7 +137,7 @@ def report_execution_time():
138
137
"--out-dir" ,
139
138
type = click .Path (file_okay = False ),
140
139
metavar = "PATH" ,
141
- help = "Set output directory explicitly." ,
140
+ help = "Set output directory explicitly. Otherwise, stubs are generated inplace. " ,
142
141
)
143
142
@click .option (
144
143
"--config" ,
@@ -176,7 +175,7 @@ def main(root_path, out_dir, config_path, group_errors, allow_errors, verbose):
176
175
177
176
Parameters
178
177
----------
179
- source_dir : Path
178
+ root_path : Path
180
179
out_dir : Path
181
180
config_path : Path
182
181
group_errors : bool
@@ -189,6 +188,12 @@ def main(root_path, out_dir, config_path, group_errors, allow_errors, verbose):
189
188
_setup_logging (verbose = verbose )
190
189
191
190
root_path = Path (root_path )
191
+ if root_path .is_file ():
192
+ logger .warning (
193
+ "Running docstub on a single file is experimental. Relative imports "
194
+ "or type references won't work."
195
+ )
196
+
192
197
config = _load_configuration (config_path )
193
198
known_imports = _build_import_map (config , root_path )
194
199
@@ -204,7 +209,7 @@ def main(root_path, out_dir, config_path, group_errors, allow_errors, verbose):
204
209
if root_path .is_file ():
205
210
out_dir = root_path .parent
206
211
else :
207
- out_dir = root_path . parent / ( root_path . name + "-stubs" )
212
+ out_dir = root_path
208
213
out_dir = Path (out_dir )
209
214
out_dir .mkdir (parents = True , exist_ok = True )
210
215
0 commit comments