Skip to content

Accept optional argument in ArrayValue.__array__ #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ julia = "1"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "Test"]
test = ["Aqua", "Random", "Test"]
8 changes: 8 additions & 0 deletions src/concrete/import.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ pyimport((m,k)::Pair) = (m_=pyimport(m); k_=pygetattr(m_,k); pydel!(m_); k_)
pyimport((m,ks)::Pair{<:Any,<:Tuple}) = (m_=pyimport(m); ks_=map(k->pygetattr(m_,k), ks); pydel!(m_); ks_)
pyimport(m1, m2, ms...) = map(pyimport, (m1, m2, ms...))
export pyimport

"""
pymoduleexists(m)

Check if module `m` can be found, without actually importing it.
"""
pymoduleexists(m::AbstractString) = pyconvert(Bool, pyimport("importlib.util").find_spec(m) != Py(nothing))
export pymoduleexists
4 changes: 2 additions & 2 deletions src/jlwrap/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function init_jlwrap_array()
@property
def __array_interface__(self):
return self._jl_callmethod($(pyjl_methodnum(pyjlarray_array_interface)))
def __array__(self):
def __array__(self, dtype=None):
# convert to an array-like object
arr = self
if not (hasattr(arr, "__array_interface__") or hasattr(arr, "__array_struct__")):
Expand All @@ -326,7 +326,7 @@ function init_jlwrap_array()
# convert to a numpy array if numpy is available
try:
import numpy
arr = numpy.array(arr)
arr = numpy.array(arr, dtype=dtype)
except ImportError:
pass
return arr
Expand Down
3 changes: 3 additions & 0 deletions test/concrete.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@testset "import" begin
@test pymoduleexists("sys")
@test pymoduleexists("os")
@test !pymoduleexists(randstring(32))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@test !pymoduleexists(randstring(32))
@test !pymoduleexists("rDJb5uy3eTorFw4c7UbcGOC59FjVGcnB") # randstring(32)

No reason to choose a different random string for each test run.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Then you can also remove the Random dependency.)

sys = pyimport("sys")
os = pyimport("os")
@test pyeq(Bool, sys.__name__, "sys")
Expand Down
16 changes: 16 additions & 0 deletions test/jlwrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,19 @@
x4 = pyconvert(Vector{Int}, x3)
@test x1 == x4
end

@testset "dtypes" begin
if !pymoduleexists("numpy")
PythonCall.C.CondaPkg.add("numpy")
end
np = pyimport("numpy");
y = range(-5,5,length=11)
arr = np.asarray(y)
@test pyconvert(Int, arr.size) == 11
@test pyconvert(String, arr.dtype.name) == "float64"
@test all(iszero.(pyconvert(Any, arr) .- y))
arr32 = np.asarray(y, dtype=np.float32)
@test pyconvert(Int, arr32.size) == 11
@test pyconvert(String, arr32.dtype.name) == "float32"
@test all(iszero.(pyconvert(Any, arr32) .- y))
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using PythonCall, Test, Dates, Aqua
using PythonCall, Test, Dates, Random, Aqua

# The unbound_args test fails on methods with signature like foo(::Type{Tuple{Vararg{V}}}) where V
# Seems like a bug.
Expand Down