Skip to content

Commit

Permalink
Py is a scalar when broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Doris committed Nov 4, 2023
1 parent de31dd2 commit 5eaeb24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/releasenotes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Release Notes

## Unreleased
* `Py` is now treated as a scalar when broadcasting.
* Bug fixes.

## 0.9.15 (2023-10-25)
Expand Down
2 changes: 2 additions & 0 deletions src/Py/Py.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ Base.in(v, x::Py) = pycontains(x, v)

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

Base.broadcastable(x::Py) = Ref(x)

(f::Py)(args...; kwargs...) = pycall(f, args...; kwargs...)

# comparisons
Expand Down
13 changes: 13 additions & 0 deletions test/compat.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
@testitem "Base.jl" begin
@testset "broadcast" begin
# Py always broadcasts as a scalar
x = [1 2; 3 4] .+ Py(1)
@test isequal(x, [Py(2) Py(3); Py(4) Py(5)])
x = Py("foo") .* [1 2; 3 4]
@test isequal(x, [Py("foo") Py("foofoo"); Py("foofoofoo") Py("foofoofoofoo")])
# this previously treated the list as a shape (2,) object
# but now tries to do `1 + [1, 2]` which properly fails
@test_throws PyException [1 2; 3 4] .+ pylist([1, 2])
end
end

@testitem "pywith" begin
@testset "no error" begin
tdir = pyimport("tempfile").TemporaryDirectory()
Expand Down

0 comments on commit 5eaeb24

Please sign in to comment.