Skip to content

chore(stubgen): __all__ correctness#2813

Open
Rayahhhmed wants to merge 3 commits intofacebook:mainfrom
Rayahhhmed:rayahhhmed-support-all-stubgen-filtering
Open

chore(stubgen): __all__ correctness#2813
Rayahhhmed wants to merge 3 commits intofacebook:mainfrom
Rayahhhmed:rayahhhmed-support-all-stubgen-filtering

Conversation

@Rayahhhmed
Copy link

@Rayahhhmed Rayahhhmed commented Mar 17, 2026

Context

When a Python module defines __all__, it explicitly declares its public API. Before this change, pyrefly stubgen ignored __all__ entirely and relied solely on naming conventions (skip _ prefixed names which is correct).

However, this meant that internal helpers not in __all__ leaked into the generated stub, and private names explicitly listed in __all__ could be incorrectly filtered out.

For example, given:

__all__ = ["public_func", "PublicClass"]
def public_func(x: int) -> str: ...
def unlisted_func() -> bool: ...  # internal, not in __all__
def _explicitly_exported() -> None: ...  # private by convention but in __all__

Before: unlisted_func appeared in the stub (wrong), _explicitly_exported was excluded (wrong).

After: Only public_func and PublicClass appear and this is exactly what __all__ specifies.

Approach

  • Import statements are always preserved regardless of __all__, since they may be needed for type resolution in the stub.
  • Type aliases (TypeVar calls, old-style aliases, PEP 695 type statements) are now also filtered through should_include_name, which they previously bypassed.
  • Threads the result through ExtractionContext into should_include_name(), which short-circuits at module level: if __all__ is present, only listed names pass. Inside classes, __all__ is irrelevant and the existing convention logic applies unchanged.
  • Falls back gracefully to convention-based filtering when __all__ is absent, unresolvable, or contains cross-module re-exports.

Test plan

  • New snapshot test: dunder_all
  • All 11 existing stubgen snapshot tests pass and no regressions for modules without
    __all__
  • python3 test.py --no-test --no-conformance passes

@meta-cla meta-cla bot added the cla signed label Mar 17, 2026
@Rayahhhmed Rayahhhmed marked this pull request as ready for review March 17, 2026 01:15
@yangdanny97 yangdanny97 self-assigned this Mar 17, 2026
@meta-codesync
Copy link

meta-codesync bot commented Mar 17, 2026

@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D96861208.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Rayahhhmed Rayahhhmed force-pushed the rayahhhmed-support-all-stubgen-filtering branch 2 times, most recently from 7676cf8 to 044f43a Compare March 17, 2026 04:20
@yangdanny97
Copy link
Contributor

@migeed-z why is mypy primer showing a huge delta here? this PR should be a no-op

@Rayahhhmed Rayahhhmed force-pushed the rayahhhmed-support-all-stubgen-filtering branch 2 times, most recently from 01cad74 to 9b97de8 Compare March 17, 2026 21:29
@facebook facebook deleted a comment from github-actions bot Mar 18, 2026
@facebook facebook deleted a comment from github-actions bot Mar 18, 2026
@migeed-z
Copy link
Contributor

@migeed-z why is mypy primer showing a huge delta here? this PR should be a no-op

The fix landed. It's looking normal again on new PRs.

@Rayahhhmed Rayahhhmed force-pushed the rayahhhmed-support-all-stubgen-filtering branch from 9b97de8 to 376b031 Compare March 18, 2026 03:45
@Rayahhhmed
Copy link
Author

Rayahhhmed commented Mar 18, 2026

Rebased on main again! Thanks @migeed-z :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants