Skip to content

Commit d969fbc

Browse files
author
Christopher Rowley
committed
tidying
1 parent 946f6a8 commit d969fbc

File tree

3 files changed

+40
-48
lines changed

3 files changed

+40
-48
lines changed

docs/src/juliacall-reference.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ from juliacall import Main as jl
201201
jl.Vector[jl.Int]()
202202
```
203203
204-
If NumPy is available, primitive types expose a `__numpy_dtype__` property that returns the
205-
corresponding `numpy.dtype` (e.g. `jl.Int64.__numpy_dtype__`). Unsupported types raise
206-
`AttributeError`.
204+
Some Julia types can be converted to corresponding numpy dtypes like `numpy.dtype(jl.Int)`.
205+
Currently supports these primitive types: `Bool`, `IntXX`, `UIntXX`, `FloatXX`,
206+
`ComplexFXX`, `Ptr{Cvoid}`.
207207
`````
208208

209209
`````@customdoc

src/JlWrap/type.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ function pyjltype_numpy_dtype(self::Type)
4444
elseif self === Ptr{Cvoid}
4545
return np.dtype("P")
4646
end
47-
@static if Int !== Int64
48-
if self === Int
49-
return np.dtype(np.int_)
50-
end
51-
end
52-
@static if UInt !== UInt64
53-
if self === UInt
54-
return np.dtype(np.uintp)
55-
end
56-
end
5747
errset(pybuiltins.AttributeError, "__numpy_dtype__")
5848
return PyNULL
5949
end

test/JlWrap.jl

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ end
472472
end
473473
end
474474

475-
@testitem "type" setup=[Setup] begin
475+
@testitem "type" setup = [Setup] begin
476476
@testset "type" begin
477477
@test pyis(pytype(pyjl(Int)), PythonCall.pyjltypetype)
478478
end
@@ -482,44 +482,46 @@ end
482482
@testset "numpy dtype" begin
483483
if Setup.devdeps
484484
np = pyimport("numpy")
485-
@test pyeq(Bool, pygetattr(pyjl(Bool), "__numpy_dtype__"), np.dtype(np.bool_))
486-
@test pyeq(Bool, pygetattr(pyjl(Int8), "__numpy_dtype__"), np.dtype(np.int8))
487-
@test pyeq(Bool, pygetattr(pyjl(Int16), "__numpy_dtype__"), np.dtype(np.int16))
488-
@test pyeq(Bool, pygetattr(pyjl(Int32), "__numpy_dtype__"), np.dtype(np.int32))
489-
@test pyeq(Bool, pygetattr(pyjl(Int64), "__numpy_dtype__"), np.dtype(np.int64))
490-
@test pyeq(Bool, pygetattr(pyjl(Int), "__numpy_dtype__"), np.dtype(np.int_))
491-
@test pyeq(Bool, pygetattr(pyjl(UInt8), "__numpy_dtype__"), np.dtype(np.uint8))
492-
@test pyeq(Bool, pygetattr(pyjl(UInt16), "__numpy_dtype__"), np.dtype(np.uint16))
493-
@test pyeq(Bool, pygetattr(pyjl(UInt32), "__numpy_dtype__"), np.dtype(np.uint32))
494-
@test pyeq(Bool, pygetattr(pyjl(UInt64), "__numpy_dtype__"), np.dtype(np.uint64))
495-
@test pyeq(Bool, pygetattr(pyjl(UInt), "__numpy_dtype__"), np.dtype(np.uintp))
496-
@test pyeq(Bool, pygetattr(pyjl(Float16), "__numpy_dtype__"), np.dtype(np.float16))
497-
@test pyeq(Bool, pygetattr(pyjl(Float32), "__numpy_dtype__"), np.dtype(np.float32))
498-
@test pyeq(Bool, pygetattr(pyjl(Float64), "__numpy_dtype__"), np.dtype(np.float64))
499-
@test pyeq(Bool, pygetattr(pyjl(ComplexF32), "__numpy_dtype__"), np.dtype(np.complex64))
500-
@test pyeq(Bool, pygetattr(pyjl(ComplexF64), "__numpy_dtype__"), np.dtype(np.complex128))
501-
@test pyeq(Bool, pygetattr(pyjl(Ptr{Cvoid}), "__numpy_dtype__"), np.dtype("P"))
502-
@test pyeq(Bool, np.dtype(pyjl(Int64)), np.dtype(np.int64))
503485

504-
err = try
505-
pygetattr(pyjl(ComplexF16), "__numpy_dtype__")
506-
nothing
507-
catch err
508-
err
486+
# success cases
487+
@testset "$t -> $d" for (t, d) in [
488+
(Bool, "bool"),
489+
(Int8, "int8"),
490+
(Int16, "int16"),
491+
(Int32, "int32"),
492+
(Int64, "int64"),
493+
(UInt8, "uint8"),
494+
(UInt16, "uint16"),
495+
(UInt32, "uint32"),
496+
(UInt64, "uint64"),
497+
(Float16, "float16"),
498+
(Float32, "float32"),
499+
(Float64, "float64"),
500+
(ComplexF32, "complex64"),
501+
(ComplexF64, "complex128"),
502+
(Ptr{Cvoid}, "P"),
503+
]
504+
@test pyeq(Bool, pygetattr(pyjl(t), "__numpy_dtype__"), np.dtype(d))
505+
@test pyeq(Bool, np.dtype(pyjl(t)), np.dtype(d))
509506
end
510-
@test err isa PythonCall.PyException
511-
@test pyis(err._t, pybuiltins.AttributeError)
512507

513-
err = try
514-
pygetattr(pyjl(String), "__numpy_dtype__")
515-
nothing
516-
catch err
517-
err
508+
# unsupported cases
509+
@testset "$t -> AttributeError" for t in [
510+
ComplexF16,
511+
String,
512+
Tuple{},
513+
Ptr{Int},
514+
Ptr{PythonCall.C.PyPtr},
515+
]
516+
err = try
517+
pygetattr(pyjl(t), "__numpy_dtype__")
518+
nothing
519+
catch err
520+
err
521+
end
522+
@test err isa PythonCall.PyException
523+
@test pyis(err.t, pybuiltins.AttributeError)
518524
end
519-
@test err isa PythonCall.PyException
520-
@test pyis(err._t, pybuiltins.AttributeError)
521-
else
522-
@test_skip Setup.devdeps
523525
end
524526
end
525527
end

0 commit comments

Comments
 (0)