Skip to content
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

Accept optional argument in ArrayValue.__array__ #159

Closed
wants to merge 6 commits into from
Closed

Accept optional argument in ArrayValue.__array__ #159

wants to merge 6 commits into from

Conversation

moble
Copy link

@moble moble commented Apr 15, 2022

I ran into a situation when passing an SVector{4, Float64} to a python function where ArrayValue.__array__ was called with the optional dtype argument, which is standard for numpy, but caused an error here. Adding this option to the call made the conversion work.

@stevengj
Copy link
Member

stevengj commented Apr 16, 2022

Ah looks like you ran into the same issue as I did earlier today. Presumably this PR fixes #160?

Maybe add a test using the test case in #160?

@moble
Copy link
Author

moble commented Apr 16, 2022

That's a pretty great github jinx!

I've added the test. Testing this code path does require adding numpy as a dependency in CondaPkg.toml, but I think that's probably better anyway.

@stevengj
Copy link
Member

Maybe instead do:

pymoduleexists(mod::AbstractString) = pyconvert(Bool, pyimport("importlib").util.find_spec(mod) != Py(nothing))

if !pymoduleexists("numpy")
   PythonCall.C.CondaPkg.add("numpy")
end

so that it only installs numpy if the test is run?

@moble
Copy link
Author

moble commented Apr 16, 2022

I guess that makes sense,(*) though it'll evidently alter the CondaPkg.toml used to run the tests, which means that people making PRs and such will need to be careful not to commit that change...

That pymoduleexists function looks so handy that, IMHO, it should be exported. I've added it to src/concrete/import.jl and exported it. But this is just my suggestion; I'm open to differing opinions.

I might even suggest that a call like that to CondaPkg would form a nice basis for extending pyimport to act more like the new Julia import — prompting a user to add a package if it's not found, maybe with options for version and channel info. The conversation by @icweaver and @sairus7 indicates there may be some interest in such functionality.


(*) Except that it has to be pyimport("importlib.util").find_spec(m).

@stevengj
Copy link
Member

I might even suggest that a call like that to CondaPkg would form a nice basis for extending pyimport to act more like the new Julia import — prompting a user to add a package if it's not found, maybe with options for version and channel info. The conversation by @icweaver and @sairus7 indicates there may be some interest in such functionality.

PyCall has something like that with pyimport_conda (though without a prompt), but I think that probably should be a topic for another issue?

@@ -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.)

@cjdoris
Copy link
Collaborator

cjdoris commented Apr 20, 2022

Thanks for the PR! Could you revert all the changes except those to array.jl please? I'm happy to discuss pymoduleexists in another ticket, though my gut is to just let the user call find_spec themselves. I don't mind this not being tested right now, this clearly works, and there is a lot of test writing to be done anyway.

@cjdoris
Copy link
Collaborator

cjdoris commented Apr 21, 2022

I've just pushed the changes to __array__(). Happy to discuss the other changes on a separate ticket.

@cjdoris cjdoris closed this Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants