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

refactor: use click in dependency_resolver.py #1071

Merged
merged 1 commit into from
Nov 1, 2023

Conversation

cj81499
Copy link
Contributor

@cj81499 cj81499 commented Feb 14, 2023

Using click makes it easier to parse arguments. Many args are now named arguments
(options), and the need for using positional args with stub "None" values isn't
necessary anymore.

There is already a dependency on click via piptools, so this doesn't introduce a new
dependency.

Relates to #1067

@google-cla
Copy link

google-cla bot commented Feb 14, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@cj81499 cj81499 marked this pull request as ready for review June 22, 2023 14:35
@cj81499
Copy link
Contributor Author

cj81499 commented Jun 26, 2023

@hrfuller Apologies for the delay. This is ready for review!

@cj81499
Copy link
Contributor Author

cj81499 commented Jul 6, 2023

@hrfuller are you available to take a look at this? If not, is there someone else who might be able to?

@hrfuller
Copy link
Contributor

hrfuller commented Jul 6, 2023

are you available to take a look at this? If not, is there someone else who might be able to?

Yes, I can take a look. @rickeylev is a more active maintainer than me these days so I'd like him to take a look as well.

Copy link
Contributor

@hrfuller hrfuller left a comment

Choose a reason for hiding this comment

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

Overall looks like an improvement to me. Thanks!

@@ -128,6 +131,8 @@ def _locate(bazel_runfiles, file):
os.environ["LC_ALL"] = "C.UTF-8"
os.environ["LANG"] = "C.UTF-8"

argv = []
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like we lose sys.argv[0] compared to before. I'm not sure if the piptools cli expects argv to have argv[0] be the name of the original script but maybe we can add sys.argv[0] here in case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to be clear, are you suggesting we change this to the following?

Suggested change
argv = []
argv = [sys.argv[0]]

Copy link
Contributor

Choose a reason for hiding this comment

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

From what I can tell, omitting argv[0] when calling cli() is correct.

Under the hood, piptools is using click, and the implementation is obscured by a ton of decorators. Digging into it...

  • compile.cli is an instance of the click Command class.
  • Command.__call__ is an alias for Command.main
  • Command.main docs say, for the first positionl arg (named args): "the arguments that should be used for parsing. If not provided sys.argv[1:] is used"

So it doesn't expect args to contain argv[0]. The docs go on further to say the optional prog_name arg is initialized to sys.argv[0] if not specified.

As a side note, there is a standalone_mode arg, which controls whether the command tries to exit or not.

requirements_in_relative
if Path(requirements_in_relative).exists()
else resolved_requirements_in
)
print(sys.argv)
Copy link
Contributor

@hrfuller hrfuller Jul 6, 2023

Choose a reason for hiding this comment

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

Unclear why this was here. Probably for debugging?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was my assumption too. Didn't seem like something that ought to stick around (adding clutter to stdout!).

Happy to add it back if someone has a good reason for it to stay there.

Copy link
Contributor Author

@cj81499 cj81499 left a comment

Choose a reason for hiding this comment

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

@rickeylev is a more active maintainer than me these days so I'd like him to take a look as well.

@hrfuller I take this to mean you'd like to hold off on merging until they review?

@@ -128,6 +131,8 @@ def _locate(bazel_runfiles, file):
os.environ["LC_ALL"] = "C.UTF-8"
os.environ["LANG"] = "C.UTF-8"

argv = []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to be clear, are you suggesting we change this to the following?

Suggested change
argv = []
argv = [sys.argv[0]]

requirements_in_relative
if Path(requirements_in_relative).exists()
else resolved_requirements_in
)
print(sys.argv)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was my assumption too. Didn't seem like something that ought to stick around (adding clutter to stdout!).

Happy to add it back if someone has a good reason for it to stay there.

@cj81499
Copy link
Contributor Author

cj81499 commented Jul 24, 2023

@hrfuller @rickeylev bump!

I would really like to get this (and the related #1067) merged.

@cj81499
Copy link
Contributor Author

cj81499 commented Aug 21, 2023

@hrfuller @rickeylev I'd still like to get this (and #1067) merged.
Please take a look at your earliest convenience.

@cj81499
Copy link
Contributor Author

cj81499 commented Sep 7, 2023

@f0rmiga I can't seem to get the attention of @hrfuller or @rickeylev and I'm not sure what else to try (other than contacting another codeowner). Perhaps you can help?

@cj81499 cj81499 force-pushed the use-click branch 2 times, most recently from 653b91f to 3b413d7 Compare October 31, 2023 18:15
avoid mutating `sys.argv`

Co-authored-by: Logan Pulley <lpulley@ocient.com>
@rickeylev rickeylev changed the title refactor dependency_resolver.py to use click refactor: use click in dependency_resolver.py Nov 1, 2023
@@ -128,6 +131,8 @@ def _locate(bazel_runfiles, file):
os.environ["LC_ALL"] = "C.UTF-8"
os.environ["LANG"] = "C.UTF-8"

argv = []
Copy link
Contributor

Choose a reason for hiding this comment

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

From what I can tell, omitting argv[0] when calling cli() is correct.

Under the hood, piptools is using click, and the implementation is obscured by a ton of decorators. Digging into it...

  • compile.cli is an instance of the click Command class.
  • Command.__call__ is an alias for Command.main
  • Command.main docs say, for the first positionl arg (named args): "the arguments that should be used for parsing. If not provided sys.argv[1:] is used"

So it doesn't expect args to contain argv[0]. The docs go on further to say the optional prog_name arg is initialized to sys.argv[0] if not specified.

As a side note, there is a standalone_mode arg, which controls whether the command tries to exit or not.

@rickeylev
Copy link
Contributor

Hi @cj81499,

Thanks for your PR. Sorry it took so long to be reviewed. I had let this languish because I thought it was introducing a new dependency on click, but we already have that dependency, so this isn't as bad as I thought.

@rickeylev rickeylev added this pull request to the merge queue Nov 1, 2023
@cj81499
Copy link
Contributor Author

cj81499 commented Nov 1, 2023

@rickeylev thank you for getting around to this! ❤️

fwiw, I did say the following in the PR description ;)

There is already a dependency on click via piptools, so this doesn't introduce a new dependency.

Excited that #1067 will be unblocked once this makes its way through the queue!

Merged via the queue into bazelbuild:main with commit 4e26bcd Nov 1, 2023
3 checks passed
@cj81499 cj81499 deleted the use-click branch November 1, 2023 21:39
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