diff --git a/mypy/stubgen.py b/mypy/stubgen.py index 1f7a01e6ae3c..ad48f0841b83 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -122,6 +122,7 @@ generate_guarded, infer_method_arg_types, infer_method_ret_type, + is_private_module, remove_misplaced_type_comments, report_missing, walk_packages, @@ -1618,6 +1619,9 @@ def generate_stub_for_py_module( If directory for target doesn't exist it will created. Existing stub will be overwritten. """ + if not include_private and is_private_module(mod.module): + return + if inspect: ngen = InspectionStubGenerator( module_name=mod.module, diff --git a/mypy/stubutil.py b/mypy/stubutil.py index 1a9c2357c58e..3e7cc00a0dda 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -805,3 +805,15 @@ def should_reexport(self, name: str, full_module: str, name_is_alias: bool) -> b if self._all_: return name in self._all_ return True + + +def is_private_module(module: str) -> bool: + """Check if a module is private. + + Return True if at least one module name starts with '_' and does not + end with '__'. + + For example, returns True if 'foo._bar.utils' is passed. + Returns False if 'foo.bar.__init__' is passed. + """ + return any(name.startswith("_") and not name.endswith("__") for name in module.split("."))