Skip to content

Commit 0966b76

Browse files
author
Christopher Doris
committed
pytype tweaks
1 parent 93e4b00 commit 0966b76

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

docs/src/pythoncall.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ Notable exceptions are:
8484
- [`pyconvert`](@ref) to convert a Python object to a Julia object.
8585
- [`pyimport`](@ref) to import a Python module.
8686
- [`pyjl`](@ref) to directly wrap a Julia object as a Python object.
87-
- [`pyclass`](@ref) to construct a new class.
8887
- [`pywith`](@ref) to emulate the Python `with` statement.
8988

9089
If a Julia value is passed as an argument to one of these functions, it is converted to a

src/concrete/type.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Create a new type. Equivalent to `type(name, bases, dict)` in Python.
1313
1414
If `bases` is not a Python object, it is converted to one using `pytuple`.
1515
16-
If `dict` is not a Python object, it is converted to one using `pydict`.
16+
The `dict` may either by a Python object or a Julia iterable. In the latter case, each item
17+
may either be a `name => value` pair or a Python object with a `__name__` attribute.
1718
1819
In order to use a Julia `Function` as an instance method, it must be wrapped into a Python
1920
function with [`pyfunc`](@ref). Similarly, see also [`pyclassmethod`](@ref),
@@ -24,7 +25,10 @@ to the function always have type `Py`. See the example below.
2425
2526
```
2627
Foo = pytype("Foo", (), [
27-
"__init__" => pyfunc(
28+
"__module__" => "__main__",
29+
30+
pyfunc(
31+
name = "__init__",
2832
doc = \"\"\"
2933
Specify x and y to store in the Foo.
3034
@@ -37,19 +41,22 @@ Foo = pytype("Foo", (), [
3741
end,
3842
),
3943
40-
"__repr__" => function (self)
41-
return "Foo(\$(self.x), \$(self.y))"
42-
end |> pyfunc,
44+
pyfunc(
45+
name = "__repr__",
46+
self -> "Foo(\$(self.x), \$(self.y))",
47+
),
4348
44-
"frompair" => pyclassmethod(
49+
pyclassmethod(
50+
name = "frompair",
4551
doc = "Construct a Foo from a tuple of length two.",
4652
(cls, xy) -> cls(xy...),
4753
),
4854
49-
"hello" => pystaticmethod(
55+
pystaticmethod(
56+
name = "hello",
5057
doc = "Prints a friendly greeting.",
5158
(name) -> println("Hello, \$name"),
52-
)
59+
),
5360
5461
"xy" => pyproperty(
5562
doc = "A tuple of x and y.",
@@ -66,7 +73,7 @@ Foo = pytype("Foo", (), [
6673
"""
6774
function pytype(name, bases, dict)
6875
bases2 = ispy(bases) ? bases : pytuple(bases)
69-
dict2 = ispy(dict) ? dict : pydict(dict)
76+
dict2 = ispy(dict) ? dict : pydict(ispy(item) ? (pygetattr(item, "__name__") => item) : item for item in dict)
7077
pybuiltins.type(name, bases2, dict2)
7178
end
7279

src/jlwrap/callback.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ end
4646
pyjlcallback(f) = pyjl(pyjlcallbacktype, f)
4747

4848
"""
49-
pyfunc(f; [name], [doc])
49+
pyfunc(f; name=nothing, doc=nothing)
5050
5151
Wrap the callable `f` as an ordinary Python function.
5252

test/concrete.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,6 @@ end
248248
@test pyeq(Bool, x.__name__, "Foo")
249249
@test pyeq(Bool, x.foo, 1)
250250
@test pyeq(Bool, x.bar, 2)
251-
x = pyclass("Bar", (), foo=3, bar=4)
252-
@test pyisinstance(x, pybuiltins.type)
253-
@test pyeq(Bool, x.__name__, "Bar")
254-
@test pyeq(Bool, x.foo, 3)
255-
@test pyeq(Bool, x.bar, 4)
256251
end
257252

258253
@testset "fraction" begin

0 commit comments

Comments
 (0)