Skip to content

Commit ce977e1

Browse files
authored
Fail if Python 2 values are specified (#887)
Fail for py2 python_version, srcs_version, and runtime values. See #886
1 parent c572cdc commit ce977e1

File tree

3 files changed

+148
-71
lines changed

3 files changed

+148
-71
lines changed

docs/python.md

Lines changed: 63 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/defs.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ Core rules for building Python projects.
1717
"""
1818

1919
load("@bazel_tools//tools/python:srcs_version.bzl", _find_requirements = "find_requirements")
20-
load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
2120
load(
2221
"//python/private:reexports.bzl",
2322
"internal_PyInfo",
2423
"internal_PyRuntimeInfo",
2524
_py_binary = "py_binary",
2625
_py_library = "py_library",
2726
_py_runtime = "py_runtime",
27+
_py_runtime_pair = "py_runtime_pair",
2828
_py_test = "py_test",
2929
)
3030

python/private/reexports.bzl

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ different name. Then we can load it from defs.bzl and export it there under
3737
the original name.
3838
"""
3939

40+
load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
41+
4042
# The implementation of the macros and tagging mechanism follows the example
4143
# set by rules_cc and rules_java.
4244

@@ -64,6 +66,8 @@ def py_library(**attrs):
6466
Args:
6567
**attrs: Rule attributes
6668
"""
69+
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
70+
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
6771

6872
# buildifier: disable=native-python
6973
native.py_library(**_add_tags(attrs))
@@ -74,6 +78,10 @@ def py_binary(**attrs):
7478
Args:
7579
**attrs: Rule attributes
7680
"""
81+
if attrs.get("python_version") == "PY2":
82+
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
83+
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
84+
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
7785

7886
# buildifier: disable=native-python
7987
native.py_binary(**_add_tags(attrs))
@@ -84,6 +92,10 @@ def py_test(**attrs):
8492
Args:
8593
**attrs: Rule attributes
8694
"""
95+
if attrs.get("python_version") == "PY2":
96+
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
97+
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
98+
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
8799

88100
# buildifier: disable=native-python
89101
native.py_test(**_add_tags(attrs))
@@ -94,6 +106,78 @@ def py_runtime(**attrs):
94106
Args:
95107
**attrs: Rule attributes
96108
"""
109+
if attrs.get("python_version") == "PY2":
110+
fail("Python 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
97111

98112
# buildifier: disable=native-python
99113
native.py_runtime(**_add_tags(attrs))
114+
115+
# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
116+
# doc generator gives useful API docs.
117+
def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs):
118+
"""A toolchain rule for Python.
119+
120+
This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
121+
However, Python 2 is no longer supported, so it now only wraps a single Python 3
122+
runtime.
123+
124+
Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
125+
rule returning a `PyRuntimeInfo` provider may be used.
126+
127+
This rule returns a `platform_common.ToolchainInfo` provider with the following
128+
schema:
129+
130+
```python
131+
platform_common.ToolchainInfo(
132+
py2_runtime = None,
133+
py3_runtime = <PyRuntimeInfo or None>,
134+
)
135+
```
136+
137+
Example usage:
138+
139+
```python
140+
# In your BUILD file...
141+
142+
load("@rules_python//python:defs.bzl", "py_runtime_pair")
143+
144+
py_runtime(
145+
name = "my_py3_runtime",
146+
interpreter_path = "/system/python3",
147+
python_version = "PY3",
148+
)
149+
150+
py_runtime_pair(
151+
name = "my_py_runtime_pair",
152+
py3_runtime = ":my_py3_runtime",
153+
)
154+
155+
toolchain(
156+
name = "my_toolchain",
157+
target_compatible_with = <...>,
158+
toolchain = ":my_py_runtime_pair",
159+
toolchain_type = "@rules_python//python:toolchain_type",
160+
)
161+
```
162+
163+
```python
164+
# In your WORKSPACE...
165+
166+
register_toolchains("//my_pkg:my_toolchain")
167+
```
168+
169+
Args:
170+
name: str, the name of the target
171+
py2_runtime: optional Label; must be unset or None; an error is raised
172+
otherwise.
173+
py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
174+
**attrs: Extra attrs passed onto the native rule
175+
"""
176+
if attrs.get("py2_runtime"):
177+
fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
178+
_py_runtime_pair(
179+
name = name,
180+
py2_runtime = py2_runtime,
181+
py3_runtime = py3_runtime,
182+
**attrs
183+
)

0 commit comments

Comments
 (0)