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): Add "python_visibility" directive that appends additional visibility labels #1784

Merged
merged 10 commits into from
Mar 1, 2024

Conversation

dougthor42
Copy link
Contributor

@dougthor42 dougthor42 commented Feb 27, 2024

Fixes #1783.

Add a new gazelle directive, python_visibility, that allows
users to add labels to the visibility attribute of generated targets.
This directive acts similar to1 the go_visibility
directive
.

The primary use case is for python projects that separate unit test
files from the python packages/modules that they test, like so:

packaging_tutorial/
├── LICENSE
├── pyproject.toml
├── README.md
├── src/
│   └── mypackage/
│       ├── __init__.py
│       └── foo.py
└── tests/
    ├── __init__.py
    └── test_foo.py

A future PR will add an example to the ./examples directory (issue
#1775).

Footnotes

  1. At least, similar based on docs. I haven't done any actual
    comparison.

gazelle/python/generate.go Outdated Show resolved Hide resolved
Comment on lines +104 to +106
for _, item := range visibility {
t.visibility.Add(item)
}
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 thought that I'd be able to do

t.visibility.Add(visibility...)

instead, but I would get this error:

python/target.go:104:19: cannot use visibility (variable of type []string) as type []interface{} in argument to t.visibility.Add

I'm relatively new to go and haven't use the gods.TreeSet package before, so I'm not sure why things don't work.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can follow the pattern from above. See addModuleDependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm... looks like doing so would mean having to adjust the arg type from []string to *treeset.Set, which would cascade to generate.go.

As-is, the diff is smaller and I'm always a fan of using built-in types when possible.

That said, LMK if you feel strongly that this func should be *treeset.Set and I'll be happy to change it.

@dougthor42 dougthor42 marked this pull request as ready for review February 27, 2024 22:22
Copy link
Collaborator

@f0rmiga f0rmiga 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 adding tests.

@@ -198,6 +198,54 @@ Python-specific directives are as follows:
| Controls the `py_test` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | |
| `# gazelle:resolve py ...` | n/a |
| Instructs the plugin what target to add as a dependency to satisfy a given import statement. The syntax is `# gazelle:resolve py import-string label` where `import-string` is the symbol in the python `import` statement, and `label` is the Bazel label that Gazelle should write in `deps`. | |
| [`# gazelle:python_visibility label`](#directive-python_visibility) | |
| Append additional visibility labels to each generated target. This directive can be set multiple times. | |
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
| Append additional visibility labels to each generated target. This directive can be set multiple times. | |
| Appends additional visibility labels to each generated target. This directive can be set multiple times. | |

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


#### Directive: `python_visibility`:

Append additional `visibility` labels to each generated target.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Append additional `visibility` labels to each generated target.
Appends additional `visibility` labels to each generated target.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// ExtraVisibility represents the directive that controls what additional
// visibility labels are added to generated targets. It mimics the behavior
// of the `go_visibility` directive.
ExtraVisibility = "python_visibility"
Copy link
Collaborator

Choose a reason for hiding this comment

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

To follow the pattern:

Suggested change
ExtraVisibility = "python_visibility"
Visibility = "python_visibility"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SGTM, updated..

Comment on lines +104 to +106
for _, item := range visibility {
t.visibility.Add(item)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can follow the pattern from above. See addModuleDependencies.

Copy link
Contributor Author

@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 adding tests.

Of course! I wouldn't have it any other way.

Addressed review comments, only one open question if you want me to change to addVisibility(visibility *treeset.Set)

@@ -198,6 +198,54 @@ Python-specific directives are as follows:
| Controls the `py_test` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | |
| `# gazelle:resolve py ...` | n/a |
| Instructs the plugin what target to add as a dependency to satisfy a given import statement. The syntax is `# gazelle:resolve py import-string label` where `import-string` is the symbol in the python `import` statement, and `label` is the Bazel label that Gazelle should write in `deps`. | |
| [`# gazelle:python_visibility label`](#directive-python_visibility) | |
| Append additional visibility labels to each generated target. This directive can be set multiple times. | |
Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


#### Directive: `python_visibility`:

Append additional `visibility` labels to each generated target.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines +104 to +106
for _, item := range visibility {
t.visibility.Add(item)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm... looks like doing so would mean having to adjust the arg type from []string to *treeset.Set, which would cascade to generate.go.

As-is, the diff is smaller and I'm always a fan of using built-in types when possible.

That said, LMK if you feel strongly that this func should be *treeset.Set and I'll be happy to change it.

// ExtraVisibility represents the directive that controls what additional
// visibility labels are added to generated targets. It mimics the behavior
// of the `go_visibility` directive.
ExtraVisibility = "python_visibility"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

SGTM, updated..

@aignas
Copy link
Collaborator

aignas commented Feb 28, 2024

Note, that the PR will get squashed and the commit message on the main branch will be the description of the PR. So consider improving the wording/layout there.

That said, I like PRs being submitted as a way to spark the discussion, it's not rude at all. It's sometimes really nice to discuss something concrete. So thank you for doing just that!

@dougthor42
Copy link
Contributor Author

PR message updated - removed the note and kept lines down to ~72 chars.

I like PRs being submitted as a way to spark the discussion, it's not rude at all. It's sometimes really nice to discuss something concrete. So thank you for doing just that!

You're welcome! Some people like it, others hate it haha. Personally I also find it nice to discuss something concrete.

Copy link
Collaborator

@f0rmiga f0rmiga left a comment

Choose a reason for hiding this comment

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

Looks good, just address my last comments and ping me to merge. Thanks!

@@ -212,7 +212,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}

parser := newPython3Parser(args.Config.RepoRoot, args.Rel, cfg.IgnoresDependency)
visibility := fmt.Sprintf("//%s:__subpackages__", pythonProjectRoot)
// TODO(gh-1682): Add support for default_visibility directive and replace
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please, remove this TODO item since there's an issue already.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@@ -136,6 +140,7 @@ type Config struct {
libraryNamingConvention string
binaryNamingConvention string
testNamingConvention string
extraVisibility []string
Copy link
Collaborator

Choose a reason for hiding this comment

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

I hoped you'd remove the extra everywhere.

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 could have sworn I did... Sorry about that, I definitely meant to.

...
...

Ah, looks like I stashed the changes instead of committing them. /facepalm. Anyway, updated 👍.

@dougthor42 dougthor42 requested a review from f0rmiga March 1, 2024 04:28
@f0rmiga f0rmiga added this pull request to the merge queue Mar 1, 2024
Merged via the queue into bazelbuild:main with commit da10ac4 Mar 1, 2024
4 checks passed
@dougthor42 dougthor42 deleted the gazelle-python-visibility branch March 1, 2024 21:03
@aignas
Copy link
Collaborator

aignas commented Mar 13, 2024

I see that the CHANGELOG.md did not get updated when merging this PR. Would one of you be willing to create a followup PR to add the a short snippet about the new features? Something like:

Added:
* (gazelle) Added a new `python_visibility` directive to control visibility of
  generated targets to allow sharing of targets between trees with different
  `python_root` values.

I am not sure if I worded it well enough, so feel free to improve upon in!

@dougthor42
Copy link
Contributor Author

dougthor42 commented Mar 13, 2024 via email

dougthor42 added a commit to dougthor42/rules_python that referenced this pull request Mar 13, 2024
github-merge-queue bot pushed a commit that referenced this pull request Mar 13, 2024
Add the missing changelog from PR #1784.
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.

Add python_visibility gazelle directive
3 participants