Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(gazelle): Add type-checking only dependencies to pyi_deps #3014

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

Merged
merged 11 commits into from
Jun 26, 2025

Conversation

amartani
Copy link
Contributor

@amartani amartani commented Jun 20, 2025

#2538 added the attribute pyi_deps to python rules, intended to be used for dependencies that are only used for type-checking purposes. This PR adds a new directive, gazelle:python_generate_pyi_deps, which, when enabled:

  • When a dependency is added only to satisfy type-checking only imports (in a if TYPE_CHECKING: block), the dependency is added to pyi_deps instead of deps;
  • Third-party stub packages (eg. boto3-stubs) are now added to pyi_deps instead of deps.

@amartani amartani marked this pull request as ready for review June 20, 2025 03:28
Copy link
Collaborator

@dougthor42 dougthor42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This will be a really nice improvement.

Couple requests:

  1. Guard this behind a gazelle directive. Maybe # gazelle:python_generate_pyi_deps (default false for now).
    • Or python_generate_pyi_deps_attribute? python_use_pyi_deps_attribute?
  2. Add the directive to gazelle/README.md - the table of directives and a section for the directive.
  3. In the section for the directive, document that only two forms are supported: if typing.TYPE_CHECKING: and if TYPE_CHECKING:.
    For example, if someone does
    import typing as foo
    
    if foo.TYPE_CHECKING:
        ...
    or other odd stuff like that, then the imports won't be marked as pyi_deps.

@amartani amartani requested review from dougthor42 and aignas June 24, 2025 16:29
@dougthor42 dougthor42 added this pull request to the merge queue Jun 26, 2025
Merged via the queue into bazel-contrib:main with commit e5ef69b Jun 26, 2025
3 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Jun 29, 2025
Fix an issue in #3014
where a dependency may end up being added in both `deps` and `pyi_deps`,
in cases where the regular and the type-checking import refer to
different python modules on the same `py_library` target. Other cases
are already deduplicated earlier on, but this case can only be
deduplicated in the resolve phase.

(No new changelog entry since this is a fix to an unreleased feature
that is already in the changelog)
github-merge-queue bot pushed a commit that referenced this pull request Jun 30, 2025
Fix an issue in #3014
where, when all type-checking dependencies are removed from a file, the
corresponding target's `pyi_deps` doesn't get cleaned up. I traced this
back to `ResolveAttrs`, though I'm not entirely sure of what other
behaviors this may trigger. (Currently, removing `deps` from
`ResolveAttrs` doesn't break any existing test case)

(No new changelog entry since this is a fix to an unreleased feature
that is already in the changelog)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants