Skip to content

Commit 8dd18ff

Browse files
authored
Merge branch 'main' into rm.groodt
2 parents 9e955b4 + 1567357 commit 8dd18ff

19 files changed

Lines changed: 216 additions & 24 deletions

.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ sphinxdocs
3333
tests/integration/compile_pip_requirements/bazel-compile_pip_requirements
3434
tests/integration/local_toolchains/bazel-local_toolchains
3535
tests/integration/py_cc_toolchain_registered/bazel-py_cc_toolchain_registered
36+
tests/integration/toolchain_target_settings/bazel-module_under_test

.bazelrc.deleted_packages

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ common --deleted_packages=tests/integration/local_toolchains
3636
common --deleted_packages=tests/integration/pip_parse
3737
common --deleted_packages=tests/integration/pip_parse/empty
3838
common --deleted_packages=tests/integration/py_cc_toolchain_registered
39+
common --deleted_packages=tests/integration/toolchain_target_settings
3940
common --deleted_packages=tests/modules/another_module
4041
common --deleted_packages=tests/modules/other
4142
common --deleted_packages=tests/modules/other/nspkg_delta

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ END_UNRELEASED_TEMPLATE
6565
### Fixed
6666
* (gazelle) Fixed handling of auto-included `__init__.py` files when generating `py_binary`
6767
targets ([#3729](https://github.com/bazel-contrib/rules_python/issues/3729)).
68+
* (entry_point) From now on `mypy` type checking will be skipped on the generated
69+
files ([#3126](https://github.com/bazel-contrib/rules_python/issues/3126)).
6870

6971
{#v0-0-0-added}
7072
### Added
73+
* (toolchain) Added {obj}`python.override.toolchain_target_settings` to allow
74+
adding `config_setting` labels to all registered toolchains.
7175
* (windows) Full venv support for Windows is available. Set
7276
{obj}`--venvs_site_packages=yes` to enable.
7377
* (runfiles) Added a pathlib-compatible API: {obj}`Runfiles.root()`

python/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ bzl_library(
8888
name = "packaging_bzl",
8989
srcs = ["packaging.bzl"],
9090
deps = [
91-
":py_binary_bzl",
91+
"//python/entry_points:py_console_script_binary_bzl",
9292
"//python/private:bzlmod_enabled_bzl",
9393
"//python/private:py_package_bzl",
9494
"//python/private:py_wheel_bzl",

python/packaging.bzl

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,12 @@ def py_wheel(
216216
**copy_propagating_kwargs(kwargs)
217217
)
218218
elif twine:
219-
if not twine.endswith(":pkg"):
220-
fail("twine label should look like @my_twine_repo//:pkg")
221-
222-
twine_main = twine.replace(":pkg", ":rules_python_wheel_entry_point_twine.py")
223-
224219
py_binary(
225220
name = "{}.publish".format(name),
226-
srcs = [twine_main],
221+
deps = [twine],
227222
args = twine_args,
228223
data = [dist_target],
229-
imports = ["."],
230-
main = twine_main,
231-
deps = [twine],
224+
main_module = "twine",
232225
tags = manual_tags,
233226
visibility = kwargs.get("visibility"),
234227
**copy_propagating_kwargs(kwargs)

python/private/py_console_script_gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
raise
6363
6464
if __name__ == "__main__":
65-
sys.exit({entry_point}())
65+
sys.exit({entry_point}()) # type: ignore
6666
"""
6767

6868

python/private/pypi/whl_library.bzl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -546,26 +546,28 @@ def _whl_library_impl(rctx):
546546
rctx.file("MODULE.bazel")
547547
rctx.file("REPO.bazel")
548548

549+
# BUILD files interfere with globbing and Bazel package boundaries.
550+
_remove_files(rctx, "BUILD", "BUILD.bazel")
551+
rctx.file("BUILD.bazel", build_file_contents)
552+
553+
if enable_pipstar and enable_pipstar_extract:
554+
if hasattr(rctx, "repo_metadata"):
555+
return rctx.repo_metadata(reproducible = True)
556+
557+
return None
558+
559+
def _remove_files(rctx, *basenames):
549560
paths = list(rctx.path(".").readdir())
550561
for _ in range(10000000):
551562
if not paths:
552563
break
553564
path = paths.pop()
554565

555-
# BUILD files interfere with globbing and Bazel package boundaries.
556-
if path.basename in ("BUILD", "BUILD.bazel"):
566+
if path.basename in basenames:
557567
rctx.delete(path)
558568
elif path.is_dir:
559569
paths.extend(path.readdir())
560570

561-
rctx.file("BUILD.bazel", build_file_contents)
562-
563-
if enable_pipstar and enable_pipstar_extract:
564-
if hasattr(rctx, "repo_metadata"):
565-
return rctx.repo_metadata(reproducible = True)
566-
567-
return None
568-
569571
def _generate_entry_point_contents(
570572
module,
571573
attribute,

python/private/python.bzl

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ def _python_impl(module_ctx):
403403
# the PLATFORMS global for this toolchain
404404
toolchain_platform_keys = {}
405405

406+
# Extra target_settings to add to every registered toolchain, e.g. for
407+
# gating the default toolchains behind a custom config_setting.
408+
global_add_target_settings = py.config.add_target_settings
409+
406410
# Split the toolchain info into separate objects so they can be passed onto
407411
# the repository rule.
408412
for entry in toolchain_impls:
@@ -414,7 +418,7 @@ def _python_impl(module_ctx):
414418

415419
# The target_settings attribute may not be present for users
416420
# patching python/versions.bzl.
417-
toolchain_ts_map[key] = getattr(entry.platform, "target_settings", [])
421+
toolchain_ts_map[key] = getattr(entry.platform, "target_settings", []) + global_add_target_settings
418422
toolchain_platform_keys[key] = entry.platform_name
419423
toolchain_python_versions[key] = entry.full_python_version
420424

@@ -702,6 +706,9 @@ def _process_global_overrides(*, tag, default, _fail = fail):
702706

703707
default["minor_mapping"] = tag.minor_mapping
704708

709+
if tag.add_target_settings:
710+
default["add_target_settings"] = list(tag.add_target_settings)
711+
705712
forwarded_attrs = sorted(AUTH_ATTRS) + [
706713
"base_url",
707714
"register_all_versions",
@@ -809,6 +816,7 @@ def _get_toolchain_config(*, modules, _fail = fail):
809816
)
810817

811818
register_all_versions = default.pop("register_all_versions", False)
819+
add_target_settings = default.pop("add_target_settings", [])
812820
kwargs = default.pop("kwargs", {})
813821

814822
versions = {}
@@ -834,6 +842,7 @@ def _get_toolchain_config(*, modules, _fail = fail):
834842
minor_mapping = minor_mapping,
835843
default = default,
836844
register_all_versions = register_all_versions,
845+
add_target_settings = add_target_settings,
837846
)
838847

839848
def _compute_default_python_version(mctx):
@@ -1099,6 +1108,30 @@ _override = tag_class(
10991108
:::
11001109
""",
11011110
attrs = {
1111+
"add_target_settings": attr.string_list(
1112+
mandatory = False,
1113+
doc = """\
1114+
A list of `config_setting` labels to add to the `target_settings` of every
1115+
toolchain registered by this module extension. This is useful for creating
1116+
separate "families" of toolchains gated behind custom build settings.
1117+
1118+
For example, to ensure the default prebuilt toolchains are only resolved when
1119+
a `prebuilt` config setting is active:
1120+
1121+
```starlark
1122+
python.override(
1123+
add_target_settings = ["@@//:python_toolchain_family_prebuilt"],
1124+
)
1125+
```
1126+
1127+
These settings are appended to the `target_settings` of all toolchains
1128+
registered by the extension, including any that already have settings
1129+
from `python.single_version_platform_override`.
1130+
1131+
:::{versionadded} VERSION_NEXT_FEATURE
1132+
:::
1133+
""",
1134+
),
11021135
"available_python_versions": attr.string_list(
11031136
mandatory = False,
11041137
doc = """\

tests/entry_points/py_console_script_gen_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def test_a_single_entry_point(self):
162162
raise
163163
164164
if __name__ == "__main__":
165-
sys.exit(baz())
165+
sys.exit(baz()) # type: ignore
166166
"""
167167
)
168168
self.assertEqual(want, got)

tests/integration/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ rules_python_integration_test(
8888
py_main = "custom_commands_test.py",
8989
)
9090

91+
rules_python_integration_test(
92+
name = "toolchain_target_settings_test",
93+
py_main = "toolchain_target_settings_test.py",
94+
)
95+
9196
py_library(
9297
name = "runner_lib",
9398
srcs = ["runner.py"],

0 commit comments

Comments
 (0)