Skip to content

Commit f01e421

Browse files
committed
create --stubs subdir for module-only packages
1 parent 6062e6d commit f01e421

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

src/strip_python3.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ def main() -> int:
237237
cmdline.add_option("-6", "--py36", action="count", default=0, help="alias --no-make-pyi --python-version=3.6")
238238
cmdline.add_option("-7", "--pyi37", action="count", default=0, help="alias --pyi-version=3.7")
239239
cmdline.add_option("-9", "--py39", action="count", default=0, help="alias --no-make-pyi --python-version=3.9")
240-
cmdline.add_option("-n", "--no-make-pyi", "--no-pyi", action="count", default=0, help="do not generate file.pyi includes")
240+
cmdline.add_option("-Y", "--make-stubs", "--stubs", action="count", default=0, help="generate file-stubs/__init__.pyi for mypy")
241241
cmdline.add_option("-y", "--make-pyi", "--pyi", action="count", default=0, help="generate file.pyi includes as well")
242+
cmdline.add_option("-n", "--no-make-pyi", "--no-pyi", action="count", default=0, help="do not generate any pyi includes")
242243
cmdline.add_option("-o", "--outfile", metavar="FILE", default=NIX, help="explicit instead of file3_2.py")
243244
cmdline_set_defaults_from(cmdline, want.toolsection, want.pyproject_toml, want.setup_cfg)
244245
opt, cmdline_args = cmdline.parse_args()
@@ -366,8 +367,9 @@ def main() -> int:
366367
eachfile = EACH_REMOVE3 if opt.remove3 else 0
367368
eachfile |= EACH_APPEND2 if opt.append2 else 0
368369
eachfile |= EACH_INPLACE if opt.inplace else 0
369-
make_pyi = opt.make_pyi or opt.append2 or opt.remove3 or opt.inplace
370-
return transformfiles(cmdline_args, eachfile=eachfile, outfile=opt.outfile, pyi=make_pyi and not no_make_pyi, minversion=back_version)
370+
make_pyi = opt.make_pyi or opt.append2 or opt.remove3 or opt.inplace
371+
return transformfiles(cmdline_args, eachfile=eachfile, outfile=opt.outfile, minversion=back_version,
372+
pyi = "i" if make_pyi and not no_make_pyi else NIX, stubs = "*-stubs/__init__py" if opt.make_stubs and not no_make_pyi else NIX)
371373

372374
def cmdline_set_defaults_from(cmdline: OptionParser, toolsection: str, *files: str) -> Dict[str, Union[str, int]]:
373375
defnames: Dict[str, str] = OrderedDict()
@@ -2225,7 +2227,7 @@ def pyi_copy_imports(pyi: ast.Module, py1: ast.AST, py2: ast.AST) -> ast.Module:
22252227
EACH_REMOVE3 = 1
22262228
EACH_APPEND2 = 2
22272229
EACH_INPLACE = 4
2228-
def transformfiles(args: List[str], eachfile: int = 0, outfile: str = "", pyi: int = 0, minversion: Tuple[int, int] = (2,7)) -> int:
2230+
def transformfiles(args: List[str], eachfile: int = 0, outfile: str = "", pyi: str = NIX, stubs: str = NIX, minversion: Tuple[int, int] = (2,7)) -> int:
22292231
written: List[str] = []
22302232
for arg in args:
22312233
with open(arg, "r", encoding="utf-8") as f:
@@ -2267,11 +2269,9 @@ def transformfiles(args: List[str], eachfile: int = 0, outfile: str = "", pyi: i
22672269
w.write(done)
22682270
if done and not done.endswith("\n"):
22692271
w.write("\n")
2270-
logg.info("written %s", out)
2272+
logg.log(DONE, "written %s", out)
22712273
written.append(out)
2272-
if pyi:
2273-
typehintsfile = out+"i"
2274-
logg.debug("--pyi => %s", typehintsfile)
2274+
if pyi or stubs:
22752275
type_ignores: List[TypeIgnore] = []
22762276
if isinstance(tree1, ast.Module):
22772277
type_ignores = tree1.type_ignores
@@ -2282,10 +2282,21 @@ def transformfiles(args: List[str], eachfile: int = 0, outfile: str = "", pyi: i
22822282
print("## typehints:")
22832283
print(done)
22842284
else:
2285-
with open(typehintsfile, "w", encoding="utf-8") as w:
2286-
w.write(done)
2287-
if done and not done.endswith("\n"):
2288-
w.write("\n")
2285+
for suffix in [pyi, stubs]:
2286+
if not suffix: continue
2287+
out_name, _ = os.path.splitext(out)
2288+
typehintsfile = suffix.replace("*", out_name) if "*" in suffix else out+suffix
2289+
logg.debug("typehints: %s", typehintsfile)
2290+
typehintsfiledir = os.path.dirname(typehintsfile)
2291+
if not os.path.isdir(typehintsfiledir):
2292+
os.makedirs(typehintsfiledir)
2293+
else:
2294+
with open(typehintsfile, "w", encoding="utf-8") as w:
2295+
w.write(done)
2296+
if done and not done.endswith("\n"):
2297+
w.write("\n")
2298+
logg.log(DONE, "written %s", typehintsfile)
2299+
22892300
return 0
22902301

22912302
def _beautify_dump(x: str) -> str:

0 commit comments

Comments
 (0)