Skip to content

Commit eeba53f

Browse files
author
Christopher Doris
committed
add docs and tests for PyTuple
1 parent 114cd40 commit eeba53f

File tree

6 files changed

+51
-3
lines changed

6 files changed

+51
-3
lines changed

docs/src/pythoncall-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Apart from a few fundamental immutable types, conversion from Python to Julia `A
181181

182182
```@docs
183183
PyList
184+
PyTuple
184185
PySet
185186
PyDict
186187
PyIterable

docs/src/releasenotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Adds methods `Py(::AbstractString)`, `Py(::AbstractChar)` (previously only builtin string and char types were allowed).
1212
* Adds methods `Py(::Integer)`, `Py(::Rational{<:Integer})`, `Py(::AbstractRange{<:Integer})` (previously only builtin integer types were allowed).
1313
* Adds method `pydict(::Pair...)` to construct a python `dict` from `Pair`s, similar to `Dict`.
14+
* Added `PyTuple` wrapper for Python tuples with typed indexing.
1415
* Bug fixes.
1516
* Internal: switch from Requires.jl to package extensions.
1617

src/Wrap/PyTuple.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ function Base.Tuple(x::PyTuple{T}) where {T<:Tuple}
8080
end
8181
end
8282

83-
# Conversion rule for builtins:tuple -> PyTuple
84-
function pyconvert_rule_tuple(
83+
# Conversion rule for Sequence -> PyTuple
84+
function pyconvert_rule_sequence(
8585
::Type{T},
8686
x::Py,
8787
::Type{T1} = Utils._type_ub(T),

src/Wrap/Wrap.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ function __init__()
7070
)
7171

7272
priority = PYCONVERT_PRIORITY_NORMAL
73-
pyconvert_add_rule("builtins:tuple", PyTuple, pyconvert_rule_tuple, priority)
73+
pyconvert_add_rule(
74+
"collections.abc:Sequence",
75+
PyTuple,
76+
pyconvert_rule_sequence,
77+
priority,
78+
)
7479
pyconvert_add_rule("<arraystruct>", Array, pyconvert_rule_array, priority)
7580
pyconvert_add_rule("<arrayinterface>", Array, pyconvert_rule_array, priority)
7681
pyconvert_add_rule("<array>", Array, pyconvert_rule_array, priority)

test/Convert.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ end
196196
@test x2 == [1, 2, 3]
197197
end
198198

199+
@testitem "sequence → PyTuple" begin
200+
x1 = pyconvert(PyTuple, pylist([1, "foo", "B"]))
201+
@test x1 isa PyTuple{Tuple}
202+
@test isequal(Tuple(x1), (1, "foo", "B"))
203+
x2 = pyconvert(PyTuple{Tuple{Int,Symbol,Char}}, pylist([1, "foo", "B"]))
204+
@test x2 isa PyTuple{Tuple{Int,Symbol,Char}}
205+
@test isequal(Tuple(x2), (1, :foo, 'B'))
206+
end
207+
199208
@testitem "set → PySet" begin
200209
x1 = pyconvert(PySet, pyset([1, 2, 3]))
201210
@test x1 isa PySet{Any}

test/Wrap.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,35 @@ end
570570
@test PyTable isa Type
571571
@test_throws Exception PyTable(0)
572572
end
573+
574+
@testitem "PyTuple" begin
575+
x = pytuple((1, "a"))
576+
y = PyTuple(x)
577+
z = PyTuple{Tuple{Int,String}}(x)
578+
@testset "construct" begin
579+
@test y isa PyTuple{Tuple}
580+
@test z isa PyTuple{Tuple{Int,String}}
581+
@test PythonCall.ispy(y)
582+
@test PythonCall.ispy(z)
583+
@test Py(y) === x
584+
@test Py(z) === x
585+
end
586+
@testset "length" begin
587+
@test length(y) == 2
588+
@test length(z) == 2
589+
v = PyTuple{Tuple{Int,Vararg{String}}}(pytuple((1, "a", "b")))
590+
@test length(v) == 3
591+
end
592+
@testset "getindex" begin
593+
@test_throws BoundsError y[0]
594+
@test y[1] === 1
595+
@test y[2] == "a"
596+
@test z[1] === 1
597+
@test z[2] == "a"
598+
@test_throws BoundsError y[3]
599+
end
600+
@testset "Tuple" begin
601+
@test Tuple(y) == (1, "a")
602+
@test Tuple(z) == (1, "a")
603+
end
604+
end

0 commit comments

Comments
 (0)