Skip to content

Fix: --include-private issue (#16808) #16822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions mypy/stubutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("."))