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

isvalid error with PythonCall.Utils.StaticString #254

Closed
CarloLucibello opened this issue Dec 24, 2022 · 5 comments
Closed

isvalid error with PythonCall.Utils.StaticString #254

CarloLucibello opened this issue Dec 24, 2022 · 5 comments

Comments

@CarloLucibello
Copy link
Contributor

CarloLucibello commented Dec 24, 2022

When converting to PyArray some numpy arrays with string elements, I end up with strings of type PythonCall.Utils.StaticString{UInt32, 44}.

It seems that this type is not well supported though, and I get the following errors when indexing and computing the length:

julia> s = PythonCall.Utils.StaticString{UInt32, 44}("ababababb")
"ababababb"

julia> s[1]
ERROR: MethodError: no method matching isvalid(::PythonCall.Utils.StaticString{UInt32, 44}, ::Int64)
Closest candidates are:
  isvalid(::AbstractString, ::Integer) at strings/basic.jl:143
  isvalid(::SubstitutionString, ::Integer) at regex.jl:566
  isvalid(::Type{T}, ::Integer) where T<:AbstractChar at strings/unicode.jl:58
  ...
Stacktrace:
 [1] isvalid
   @ ./strings/basic.jl:143 [inlined]
 [2] getindex(s::PythonCall.Utils.StaticString{UInt32, 44}, i::Int64)
   @ Base ./strings/basic.jl:186
 [3] top-level scope
   @ REPL[5]:1

julia> length(s)
ERROR: MethodError: no method matching isvalid(::PythonCall.Utils.StaticString{UInt32, 44}, ::Int64)
Closest candidates are:
  isvalid(::AbstractString, ::Integer) at strings/basic.jl:143
  isvalid(::SubstitutionString, ::Integer) at regex.jl:566
  isvalid(::Type{T}, ::Integer) where T<:AbstractChar at strings/unicode.jl:58
  ...
Stacktrace:
 [1] isvalid
   @ ./strings/basic.jl:143 [inlined]
 [2] length
   @ ./strings/basic.jl:396 [inlined]
 [3] length(s::PythonCall.Utils.StaticString{UInt32, 44})
   @ Base ./strings/basic.jl:387
 [4] top-level scope
   @ REPL[6]:1

julia> s isa AbstractString # in principle the type should implement the whole interface
true
@CarloLucibello CarloLucibello changed the title isvalid error with PythonCall.Utils.StaticString{UInt32, 44} isvalid error with PythonCall.Utils.StaticString Dec 24, 2022
@cjdoris
Copy link
Collaborator

cjdoris commented Jan 12, 2023

Yeah, PythonCall.Utils.StaticString is pretty minimal in what it supports - you can convert them to String but not much else.

Supporting indexing is harder because it requires understanding UTF-8 and UTF-32 - AFAIK Julia doesn't provide much support for implementing your own string types in terms of handling the encoding (even UTF-8) except for the transcode function, which I use to convert to String.

If you are getting these from a PyArray, I have just pushed a change to the main branch so that you can do PyArray{String}(x) on a Python array of strings, which converts the elements to String when you access them. Of course this is slower than accessing them as PythonCall.Util.StaticString but if you need indexing then it's an option.

@CarloLucibello
Copy link
Contributor Author

I see you have implemented indexing in the last commits, thanks, the examples in the OP now work:

julia> s = PythonCall.Utils.StaticString{UInt32, 44}("ababababb")
"ababababb"

julia> s[1]
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> length(s)
9

julia> s[1:2]
"ab"

julia> s[1:2] |> typeof
SubString{PythonCall.Utils.StaticString{UInt32, 44}}

Can we get a new release?

CarloLucibello added a commit to CarloLucibello/PythonCall.jl that referenced this issue Jan 15, 2023
@cjdoris
Copy link
Collaborator

cjdoris commented Jan 17, 2023

I decided it wasn't so hard! Sure I'll make a release this week.

cjdoris pushed a commit that referenced this issue Jan 17, 2023
@CarloLucibello
Copy link
Contributor Author

Hi, sorry to bother but it would be great to have a new release!

@cjdoris
Copy link
Collaborator

cjdoris commented Feb 15, 2023

Sure - done.

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

No branches or pull requests

2 participants