From 9631cb7ddabce4b4b52a02a897be00dd755d54a0 Mon Sep 17 00:00:00 2001 From: Christopher Rowley Date: Sun, 2 Nov 2025 11:31:58 +0000 Subject: [PATCH] Remove Py-Number arithmetic methods --- src/Core/Py.jl | 50 ----------------------------------------- test/Core.jl | 60 ++++---------------------------------------------- test/JlWrap.jl | 2 +- 3 files changed, 5 insertions(+), 107 deletions(-) diff --git a/src/Core/Py.jl b/src/Core/Py.jl index c01a2977..02a2fee5 100644 --- a/src/Core/Py.jl +++ b/src/Core/Py.jl @@ -369,19 +369,6 @@ Base.:(<)(x::Py, y::Py) = pylt(Bool, x, y) Base.isless(x::Py, y::Py) = pylt(Bool, x, y) Base.isequal(x::Py, y::Py) = pyeq(Bool, x, y) -# we also allow comparison with numbers -Base.:(==)(x::Py, y::Number) = pyeq(Bool, x, y) -Base.:(<=)(x::Py, y::Number) = pyle(Bool, x, y) -Base.:(<)(x::Py, y::Number) = pylt(Bool, x, y) -Base.isless(x::Py, y::Number) = pylt(Bool, x, y) -Base.isequal(x::Py, y::Number) = pyeq(Bool, x, y) - -Base.:(==)(x::Number, y::Py) = pyeq(Bool, x, y) -Base.:(<=)(x::Number, y::Py) = pyle(Bool, x, y) -Base.:(<)(x::Number, y::Py) = pylt(Bool, x, y) -Base.isless(x::Number, y::Py) = pylt(Bool, x, y) -Base.isequal(x::Number, y::Py) = pyeq(Bool, x, y) - Base.zero(::Type{Py}) = pyint(0) Base.one(::Type{Py}) = pyint(1) @@ -407,44 +394,7 @@ Base.xor(x::Py, y::Py) = pyxor(x, y) Base.:(|)(x::Py, y::Py) = pyor(x, y) Base.:(^)(x::Py, y::Py) = pypow(x, y) -# also allow binary arithmetic with numbers -Base.:(+)(x::Number, y::Py) = pyadd(x, y) -Base.:(-)(x::Number, y::Py) = pysub(x, y) -Base.:(*)(x::Number, y::Py) = pymul(x, y) -# Base.:(+)(x::Number, y::Py) = pymatmul(x, y) -Base.div(x::Number, y::Py) = pyfloordiv(x, y) -Base.:(/)(x::Number, y::Py) = pytruediv(x, y) -Base.rem(x::Number, y::Py) = pymod(x, y) -# Base.:(+)(x::Number, y::Py) = pydivmod(x, y) -Base.:(<<)(x::Number, y::Py) = pylshift(x, y) -Base.:(>>)(x::Number, y::Py) = pyrshift(x, y) -Base.:(&)(x::Number, y::Py) = pyand(x, y) -Base.xor(x::Number, y::Py) = pyxor(x, y) -Base.:(|)(x::Number, y::Py) = pyor(x, y) -Base.:(^)(x::Number, y::Py) = pypow(x, y) - -Base.:(+)(x::Py, y::Number) = pyadd(x, y) -Base.:(-)(x::Py, y::Number) = pysub(x, y) -Base.:(*)(x::Py, y::Number) = pymul(x, y) -# Base.:(+)(x::Py, y::Number) = pymatmul(x, y) -Base.div(x::Py, y::Number) = pyfloordiv(x, y) -Base.:(/)(x::Py, y::Number) = pytruediv(x, y) -Base.rem(x::Py, y::Number) = pymod(x, y) -# Base.:(+)(x::Py, y::Number) = pydivmod(x, y) -Base.:(<<)(x::Py, y::Number) = pylshift(x, y) -Base.:(>>)(x::Py, y::Number) = pyrshift(x, y) -Base.:(&)(x::Py, y::Number) = pyand(x, y) -Base.xor(x::Py, y::Number) = pyxor(x, y) -Base.:(|)(x::Py, y::Number) = pyor(x, y) -Base.:(^)(x::Py, y::Number) = pypow(x, y) - Base.powermod(x::Py, y::Py, z::Py) = pypow(x, y, z) -Base.powermod(x::Number, y::Py, z::Py) = pypow(x, y, z) -Base.powermod(x::Py, y::Number, z::Py) = pypow(x, y, z) -Base.powermod(x::Py, y::Py, z::Number) = pypow(x, y, z) -Base.powermod(x::Number, y::Number, z::Py) = pypow(x, y, z) -Base.powermod(x::Number, y::Py, z::Number) = pypow(x, y, z) -Base.powermod(x::Py, y::Number, z::Number) = pypow(x, y, z) # documentation function Base.Docs.getdoc(x::Py, @nospecialize(sig) = Union{}) diff --git a/test/Core.jl b/test/Core.jl index dcfea098..16574a4a 100644 --- a/test/Core.jl +++ b/test/Core.jl @@ -247,58 +247,6 @@ @test Py(5) >= Py(5) @test !(Py(5) >= Py(6)) end - @testset "Py vs Number" begin - # == - @test Py(1) == 1 - @test !(Py(1) == 2) - @test !(Py(1) == 0) - # != - @test Py(2) != 1 - @test Py(2) != 3 - @test !(Py(2) != 2) - # < - @test Py(3) < 4 - @test !(Py(3) < 3) - @test !(Py(3) < 2) - # <= - @test Py(4) <= 5 - @test Py(4) <= 4 - @test !(Py(4) <= 3) - # > - @test Py(5) > 4 - @test !(Py(5) > 5) - @test !(Py(5) > 6) - # >= - @test Py(5) >= 4 - @test Py(5) >= 5 - @test !(Py(5) >= 6) - end - @testset "Number vs Py" begin - # == - @test 1 == Py(1) - @test !(1 == Py(2)) - @test !(1 == Py(0)) - # != - @test 2 != Py(1) - @test 2 != Py(3) - @test !(2 != Py(2)) - # < - @test 3 < Py(4) - @test !(3 < Py(3)) - @test !(3 < Py(2)) - # <= - @test 4 <= Py(5) - @test 4 <= Py(4) - @test !(4 <= Py(3)) - # > - @test 5 > Py(4) - @test !(5 > Py(5)) - @test !(5 > Py(6)) - # >= - @test 5 >= Py(4) - @test 5 >= Py(5) - @test !(5 >= Py(6)) - end end end @@ -865,13 +813,13 @@ end @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] + x = Py.([1 2; 3 4]) .+ Py(1) + @test isequal(x, Py.([2 3; 4 5])) + x = Py("foo") .* Py.([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]) + @test_throws PyException Py.([1 2; 3 4]) .+ pylist([1, 2]) end @testset "showable" begin @test showable(MIME("text/plain"), Py(nothing)) diff --git a/test/JlWrap.jl b/test/JlWrap.jl index b1497931..b656ab91 100644 --- a/test/JlWrap.jl +++ b/test/JlWrap.jl @@ -718,7 +718,7 @@ end y = pytextio(x) z = y.fileno() @test pyisinstance(z, pybuiltins.int) - @test z == Base.cconvert(Cint, fd(x)) + @test pyconvert(Int, z) == Base.cconvert(Cint, fd(x)) end end @testset "flush" begin