Skip to content

Commit 5eaeb24

Browse files
author
Christopher Doris
committed
Py is a scalar when broadcasting
1 parent de31dd2 commit 5eaeb24

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

docs/src/releasenotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Release Notes
22

33
## Unreleased
4+
* `Py` is now treated as a scalar when broadcasting.
45
* Bug fixes.
56

67
## 0.9.15 (2023-10-25)

src/Py/Py.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ Base.in(v, x::Py) = pycontains(x, v)
334334

335335
Base.hash(x::Py, h::UInt) = reinterpret(UInt, Int(pyhash(x))) - 3h
336336

337+
Base.broadcastable(x::Py) = Ref(x)
338+
337339
(f::Py)(args...; kwargs...) = pycall(f, args...; kwargs...)
338340

339341
# comparisons

test/compat.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
@testitem "Base.jl" begin
2+
@testset "broadcast" begin
3+
# Py always broadcasts as a scalar
4+
x = [1 2; 3 4] .+ Py(1)
5+
@test isequal(x, [Py(2) Py(3); Py(4) Py(5)])
6+
x = Py("foo") .* [1 2; 3 4]
7+
@test isequal(x, [Py("foo") Py("foofoo"); Py("foofoofoo") Py("foofoofoofoo")])
8+
# this previously treated the list as a shape (2,) object
9+
# but now tries to do `1 + [1, 2]` which properly fails
10+
@test_throws PyException [1 2; 3 4] .+ pylist([1, 2])
11+
end
12+
end
13+
114
@testitem "pywith" begin
215
@testset "no error" begin
316
tdir = pyimport("tempfile").TemporaryDirectory()

0 commit comments

Comments
 (0)