CodeSampleX

示例

numpy 2.5.1: broadcast_to

已验证示例 — pypi numpy 2.5.1: broadcast_to. contract 在 python 3.12 · linux debian/x64 · docker 上运行并通过: assert numpy.broadcast_to broadcasts 1D array to…

sha256:9eabce8c33c420f6100a7e2b6d48863406206f85eb79ba3af81aa3612e5e10a0

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 pip

验证运行环境

环境 契约 阶段 运行日期
python 3.12 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1python:3.12-slim@sha256:09f7da3bc104…
2026-09-01

案例

HOW
目标
verify numpy.broadcast_to in pkg:pypi/numpy@2.5.1
包
符号
  • numpy.broadcast_to
创建时间
2026-09-01T21:58:03Z

契约

  1. assert numpy.broadcast_to broadcasts 1D array to compatible 2D shape producing a view with zero stride
  2. assert numpy.broadcast_to broadcasts scalar and 0D array to arbitrary target shape
  3. assert numpy.broadcast_to expands singleton dimensions to target shape
  4. assert numpy.broadcast_to returns read-only view with writeable flag set to False
  5. assert numpy.broadcast_to raises ValueError when target shape is incompatible with input shape
  6. assert numpy.broadcast_to preserves ndarray subclasses when subok is True and forces base ndarray when subok is False

文件

  • PROMPT.md
  • csx.json
  • pyproject.toml
  • requirements.txt
  • spec.json
  • test/contract.py

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify numpy.broadcast_to in pkg:pypi/numpy@2.5.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:pypi/numpy@2.5.1
Demonstrate these symbols/APIs:
  - numpy.broadcast_to

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:1993163957d610a0d0df4506244a594a56920f1b2db5c0b7523af0ecf6428f23","contract":["assert numpy.broadcast_to broadcasts 1D array to compatible 2D shape producing a view with zero stride","assert numpy.broadcast_to broadcasts scalar and 0D array to arbitrary target shape","assert numpy.broadcast_to expands singleton dimensions to target shape","assert numpy.broadcast_to returns read-only view with writeable flag set to False","assert numpy.broadcast_to raises ValueError when target shape is incompatible with input shape","assert numpy.broadcast_to preserves ndarray subclasses when subok is True and forces base ndarray when subok is False"],"goal":"verify numpy.broadcast_to in pkg:pypi/numpy@2.5.1","kind":"HOW","packages":["pkg:pypi/numpy@2.5.1"],"schemaVersion":1,"symbols":["numpy.broadcast_to"]},"contractCommand":["python3","test/contract.py"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"pypi","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"pip","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/numpy@2.5.1"],"schemaVersion":1,"subject":"pkg:pypi/numpy@2.5.1","symbols":["numpy.broadcast_to"],"verifierAdapter":"python@1"}
pyproject.toml
[project]
name = "verify-numpy-broadcast-to"
version = "0.1.0"
dependencies = [
    "numpy==2.5.1",
]
requirements.txt
numpy==2.5.1
spec.json
{
  "schemaVersion": 1,
  "goal": "verify numpy.broadcast_to in pkg:pypi/numpy@2.5.1",
  "kind": "HOW",
  "packages": [
    "pkg:pypi/numpy@2.5.1"
  ],
  "symbols": [
    "numpy.broadcast_to"
  ]
}
test/contract.py
import numpy as np


def test_broadcast_to_1d_to_2d():
    arr = np.array([1, 2, 3])
    b = np.broadcast_to(arr, (2, 3))
    assert b.shape == (2, 3)
    assert np.array_equal(b, np.array([[1, 2, 3], [1, 2, 3]]))
    assert b.strides[0] == 0
    assert b.strides[1] == arr.strides[0]


def test_broadcast_to_scalar_and_0d():
    b_scalar = np.broadcast_to(7, (2, 3))
    assert b_scalar.shape == (2, 3)
    assert np.all(b_scalar == 7)

    arr_0d = np.array(42)
    b_0d = np.broadcast_to(arr_0d, (3, 1, 2))
    assert b_0d.shape == (3, 1, 2)
    assert np.all(b_0d == 42)


def test_broadcast_to_singleton_expansion():
    arr = np.array([[1], [2], [3]])
    b = np.broadcast_to(arr, (3, 4))
    assert b.shape == (3, 4)
    assert np.array_equal(b, np.array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]))
    assert b.strides == (arr.strides[0], 0)

    arr_2d = np.array([[10, 20], [30, 40]])
    b_3d = np.broadcast_to(arr_2d, (3, 2, 2))
    assert b_3d.shape == (3, 2, 2)
    assert b_3d.strides[0] == 0
    assert np.array_equal(b_3d[0], arr_2d)
    assert np.array_equal(b_3d[1], arr_2d)
    assert np.array_equal(b_3d[2], arr_2d)


def test_broadcast_to_readonly_view():
    arr = np.array([1, 2, 3])
    b = np.broadcast_to(arr, (2, 3))
    assert b.flags.writeable is False
    try:
        b[0, 0] = 99
        assert False, "Assignment to broadcast_to view should raise ValueError"
    except ValueError:
        pass


def test_broadcast_to_incompatible_shape():
    arr = np.array([1, 2, 3])
    try:
        np.broadcast_to(arr, (2, 2))
        assert False, "Incompatible shape should raise ValueError"
    except ValueError:
        pass
    try:
        np.broadcast_to(arr, (2,))
        assert False, "Incompatible shape should raise ValueError"
    except ValueError:
        pass


def test_broadcast_to_subok():
    class CustomArray(np.ndarray):
        pass

    sub_arr = np.array([1, 2, 3]).view(CustomArray)
    b_subok_true = np.broadcast_to(sub_arr, (2, 3), subok=True)
    assert isinstance(b_subok_true, CustomArray)
    assert b_subok_true.shape == (2, 3)

    b_subok_false = np.broadcast_to(sub_arr, (2, 3), subok=False)
    assert type(b_subok_false) is np.ndarray
    assert b_subok_false.shape == (2, 3)


def main():
    test_broadcast_to_1d_to_2d()
    test_broadcast_to_scalar_and_0d()
    test_broadcast_to_singleton_expansion()
    test_broadcast_to_readonly_view()
    test_broadcast_to_incompatible_shape()
    test_broadcast_to_subok()
    print("All contract assertions passed.")


if __name__ == "__main__":
    main()

原始种子者

匿名