Skip to content

Commit df6f580

Browse files
committed
rework the symbol deprecation and add tests to ensure that we are still working
1 parent 6bf4281 commit df6f580

File tree

9 files changed

+226
-137
lines changed

9 files changed

+226
-137
lines changed

python/config_settings/transition.bzl

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,20 @@ of them should be changed to load the regular rules directly.
2121
:::
2222
"""
2323

24-
load("@rules_python_internal//:rules_python_config.bzl", "config")
2524
load("//python:py_binary.bzl", _py_binary = "py_binary")
2625
load("//python:py_test.bzl", _py_test = "py_test")
26+
load("//python/private:deprecation.bzl", "with_deprecation")
27+
load("//python/private:text_util.bzl", "render")
2728

28-
_DEPRECATION_MESSAGE = """
29-
The '{name}' symbol in @{deprecated}
30-
is deprecated. It is an alias to the regular rule; use it directly instead:
31-
32-
load("@rules_python//python{load_name}.bzl", "{name}")
33-
34-
{name}(
35-
# ...
36-
python_version = "{python_version}",
37-
# ...
38-
)
39-
"""
40-
41-
def with_deprecation(kwargs, *, symbol_name, python_version, load_name = None, deprecated = "rules_python//python/config_settings:transition.bzl"):
42-
"""An internal function to propagate the deprecation warning.
43-
44-
This is not an API that should be used outside `rules_python`.
45-
46-
Args:
47-
kwargs: Arguments to modify.
48-
symbol_name: {type}`str` the symbol name that is deprecated.
49-
python_version: {type}`str` the python version to be used.
50-
load_name: {type}`str` the load location under `//python`. Should start
51-
either with `/` or `:`. Defaults to `:<symbol_name>`.
52-
deprecated: {type}`str` the symbol import location that we are deprecating.
53-
54-
Returns:
55-
The kwargs to be used in the macro creation.
56-
"""
57-
58-
if config.enable_deprecation_warnings:
59-
load_name = load_name or (":" + symbol_name)
60-
61-
deprecation = _DEPRECATION_MESSAGE.format(
62-
name = symbol_name,
63-
load_name = load_name,
64-
python_version = python_version,
65-
deprecated = deprecated,
66-
)
67-
if kwargs.get("deprecation"):
68-
deprecation = kwargs.get("deprecation") + "\n\n" + deprecation
69-
kwargs["deprecation"] = deprecation
29+
def _with_deprecation(kwargs, *, name, python_version):
7030
kwargs["python_version"] = python_version
71-
return kwargs
31+
return with_deprecation.symbol(
32+
kwargs,
33+
symbol_name = name,
34+
old_load = "@rules_python//python/config_settings:transition.bzl",
35+
new_load = "@rules_python//python:{}.bzl".format(name),
36+
snippet = render.call(name, **{k: repr(v) for k, v in kwargs.items()}),
37+
)
7238

7339
def py_binary(**kwargs):
7440
"""[DEPRECATED] Deprecated alias for py_binary.
@@ -77,12 +43,12 @@ def py_binary(**kwargs):
7743
**kwargs: keyword args forwarded onto {obj}`py_binary`.
7844
"""
7945

80-
_py_binary(**with_deprecation(kwargs, name = "py_binary", python_version = kwargs.get("python_version")))
46+
_py_binary(**_with_deprecation(kwargs, name = "py_binary", python_version = kwargs.get("python_version")))
8147

8248
def py_test(**kwargs):
8349
"""[DEPRECATED] Deprecated alias for py_test.
8450
8551
Args:
8652
**kwargs: keyword args forwarded onto {obj}`py_binary`.
8753
"""
88-
_py_test(**with_deprecation(kwargs, name = "py_test", python_version = kwargs.get("python_version")))
54+
_py_test(**_with_deprecation(kwargs, name = "py_test", python_version = kwargs.get("python_version")))

python/private/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ bzl_library(
138138
],
139139
)
140140

141+
bzl_library(
142+
name = "deprecation_bzl",
143+
srcs = ["deprecation.bzl"],
144+
deps = [
145+
"@rules_python_internal//:rules_python_config_bzl",
146+
],
147+
)
148+
141149
bzl_library(
142150
name = "enum_bzl",
143151
srcs = ["enum.bzl"],

python/private/deprecation.bzl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Helper functions to deprecation utilities.
16+
"""
17+
18+
load("@rules_python_internal//:rules_python_config.bzl", "config")
19+
20+
_DEPRECATION_MESSAGE = """
21+
The '{name}' symbol in '{old_load}'
22+
is deprecated. It is an alias to the regular rule; use it directly instead:
23+
24+
load("{new_load}", "{name}")
25+
26+
{snippet}
27+
"""
28+
29+
def _symbol(kwargs, *, symbol_name, new_load, old_load, snippet = ""):
30+
"""An internal function to propagate the deprecation warning.
31+
32+
This is not an API that should be used outside `rules_python`.
33+
34+
Args:
35+
kwargs: Arguments to modify.
36+
symbol_name: {type}`str` the symbol name that is deprecated.
37+
new_load: {type}`str` the new load location under `//`.
38+
old_load: {type}`str` the symbol import location that we are deprecating.
39+
snippet: {type}`str` the usage snippet of the new symbol.
40+
41+
Returns:
42+
The kwargs to be used in the macro creation.
43+
"""
44+
45+
if config.enable_deprecation_warnings:
46+
deprecation = _DEPRECATION_MESSAGE.format(
47+
name = symbol_name,
48+
old_load = old_load,
49+
new_load = new_load,
50+
snippet = snippet,
51+
)
52+
if kwargs.get("deprecation"):
53+
deprecation = kwargs.get("deprecation") + "\n\n" + deprecation
54+
kwargs["deprecation"] = deprecation
55+
return kwargs
56+
57+
with_deprecation = struct(
58+
symbol = _symbol,
59+
)

python/private/internal_config_repo.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ such as globals available to Bazel versions, or propagating user environment
1818
settings for rules to later use.
1919
"""
2020

21+
load(":repo_utils.bzl", "repo_utils")
22+
2123
_ENABLE_PYSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PYSTAR"
2224
_ENABLE_PYSTAR_DEFAULT = "1"
2325
_ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME = "RULES_PYTHON_DEPRECATION_WARNINGS"
@@ -116,4 +118,4 @@ internal_config_repo = repository_rule(
116118
)
117119

118120
def _bool_from_environ(rctx, key, default):
119-
return bool(int(rctx.os.environ.get(key, default)))
121+
return bool(int(repo_utils.getenv(rctx, key, default)))

python/private/toolchains_repo.bzl

Lines changed: 42 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -151,61 +151,37 @@ toolchain_aliases(
151151
rctx.file("defs.bzl", content = """\
152152
# Generated by python/private/toolchains_repo.bzl
153153
154-
load("{rules_python}//python/config_settings:transition.bzl", "with_deprecation")
154+
load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
155+
load("{rules_python}//python/private:deprecation.bzl", "with_deprecation")
156+
load("{rules_python}//python/private:text_util.bzl", "render")
155157
load("{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
156158
load("{rules_python}//python:py_test.bzl", _py_test = "py_test")
157159
load(
158160
"{rules_python}//python/entry_points:py_console_script_binary.bzl",
159161
_py_console_script_binary = "py_console_script_binary",
160162
)
161-
load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
162163
163-
def py_binary(**kwargs):
164-
return _py_binary(
165-
**with_deprecation(
166-
kwargs,
167-
symbol_name = "py_binary",
168-
python_version = "{python_version}",
169-
deprecated = "@{name}//:defs.bzl",
170-
),
164+
def _with_deprecation(kwargs, *, name):
165+
kwargs["python_version"] = "{python_version}"
166+
return with_deprecation.symbol(
167+
kwargs,
168+
symbol_name = name,
169+
old_load = "@{name}//:defs.bzl",
170+
new_load = "@rules_python//python:{{}}.bzl".format(name),
171+
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
171172
)
172173
173-
def py_console_script_binary(name, **kwargs):
174-
return _py_console_script_binary(
175-
name = name,
176-
binary_rule = _py_binary,
177-
**with_deprecation(
178-
kwargs,
179-
symbol_name = "py_console_script_binary",
180-
load_name = "/entry_points:py_console_script_binary",
181-
python_version = "{python_version}",
182-
deprecated = "@{name}//:defs.bzl",
183-
),
184-
)
174+
def py_binary(**kwargs):
175+
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))
185176
186-
def py_test(name, **kwargs):
187-
return _py_test(
188-
name = name,
189-
**with_deprecation(
190-
kwargs,
191-
symbol_name = "py_test",
192-
python_version = "{python_version}",
193-
deprecated = "@{name}//:defs.bzl",
194-
),
195-
)
177+
def py_console_script_binary(**kwargs):
178+
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))
196179
197-
def compile_pip_requirements(name, **kwargs):
198-
return _compile_pip_requirements(
199-
name = name,
200-
**with_deprecation(
201-
kwargs,
202-
symbol_name = "compile_pip_requirements",
203-
load_name = "pip",
204-
python_version = "{python_version}",
205-
deprecated = "@{name}//:defs.bzl",
206-
),
207-
)
180+
def py_test(**kwargs):
181+
return _py_test(**_with_deprecation(kwargs, name = "py_test"))
208182
183+
def compile_pip_requirements(**kwargs):
184+
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
209185
""".format(
210186
name = rctx.attr.name,
211187
python_version = rctx.attr.python_version,
@@ -332,66 +308,42 @@ def _multi_toolchain_aliases_impl(rctx):
332308
rctx.file(file, content = """\
333309
# Generated by python/private/toolchains_repo.bzl
334310
335-
load("@@{rules_python}//python/config_settings:transition.bzl", "with_deprecation")
336-
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
337-
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
311+
load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
312+
load("{rules_python}//python/private:deprecation.bzl", "with_deprecation")
313+
load("{rules_python}//python/private:text_util.bzl", "render")
314+
load("{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
315+
load("{rules_python}//python:py_test.bzl", _py_test = "py_test")
338316
load(
339-
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
317+
"{rules_python}//python/entry_points:py_console_script_binary.bzl",
340318
_py_console_script_binary = "py_console_script_binary",
341319
)
342-
load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
343320
344-
def py_binary(**kwargs):
345-
return _py_binary(
346-
**with_deprecation(
347-
kwargs,
348-
symbol_name = "py_binary",
349-
python_version = "{python_version}",
350-
deprecated = "@{name}//{python_version}:defs.bzl",
351-
),
321+
def _with_deprecation(kwargs, *, name):
322+
kwargs["python_version"] = "{python_version}"
323+
return with_deprecation.symbol(
324+
kwargs,
325+
symbol_name = name,
326+
old_load = "@{name}//{python_version}:defs.bzl",
327+
new_load = "@rules_python//python:{{}}.bzl".format(name),
328+
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
352329
)
353330
354-
def py_console_script_binary(name, **kwargs):
355-
return _py_console_script_binary(
356-
name = name,
357-
binary_rule = _py_binary,
358-
**with_deprecation(
359-
kwargs,
360-
symbol_name = "py_console_script_binary",
361-
load_name = "/entry_points:py_console_script_binary",
362-
python_version = "{python_version}",
363-
deprecated = "@{name}//{python_version}:defs.bzl",
364-
),
365-
)
331+
def py_binary(**kwargs):
332+
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))
366333
367-
def py_test(name, **kwargs):
368-
return _py_test(
369-
name = name,
370-
**with_deprecation(
371-
kwargs,
372-
symbol_name = "py_test",
373-
python_version = "{python_version}",
374-
deprecated = "@{name}//{python_version}:defs.bzl",
375-
),
376-
)
334+
def py_console_script_binary(**kwargs):
335+
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))
377336
378-
def compile_pip_requirements(name, **kwargs):
379-
return _compile_pip_requirements(
380-
name = name,
381-
**with_deprecation(
382-
kwargs,
383-
symbol_name = "compile_pip_requirements",
384-
load_name = "pip",
385-
python_version = "{python_version}",
386-
deprecated = "@{name}//{python_version}:defs.bzl",
387-
),
388-
)
337+
def py_test(**kwargs):
338+
return _py_test(**_with_deprecation(kwargs, name = "py_test"))
389339
340+
def compile_pip_requirements(**kwargs):
341+
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
390342
""".format(
391343
repository_name = repository_name,
392344
name = rctx.attr.name,
393345
python_version = python_version,
394-
rules_python = rules_python,
346+
rules_python = "@" + rules_python,
395347
))
396348
rctx.file("{}/BUILD.bazel".format(python_version), "")
397349

0 commit comments

Comments
 (0)