Skip to content
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

feat(gazelle): Include types/stubs packages #2425

Merged
merged 9 commits into from
Dec 8, 2024

Conversation

ewianda
Copy link
Contributor

@ewianda ewianda commented Nov 18, 2024

This PR adds logic that checks if a package has a corresponding types or stubs package and automatically adds that to the BUILD file. This is useful for typeckers e.g pyright , mypy

@ewianda ewianda requested a review from f0rmiga as a code owner November 18, 2024 15:58
@aignas
Copy link
Collaborator

aignas commented Nov 18, 2024

Thank you @ewianda for the contribution. @dougthor42, would you have time to look at this?

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.

I'll probably be able to test this out sometime this week.

Until then, here are some review comments. Also, please add a flag to turn this on or off. It should be off by default for now, and then in a couple releases we can consider making it on by default.

gazelle/python/resolve.go Outdated Show resolved Hide resolved
gazelle/python/resolve.go Outdated Show resolved Hide resolved
gazelle/modules_mapping/test_generator.py Outdated Show resolved Hide resolved
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.

Please add documentation to gazelle/README.md


Running with the default include_stub_packages = False didn't cause any problems with our large project running gazelle, so 👍 there.

Running with include_stub_packages = True didn't add any *_types or _stubs deps to targets 👎.

Is there something else I need to do? We already have some type/stub packages as part of our requirements.in (and thus requirements.txt and gazelle_python.yaml). For example, we have sqlmypy: sqlalchemy_stubs and annotated_types: annotated_types in gazelle_python.yaml

Test methodology

  1. bazel run //:buildozer 'remove deps' '//src/pyle_xc/...:*'
  2. bazel run //:gazelle src/pyle_xc
  3. git diff and see what has changed.

gazelle/modules_mapping/generator.py Outdated Show resolved Hide resolved
gazelle/modules_mapping/generator.py Show resolved Hide resolved
gazelle/modules_mapping/generator.py Outdated Show resolved Hide resolved
gazelle/python/resolve.go Outdated Show resolved Hide resolved
@ewianda
Copy link
Contributor Author

ewianda commented Nov 23, 2024

Please add documentation to gazelle/README.md

Running with the default include_stub_packages = False didn't cause any problems with our large project running gazelle, so 👍 there.

Running with include_stub_packages = True didn't add any *_types or _stubs deps to targets 👎.

Is there something else I need to do? We already have some type/stub packages as part of our requirements.in (and thus requirements.txt and gazelle_python.yaml). For example, we have sqlmypy: sqlalchemy_stubs and annotated_types: annotated_types in gazelle_python.yaml

Test methodology

  1. bazel run //:buildozer 'remove deps' '//src/pyle_xc/...:*'
  2. bazel run //:gazelle src/pyle_xc
  3. git diff and see what has changed.

Thanks, @dougthor42, for testing. I think you missed bazel run //:gazelle_python_manifest.update to add the types to the manifest.

Did you fix 631ab19
locally before running the test?

Also, the logic is that sqlalchemy has to be a direct dependency before sqlalchemy-stubs will be added to the BUILD file. annotated-types doesn't fall under this category as well.

@ewianda ewianda force-pushed the handle-types-and-stubs branch from 692ff24 to dfddffa Compare November 25, 2024 04:55
@dougthor42
Copy link
Collaborator

Did you fix 631ab19 locally before running the test?

Ah, no I did not. I applied that change and ran gazelle_python_manifest.update. Here's the full diff of gazelle_python.yaml:

diff --git a/gazelle_python.yaml b/gazelle_python.yaml
index 5752a8274a..66e8ab5c4f 100644
--- a/gazelle_python.yaml
+++ b/gazelle_python.yaml
@@ -395,6 +395,7 @@ manifest:
     pymatching: PyMatching
     pymodbus: pymodbus
     pyparsing: pyparsing
+    pyqt5_stubs: pyqt5_stubs
     pytest: pytest
     pytest_asyncio: pytest_asyncio
     pytest_benchmark: pytest_benchmark
@@ -484,9 +485,8 @@ manifest:
     sphinxcontrib.svgbob: sphinxcontrib_svgbob
     sqlalchemy: SQLAlchemy
     sqlalchemy_bigquery: sqlalchemy_bigquery
-    sqlmypy: sqlalchemy_stubs
+    sqlalchemy_stubs: sqlalchemy_stubs
     sqlparse: sqlparse
-    sqltyping: sqlalchemy_stubs
     stack_data: stack_data
     stim: stim
     stimcirq: stimcirq

So it's is correctly pulling in new stubs (like pyqt5_stubs) 👍 but also changing names a bit 👎. This 2nd part is an issue if user code ends up trying to import the original name (eg sqlmypy) directly: gazelle will fail saying "sqlmypy" is an invalid dependency.

Luckily our code doesn't import sqlmypy or sqltyping directly, so I didn't run into that error and was able to move on and run gazelle. Here's a partial diff:

diff --git a/src/labrad/servers/GUIs/ADR/BUILD.bazel b/src/labrad/servers/GUIs/ADR/BUILD.bazel
index 0667587998..b8e716cc30 100644
--- a/src/labrad/servers/GUIs/ADR/BUILD.bazel
+++ b/src/labrad/servers/GUIs/ADR/BUILD.bazel
@@ -11,5 +11,6 @@ py_library(
         "//src/pyle/datavault:util",
         "@pypi//duet",
         "@pypi//pyqt5",
+        "@pypi//pyqt5_stubs",
     ],
 )
diff --git a/src/pyle/cloud/bigquery/scripts/BUILD.bazel b/src/pyle/cloud/bigquery/scripts/BUILD.bazel
index 0d10c6b62d..f86c846852 100644
--- a/src/pyle/cloud/bigquery/scripts/BUILD.bazel
+++ b/src/pyle/cloud/bigquery/scripts/BUILD.bazel
@@ -86,6 +86,7 @@ pyle_py_binary(
         "@pypi//google_cloud_bigquery",
         "@pypi//pandas",
         "@pypi//sqlalchemy",
+        "@pypi//sqlalchemy_stubs",
     ],
 )

So overall it looks like it's WAI 👍 🎉 🎊

@ewianda ewianda force-pushed the handle-types-and-stubs branch from dfddffa to 556857a Compare November 25, 2024 05:23
@ewianda
Copy link
Contributor Author

ewianda commented Nov 25, 2024

This 2nd part is an issue if user code ends up trying to import the original name (eg sqlmypy) directly: gazelle will fail saying "sqlmypy" is an invalid dependency.

These are stub files, I will be surprised if folks are importing this directly since they are meant for type-checking tools, how ever we can let dig_wheel proceed to add package names.

@ewianda ewianda force-pushed the handle-types-and-stubs branch from 556857a to c9f367e Compare November 25, 2024 05:47
@ewianda
Copy link
Contributor Author

ewianda commented Dec 6, 2024

ping @dougthor42 is there something else you will like to change for this to go in

@aignas
Copy link
Collaborator

aignas commented Dec 7, 2024

I have merged main here since it has various CI fixes.

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 for the ping. There are still two open items:

  1. Please add documentation to gazelle/README.md. A comment and example here
    gazelle_python_manifest(
    name = "gazelle_python_manifest",
    modules_mapping = ":modules_map",
    # This is what we called our `pip_parse` rule, where third-party
    # python libraries are loaded in BUILD files.
    pip_repository_name = "pip",
    # This should point to wherever we declare our python dependencies
    # (the same as what we passed to the modules_mapping rule in WORKSPACE)
    # This argument is optional. If provided, the `.test` target is very
    # fast because it just has to check an integrity field. If not provided,
    # the integrity field is not added to the manifest which can help avoid
    # merge conflicts in large repos.
    requirements = "//:requirements_lock.txt",
    )
    is probably sufficient.
  2. Please add a short blurb to CHANGELOG.md describing the new feature.

Otherwise this LGTM.

gazelle/WORKSPACE Show resolved Hide resolved
@@ -50,6 +52,11 @@ modules_mapping = rule(
doc = "A set of regex patterns to match against each calculated module path. By default, exclude the modules starting with underscores.",
mandatory = False,
),
"include_stub_packages": attr.bool(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Other new features get labeled as "experimental" for a couple releases (eg: experimental_requirement_cycles). Do we want to do the same here as experimental_include_stub_packages?

Given that it's opt-in and that no other gazelle features are experimental, I'm inclined to leave it as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I vote to leave it as is

@ewianda ewianda requested a review from groodt as a code owner December 7, 2024 20:09
@ewianda ewianda force-pushed the handle-types-and-stubs branch from c581b5d to d1f8b9e Compare December 7, 2024 20:09
CHANGELOG.md Outdated Show resolved Hide resolved
@ewianda ewianda force-pushed the handle-types-and-stubs branch from 4e2c75b to 8a90f5b Compare December 8, 2024 02:05
CHANGELOG.md Show resolved Hide resolved
Copy link
Collaborator

@aignas aignas left a comment

Choose a reason for hiding this comment

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

Thank you @dougthor42 for the review and thank you @ewianda for the contribution. Stamping and merging.

gazelle/README.md Outdated Show resolved Hide resolved

# This wheel is purely here to validate the wheel extraction code. It's not
# intended for anything else.
internal_dev_deps()
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: FYI, this could return a list of repos that are created internally via the bazel skylib's extension metadata. Then bazel mod tidy would ensure that the use_repo statement in the MODULE.bazel is always up to date.

@aignas aignas enabled auto-merge December 8, 2024 03:08
@aignas aignas added this pull request to the merge queue Dec 8, 2024
Merged via the queue into bazelbuild:main with commit 6b0e40a Dec 8, 2024
3 of 4 checks passed
@ewianda ewianda deleted the handle-types-and-stubs branch December 8, 2024 05:19
@aignas aignas mentioned this pull request Dec 27, 2024
4 tasks
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.

4 participants