Skip to content

Commit eb771e1

Browse files
authored
Relax conversions to python (#671)
* Allow any string or integer to be converted to python Specifically this adds methods `Py(::AbstractString)`, `Py(::AbstractChar)`, `Py(::Integer)`, `Py(::Rational{<:Integer})`, `Py(::AbstractRange{<:Integer})` whereas previously we only allowed the builtin Julia string, char and integer types. * update the docs for conversion to python * document new methods --------- Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent a9b77e4 commit eb771e1

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

docs/src/conversion-to-python.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ From Python, this occurs when converting the return value of a Julia function.
1212
| From | To |
1313
| :------------------------------------------------------------------ | :------------------------------------------------------ |
1414
| Any Python object type (`Py`, `PyList`, etc.) | itself |
15-
| `Nothing`, `Missing` | `None` |
15+
| `Nothing` | `None` |
1616
| `Bool` | `bool` |
17-
| Standard integer (`IntXX`, `UIntXX`, `BigInt`) | `int` |
18-
| Standard rational (`Rational{T}`, `T` a standard integer) | `fractions.Fraction` |
19-
| Standard float (`FloatXX`) | `float` |
20-
| Standard complex (`Complex{T}`, `T` a standard float) | `complex` |
21-
| Standard string/char (`String` and `SubString{String}`, `Char`) | `str` |
22-
| `Tuple` | `tuple` |
23-
| Standard integer range (`AbstractRange{T}`, `T` a standard integer) | `range` |
24-
| `Date`, `Time`, `DateTime` (from `Dates`) | `date`, `time`, `datetime` (from `datetime`) |
25-
| `Second`, `Millisecond`, `Microsecond`, `Nanosecond` (from `Dates`) | `timedelta` (from `datetime`) |
17+
| `Integer` | `int` |
18+
| `Rational{<:Integer}` | `fractions.Fraction` |
19+
| `Float64`, `Float32`, `Float16` | `float` |
20+
| `Complex{Float64}`, `Complex{Float32}`, `Complex{Float16}` | `complex` |
21+
| `AbstractString`, `AbstractChar` | `str` |
22+
| `Base.CodeUnits{UInt8}` (e.g. `b"example"`) | `bytes` |
23+
| `Tuple`, `Pair` | `tuple` |
24+
| `AbstractRange{<:Integer}` | `range` |
25+
| `Dates.Date` | `datetime.date` |
26+
| `Dates.Time` | `datetime.time` |
27+
| `Dates.DateTime` | `datetime.datetime` |
28+
| `Dates.Second`, `Dates.Millisecond`, `Dates.Microsecond`, `Dates.Nanosecond` | `datetime.timedelta` |
2629
| `Number` | `juliacall.NumberValue`, `juliacall.ComplexValue`, etc. |
2730
| `AbstractArray` | `juliacall.ArrayValue`, `juliacall.VectorValue` |
2831
| `AbstractDict` | `juliacall.DictValue` |

docs/src/releasenotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* If `JULIA_PYTHONCALL_EXE` is a relative path, it is now considered relative to the active project.
99
* Added option `JULIA_PYTHONCALL_EXE=@venv` to use a Python virtual environment relative to the active project.
1010
* Added `PYTHON_JULIACALL_EXE` and `PYTHON_JULIACALL_PROJECT` for specifying the Julia binary and project to override JuliaPkg.
11+
* Adds methods `Py(::AbstractString)`, `Py(::AbstractChar)` (previously only builtin string and char types were allowed).
12+
* Adds methods `Py(::Integer)`, `Py(::Rational{<:Integer})`, `Py(::AbstractRange{<:Integer})` (previously only builtin integer types were allowed).
1113
* Bug fixes.
1214
* Internal: switch from Requires.jl to package extensions.
1315

src/Core/Py.jl

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,16 @@ end
122122
Py(x::Py) = x
123123
Py(x::Nothing) = pybuiltins.None
124124
Py(x::Bool) = x ? pybuiltins.True : pybuiltins.False
125-
Py(x::Union{String,SubString{String},Char}) = pystr(x)
125+
Py(x::Union{AbstractString,AbstractChar}) = pystr(x)
126126
Py(x::Base.CodeUnits{UInt8,String}) = pybytes(x)
127127
Py(x::Base.CodeUnits{UInt8,SubString{String}}) = pybytes(x)
128128
Py(x::Tuple) = pytuple_fromiter(x)
129129
Py(x::Pair) = pytuple_fromiter(x)
130-
Py(x::Union{Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128,BigInt}) =
131-
pyint(x)
132-
Py(
133-
x::Rational{
134-
<:Union{Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128,BigInt},
135-
},
136-
) = pyfraction(x)
130+
Py(x::Integer) = pyint(x)
131+
Py(x::Rational{<:Integer}) = pyfraction(x)
137132
Py(x::Union{Float16,Float32,Float64}) = pyfloat(x)
138-
Py(x::Complex{<:Union{Float16,Float32,Float64}}) = pycomplex(x)
139-
Py(
140-
x::AbstractRange{
141-
<:Union{Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128,BigInt},
142-
},
143-
) = pyrange_fromrange(x)
133+
Py(x::Union{Complex{Float16},Complex{Float32},Complex{Float64}}) = pycomplex(x)
134+
Py(x::AbstractRange{<:Integer}) = pyrange_fromrange(x)
144135
Py(x::Date) = pydate(x)
145136
Py(x::Time) = pytime(x)
146137
Py(x::DateTime) = pydatetime(x)

src/Core/builtins.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ pystr(x) = pynew(errcheck(@autopy x C.PyObject_Str(x_)))
592592
pystr(x::String) = pystr_fromUTF8(x)
593593
pystr(x::SubString{String}) = pystr_fromUTF8(x)
594594
pystr(x::Char) = pystr(string(x))
595+
pystr(x::AbstractString) = pystr(convert(String, x)::String)
596+
pystr(x::AbstractChar) = pystr(convert(Char, x)::Char)
595597
pystr(::Type{String}, x) = (s = pystr(x); ans = pystr_asstring(s); pydel!(s); ans)
596598

597599
pystr_asUTF8bytes(x::Py) = pynew(errcheck(C.PyUnicode_AsUTF8String(x)))

0 commit comments

Comments
 (0)