Skip to content

wip: flag to evaluate pypa dep specs #2832

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

rickeylev
Copy link
Collaborator

@rickeylev rickeylev commented Apr 27, 2025

wip/prototype to help bootstrap the impl of an analysis-time flag that evaluates
the pep508 dep specs

Creating a PR to make collab easier (maintainers can directly edit)

TODO:

  • Add tests
  • Rename file/flag ("pypa_dependency_specifier_flag" is pretty verbose)
  • probably more stuff

Work towards #2826

@rickeylev rickeylev requested review from aignas and groodt as code owners April 27, 2025 02:57
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.

Shall I push some of my thoughts as code or leave this at a comment level in the PR?

def _impl(ctx):
env = {}

runtime = ctx.toolchains[TARGET_TOOLCHAIN_TYPE].py3_runtime
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was wondering if we should get it from the toolchain info or the //python/config_settings:python_version flag. Probably better from toolchain as it will be more representative, but then we need to fetch the toolchain.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fetching the toolchain should be NBD. If we're evaluating which pypi dependency to choose, then it's almost certainly because there's some downstream py_binary or py_library asking, which lookup the toolchain already. (aside: having a separate toolchain type of just the interpreter "metadata" might be handy, though).

The part using the toolchain info will fall short with is the runtime_env toolchain -- that has a bunch of fields set to None, so we need fallback code paths.


env["os_name"] = struct()
env["platform_machine"] = struct()
env["platform_python_implementation"] = struct()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be CPython if cpython is implementation name. We could probably have that as a flag/or attach it to the runtime.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I dug through peps a bit. From what i can tell, sys.implementation.name was created to replace platform.python_implementation. It turns out the platform uses regex parsing of the sys.version string to determine its values 😲 .

The docs say there are 4 possible values, 2 of which are essentially defunct (IronPython, Jython). The other two are CPython and PyPy.

I just added an if condition to transform cpython to CPython. If we know the sys.implementation.name value that pypy uses, we could special case that, too.

# likely to be true.
env["implementation_name"] = runtime.implementation_name or "cpython"

env["os_name"] = struct()
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can have a select statement in that sets a string based on @platform//os: or the user can override it. Have the default based on some logic could help.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

(aside: this value is kinda weird, see #2826 (comment) )

env["platform_system"] = struct()

# todo: add flag to carry this
env["platform_version"] = struct()
Copy link
Collaborator

Choose a reason for hiding this comment

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

A note to myself. Right now pypi/config_settings is generating something similar for osx_version and maybe this flag could be used instead of creating a new separate flag for the osx_version.

@rickeylev
Copy link
Collaborator Author

Shall I push some of my thoughts as code or leave this at a comment level in the PR?

Yes, go ahead and push changes.

Copy link
Collaborator Author

@rickeylev rickeylev left a comment

Choose a reason for hiding this comment

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

mapped in the various things. added 2 flags.

def _impl(ctx):
env = {}

runtime = ctx.toolchains[TARGET_TOOLCHAIN_TYPE].py3_runtime
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fetching the toolchain should be NBD. If we're evaluating which pypi dependency to choose, then it's almost certainly because there's some downstream py_binary or py_library asking, which lookup the toolchain already. (aside: having a separate toolchain type of just the interpreter "metadata" might be handy, though).

The part using the toolchain info will fall short with is the runtime_env toolchain -- that has a bunch of fields set to None, so we need fallback code paths.

# likely to be true.
env["implementation_name"] = runtime.implementation_name or "cpython"

env["os_name"] = struct()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

(aside: this value is kinda weird, see #2826 (comment) )


env["os_name"] = struct()
env["platform_machine"] = struct()
env["platform_python_implementation"] = struct()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I dug through peps a bit. From what i can tell, sys.implementation.name was created to replace platform.python_implementation. It turns out the platform uses regex parsing of the sys.version string to determine its values 😲 .

The docs say there are 4 possible values, 2 of which are essentially defunct (IronPython, Jython). The other two are CPython and PyPy.

I just added an if condition to transform cpython to CPython. If we know the sys.implementation.name value that pypy uses, we could special case that, too.

@rickeylev
Copy link
Collaborator Author

As I've poked around the various pep508.bzl files, I see there's a lot of little utilities, tests, edge cases, etc. I'm just plowing ahead and copy/pasting things. Feel free to point out specific places where something should be wired into something else

"expression": attt.string(),
"os_name": attr.string(),
"sys_platform": attr.string(),
"platform_machine": attr.string(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Ideally I would just like to use the value of the currently active @platforms//cpu value. We can use the aliases feature to connect different values together.

That would allow us to not need modify rules_python just to add a new supported arch, which would be a nice thing for the Tier2 platforms out there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, that'd be nice. Unfortunately, I don't see a direct way to get that flag from Starlark. The closet I could find was CcToolchain.cpu. I'll ask around just in case I missed something. At the least, we can simply map all the platform:cpu values to what we know of today

@rickeylev
Copy link
Collaborator Author

Ok, basic flag with tests is working.

Based on the convo in the other issue, it's pretty clear I didn't understand the jargon and renaming things is in order.

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.

2 participants