Move logic for generating stubs into dedicated module

This commit is contained in:
Pradyun Gedam 2019-11-10 16:53:30 +05:30
parent d8d948de64
commit 25d98529dd
No known key found for this signature in database
GPG Key ID: DA17C4B29CB32E4B
2 changed files with 41 additions and 27 deletions

View File

@ -10,6 +10,8 @@ import tarfile
import zipfile
from pathlib import Path
from .typing import generate_stubs
import invoke
import requests
@ -268,33 +270,7 @@ def update_stubs(ctx):
vendored_libs = detect_vendored_libs(vendor_dir)
print("[vendoring.update_stubs] Add mypy stubs")
extra_stubs_needed = {
# Some projects need stubs other than a simple <name>.pyi
"six": [
"six.__init__",
"six.moves.__init__",
"six.moves.configparser",
],
# Some projects should not have stubs coz they're single file modules
"appdirs": [],
"contextlib2": [],
}
for lib in vendored_libs:
if lib not in extra_stubs_needed:
(vendor_dir / (lib + ".pyi")).write_text("from %s import *" % lib)
continue
for selector in extra_stubs_needed[lib]:
fname = selector.replace(".", os.sep) + ".pyi"
if selector.endswith(".__init__"):
selector = selector[:-9]
f_path = vendor_dir / fname
if not f_path.parent.exists():
f_path.parent.mkdir()
f_path.write_text("from %s import *" % selector)
generate_stubs(vendor_dir, vendored_libs)
@invoke.task(name="update", post=[update_stubs])

View File

@ -0,0 +1,38 @@
"""Logic for adding static typing related stubs of vendored dependencies.
We autogenerate `.pyi` stub files for the vendored modules, when vendoring.
These .pyi files are not distributed (thanks to MANIFEST.in). The stub files
are merely `from ... import *` but they do what they're supposed to and mypy
is able to find the correct declarations using these files.
"""
import os
extra_stubs_needed = {
# Some projects need stubs other than a simple <name>.pyi
"six": [
"six.__init__",
"six.moves.__init__",
"six.moves.configparser",
],
# Some projects should not have stubs coz they're single file modules
"appdirs": [],
"contextlib2": [],
}
def generate_stubs(vendor_dir, vendored_libs):
for lib in vendored_libs:
if lib not in extra_stubs_needed:
(vendor_dir / (lib + ".pyi")).write_text("from %s import *" % lib)
continue
for selector in extra_stubs_needed[lib]:
fname = selector.replace(".", os.sep) + ".pyi"
if selector.endswith(".__init__"):
selector = selector[:-9]
f_path = vendor_dir / fname
if not f_path.parent.exists():
f_path.parent.mkdir()
f_path.write_text("from %s import *" % selector)