From 0b990bc131a3145a54b7548ad7ae0eb2aa0d2b1e Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Fri, 18 Nov 2022 10:19:14 -0800 Subject: [PATCH 1/5] Fail for py2 python_version, srcs_version, and runtime values --- python/defs.bzl | 2 +- python/private/reexports.bzl | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/python/defs.bzl b/python/defs.bzl index 88f28c5fc0..7b60c6513b 100644 --- a/python/defs.bzl +++ b/python/defs.bzl @@ -17,7 +17,6 @@ Core rules for building Python projects. """ load("@bazel_tools//tools/python:srcs_version.bzl", _find_requirements = "find_requirements") -load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair") load( "//python/private:reexports.bzl", "internal_PyInfo", @@ -25,6 +24,7 @@ load( _py_binary = "py_binary", _py_library = "py_library", _py_runtime = "py_runtime", + _py_runtime_pair = "py_runtime_pair", _py_test = "py_test", ) diff --git a/python/private/reexports.bzl b/python/private/reexports.bzl index 6ad9e0cdcc..9e4020bec4 100644 --- a/python/private/reexports.bzl +++ b/python/private/reexports.bzl @@ -37,6 +37,8 @@ different name. Then we can load it from defs.bzl and export it there under the original name. """ +load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair") + # The implementation of the macros and tagging mechanism follows the example # set by rules_cc and rules_java. @@ -64,6 +66,8 @@ def py_library(**attrs): Args: **attrs: Rule attributes """ + if attrs.get("srcs_version") in ("PY2", "PY2ONLY"): + fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886") # buildifier: disable=native-python native.py_library(**_add_tags(attrs)) @@ -74,6 +78,10 @@ def py_binary(**attrs): Args: **attrs: Rule attributes """ + if attrs.get("python_version") == "PY2": + fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886") + if attrs.get("srcs_version") in ("PY2", "PY2ONLY"): + fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886") # buildifier: disable=native-python native.py_binary(**_add_tags(attrs)) @@ -84,6 +92,10 @@ def py_test(**attrs): Args: **attrs: Rule attributes """ + if attrs.get("python_version") == "PY2": + fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886") + if attrs.get("srcs_version") in ("PY2", "PY2ONLY"): + fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886") # buildifier: disable=native-python native.py_test(**_add_tags(attrs)) @@ -94,6 +106,13 @@ def py_runtime(**attrs): Args: **attrs: Rule attributes """ + if attrs.get("python_version") == "PY2": + fail("Python 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886") # buildifier: disable=native-python native.py_runtime(**_add_tags(attrs)) + +def py_runtime_pair(**attrs): + if attrs.get("py2_runtime"): + fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886") + _py_runtime_pair(**attrs) From 26469df855cc89de53ee26bef14a883f48eafda5 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Fri, 18 Nov 2022 10:39:23 -0800 Subject: [PATCH 2/5] Add and update docs for py_runtime_pair --- docs/python.md | 96 +++++++----------------------------- python/private/reexports.bzl | 62 ++++++++++++++++++++++- 2 files changed, 79 insertions(+), 79 deletions(-) diff --git a/docs/python.md b/docs/python.md index bd14b8258e..72c23aaf25 100755 --- a/docs/python.md +++ b/docs/python.md @@ -51,84 +51,6 @@ This rule allows the use of Python packages as dependencies. | srcs | The list of Python package files provided to Python targets that depend on this target. Note that currently only the .egg format is accepted. For .whl files, try the whl_library rule. We accept contributions to extend py_import to handle .whl. | List of labels | optional | [] | - - -## py_runtime_pair - -
-py_runtime_pair(name, py2_runtime, py3_runtime)
-
- -A toolchain rule for Python. - -This wraps up to two Python runtimes, one for Python 2 and one for Python 3. -The rule consuming this toolchain will choose which runtime is appropriate. -Either runtime may be omitted, in which case the resulting toolchain will be -unusable for building Python code using that version. - -Usually the wrapped runtimes are declared using the `py_runtime` rule, but any -rule returning a `PyRuntimeInfo` provider may be used. - -This rule returns a `platform_common.ToolchainInfo` provider with the following -schema: - -```python -platform_common.ToolchainInfo( - py2_runtime = , - py3_runtime = , -) -``` - -Example usage: - -```python -# In your BUILD file... - -load("@rules_python//python:defs.bzl", "py_runtime_pair") - -py_runtime( - name = "my_py2_runtime", - interpreter_path = "/system/python2", - python_version = "PY2", -) - -py_runtime( - name = "my_py3_runtime", - interpreter_path = "/system/python3", - python_version = "PY3", -) - -py_runtime_pair( - name = "my_py_runtime_pair", - py2_runtime = ":my_py2_runtime", - py3_runtime = ":my_py3_runtime", -) - -toolchain( - name = "my_toolchain", - target_compatible_with = <...>, - toolchain = ":my_py_runtime_pair", - toolchain_type = "@rules_python//python:toolchain_type", -) -``` - -```python -# In your WORKSPACE... - -register_toolchains("//my_pkg:my_toolchain") -``` - - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :-------------: | :-------------: | :-------------: | :-------------: | :-------------: | -| name | A unique name for this target. | Name | required | | -| py2_runtime | The runtime to use for Python 2 targets. Must have python_version set to PY2. | Label | optional | None | -| py3_runtime | The runtime to use for Python 3 targets. Must have python_version set to PY3. | Label | optional | None | - - ## py_binary @@ -183,6 +105,24 @@ See the Bazel core [py_runtime](https://docs.bazel.build/versions/master/be/pyth | attrs | Rule attributes | none | + + +## py_runtime_pair + +
+py_runtime_pair(attrs)
+
+ + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :-------------: | :-------------: | :-------------: | +| attrs |

-

| none | + + ## py_test diff --git a/python/private/reexports.bzl b/python/private/reexports.bzl index 9e4020bec4..dd098f3205 100644 --- a/python/private/reexports.bzl +++ b/python/private/reexports.bzl @@ -112,7 +112,67 @@ def py_runtime(**attrs): # buildifier: disable=native-python native.py_runtime(**_add_tags(attrs)) -def py_runtime_pair(**attrs): +# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our +# doc generator gives useful API docs. +def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs): + """A toolchain rule for Python. + +This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3. +However, Python 2 is no longer supported, so it now only wraps a single Python 3 +runtime. + +Usually the wrapped runtimes are declared using the `py_runtime` rule, but any +rule returning a `PyRuntimeInfo` provider may be used. + +This rule returns a `platform_common.ToolchainInfo` provider with the following +schema: + +```python +platform_common.ToolchainInfo( + py2_runtime = None, + py3_runtime = , +) +``` + +Example usage: + +```python +# In your BUILD file... + +load("@rules_python//python:defs.bzl", "py_runtime_pair") + +py_runtime( + name = "my_py3_runtime", + interpreter_path = "/system/python3", + python_version = "PY3", +) + +py_runtime_pair( + name = "my_py_runtime_pair", + py3_runtime = ":my_py3_runtime", +) + +toolchain( + name = "my_toolchain", + target_compatible_with = <...>, + toolchain = ":my_py_runtime_pair", + toolchain_type = "@rules_python//python:toolchain_type", +) +``` + +```python +# In your WORKSPACE... + +register_toolchains("//my_pkg:my_toolchain") +``` + + Args: + name: str, the name of the target + py2_runtime: optional Label; must be unset or None; an error is raised + otherwise. + py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3. + **attrs: Extra attrs passed onto the native rule + """ if attrs.get("py2_runtime"): fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886") _py_runtime_pair(**attrs) From 1e96b4b4a7fef05329f5fe9c02a7129db9690ec2 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Fri, 18 Nov 2022 13:51:11 -0800 Subject: [PATCH 3/5] Update Args doc formatting to satisfy CI --- python/private/reexports.bzl | 96 ++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/python/private/reexports.bzl b/python/private/reexports.bzl index dd098f3205..d687e9590f 100644 --- a/python/private/reexports.bzl +++ b/python/private/reexports.bzl @@ -117,54 +117,54 @@ def py_runtime(**attrs): def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs): """A toolchain rule for Python. -This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3. -However, Python 2 is no longer supported, so it now only wraps a single Python 3 -runtime. - -Usually the wrapped runtimes are declared using the `py_runtime` rule, but any -rule returning a `PyRuntimeInfo` provider may be used. - -This rule returns a `platform_common.ToolchainInfo` provider with the following -schema: - -```python -platform_common.ToolchainInfo( - py2_runtime = None, - py3_runtime = , -) -``` - -Example usage: - -```python -# In your BUILD file... - -load("@rules_python//python:defs.bzl", "py_runtime_pair") - -py_runtime( - name = "my_py3_runtime", - interpreter_path = "/system/python3", - python_version = "PY3", -) - -py_runtime_pair( - name = "my_py_runtime_pair", - py3_runtime = ":my_py3_runtime", -) - -toolchain( - name = "my_toolchain", - target_compatible_with = <...>, - toolchain = ":my_py_runtime_pair", - toolchain_type = "@rules_python//python:toolchain_type", -) -``` - -```python -# In your WORKSPACE... - -register_toolchains("//my_pkg:my_toolchain") -``` + This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3. + However, Python 2 is no longer supported, so it now only wraps a single Python 3 + runtime. + + Usually the wrapped runtimes are declared using the `py_runtime` rule, but any + rule returning a `PyRuntimeInfo` provider may be used. + + This rule returns a `platform_common.ToolchainInfo` provider with the following + schema: + + ```python + platform_common.ToolchainInfo( + py2_runtime = None, + py3_runtime = , + ) + ``` + + Example usage: + + ```python + # In your BUILD file... + + load("@rules_python//python:defs.bzl", "py_runtime_pair") + + py_runtime( + name = "my_py3_runtime", + interpreter_path = "/system/python3", + python_version = "PY3", + ) + + py_runtime_pair( + name = "my_py_runtime_pair", + py3_runtime = ":my_py3_runtime", + ) + + toolchain( + name = "my_toolchain", + target_compatible_with = <...>, + toolchain = ":my_py_runtime_pair", + toolchain_type = "@rules_python//python:toolchain_type", + ) + ``` + + ```python + # In your WORKSPACE... + + register_toolchains("//my_pkg:my_toolchain") + ``` Args: name: str, the name of the target From f0e954766e21f6279a6ed5b7a22f244cc2420800 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Fri, 18 Nov 2022 14:02:26 -0800 Subject: [PATCH 4/5] Update docs via //docs:update --- docs/python.md | 57 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/docs/python.md b/docs/python.md index 72c23aaf25..989d8c9764 100755 --- a/docs/python.md +++ b/docs/python.md @@ -110,9 +110,59 @@ See the Bazel core [py_runtime](https://docs.bazel.build/versions/master/be/pyth ## py_runtime_pair
-py_runtime_pair(attrs)
+py_runtime_pair(name, py2_runtime, py3_runtime, attrs)
 
+A toolchain rule for Python. + +This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3. +However, Python 2 is no longer supported, so it now only wraps a single Python 3 +runtime. + +Usually the wrapped runtimes are declared using the `py_runtime` rule, but any +rule returning a `PyRuntimeInfo` provider may be used. + +This rule returns a `platform_common.ToolchainInfo` provider with the following +schema: + +```python +platform_common.ToolchainInfo( + py2_runtime = None, + py3_runtime = , +) +``` + +Example usage: + +```python +# In your BUILD file... + +load("@rules_python//python:defs.bzl", "py_runtime_pair") + +py_runtime( + name = "my_py3_runtime", + interpreter_path = "/system/python3", + python_version = "PY3", +) + +py_runtime_pair( + name = "my_py_runtime_pair", + py3_runtime = ":my_py3_runtime", +) + +toolchain( + name = "my_toolchain", + target_compatible_with = <...>, + toolchain = ":my_py_runtime_pair", + toolchain_type = "@rules_python//python:toolchain_type", +) +``` + +```python +# In your WORKSPACE... + +register_toolchains("//my_pkg:my_toolchain") +``` **PARAMETERS** @@ -120,7 +170,10 @@ py_runtime_pair(attrs) | Name | Description | Default Value | | :-------------: | :-------------: | :-------------: | -| attrs |

-

| none | +| name | str, the name of the target | none | +| py2_runtime | optional Label; must be unset or None; an error is raised otherwise. | None | +| py3_runtime | Label; a target with PyRuntimeInfo for Python 3. | None | +| attrs | Extra attrs passed onto the native rule | none | From 60e47737b6c8137fae11bae63002d37a24ff53ac Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Fri, 18 Nov 2022 14:18:57 -0800 Subject: [PATCH 5/5] Pass through the py_runtime_pair args to the underlying rule call It's a bit concerning that no tests caught this, but that can be addressed separately. --- python/private/reexports.bzl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/private/reexports.bzl b/python/private/reexports.bzl index d687e9590f..987187c155 100644 --- a/python/private/reexports.bzl +++ b/python/private/reexports.bzl @@ -175,4 +175,9 @@ def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs): """ if attrs.get("py2_runtime"): fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886") - _py_runtime_pair(**attrs) + _py_runtime_pair( + name = name, + py2_runtime = py2_runtime, + py3_runtime = py3_runtime, + **attrs + )