Skip to content

Commit 93e4b00

Browse files
author
Christopher Doris
committed
remove pyclass
1 parent 4ebceed commit 93e4b00

File tree

4 files changed

+82
-88
lines changed

4 files changed

+82
-88
lines changed

docs/src/pythoncall.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -96,60 +96,60 @@ These functions construct Python objects of builtin types from Julia values.
9696

9797
```@docs
9898
pybool
99-
pyint
100-
pyfloat
101-
pycomplex
102-
pystr
99+
pycollist
103100
pybytes
104-
pytuple
101+
pycomplex
102+
pydict
103+
pyfloat
104+
pyfrozenset
105+
pyint
105106
pylist
106-
pycollist
107+
pyrange
107108
pyrowlist
108109
pyset
109-
pyfrozenset
110-
pydict
111110
pyslice
112-
pyrange
113-
pytype
111+
pystr
112+
pytuple
114113
```
115114

116115
### Builtins
117116

118117
These functions mimic the Python builtin functions or keywords of the same name.
119118

120119
```@docs
121-
pyimport
122-
pywith
123-
pyis
124-
pyrepr
120+
pyall
121+
pyany
125122
pyascii
126-
pyhasattr
127-
pygetattr
128-
pysetattr
129-
pydelattr
130-
pydir
131123
pycall
132-
pylen
124+
pycallable
125+
pycompile
133126
pycontains
134-
pyin
135-
pygetitem
136-
pysetitem
127+
pydelattr
137128
pydelitem
138-
pyissubclass
139-
pyisinstance
129+
pydir
130+
pyeval
131+
@pyeval
132+
pyexec
133+
@pyexec
134+
pygetattr
135+
pygetitem
136+
pyhasattr
140137
pyhash
138+
pyhelp
139+
pyimport
140+
pyin
141+
pyis
142+
pyisinstance
143+
pyissubclass
141144
pyiter
145+
pylen
142146
pynext
143-
pyhelp
144147
pyprint
145-
pyall
146-
pyany
147-
pycallable
148-
pyeval
149-
pyexec
150-
pycompile
151-
@pyeval
152-
@pyexec
148+
pyrepr
149+
pysetattr
150+
pysetitem
151+
pytype(::Any)
152+
pywith
153153
```
154154

155155
### Conversion to Julia
@@ -173,8 +173,8 @@ pyjl
173173
pyjlraw
174174
pyisjl
175175
pyjlvalue
176-
pytextio
177176
pybinaryio
177+
pytextio
178178
```
179179

180180
### Arithmetic
@@ -248,7 +248,7 @@ These functions can be used to create new Python classes where the functions are
248248
in Julia. You can instead use [`@pyeval`](@ref) etc. to create pure-Python classes.
249249

250250
```@docs
251-
pyclass
251+
pytype(::Any, ::Any, ::Any)
252252
pyfunc
253253
pyclassmethod
254254
pystaticmethod

docs/src/releasenotes.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Release Notes
22

33
## Unreleased
4-
* **Breaking:** Removes `pymethod`.
5-
* **Breaking:** Any member passed to `pyclass` which is a `Function` is automatically
6-
wrapped with `pyfunc`, making it an instance method.
4+
* **Breaking:** Removes `pymethod` and `pyclass`. In the future, `pyclass` may become sugar
5+
for `types.new_class` (namely you can specify a metaclass).
76
* Adds `pyfunc`, `pyclassmethod`, `pystaticmethod` and `pyproperty`.
87
* `pyconvert_add_rule` is now documented. Its semantics have changed, including the
98
separator of the first argument from `/` to `:`.

src/concrete/type.jl

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,69 +11,64 @@ export pytype
1111
1212
Create a new type. Equivalent to `type(name, bases, dict)` in Python.
1313
14-
See [`pyclass`](@ref) for a more convenient syntax.
15-
"""
16-
pytype(name, bases, dict) = pybuiltins.type(name, ispy(bases) ? bases : pytuple(bases), ispy(dict) ? dict : pydict(dict))
17-
18-
"""
19-
pyclass(name, bases=(); members...)
14+
If `bases` is not a Python object, it is converted to one using `pytuple`.
2015
21-
Construct a new Python type with the given name, bases and members.
16+
If `dict` is not a Python object, it is converted to one using `pydict`.
2217
23-
The `bases` may be a Python type or a tuple of Python types.
24-
25-
Any `members` which are Julia functions are interpreted as instance methods (equivalent to
26-
wrapping the function in [`pyfunc`](@ref)). To create class methods, static methods or
27-
properties, wrap the function in [`pyclassmethod`](@ref), [`pystaticmethod`](@ref) or
28-
[`pyproperty`](@ref).
29-
30-
Note that the arguments to any method or property are passed as `Py`, i.e. they are not
31-
converted first.
18+
In order to use a Julia `Function` as an instance method, it must be wrapped into a Python
19+
function with [`pyfunc`](@ref). Similarly, see also [`pyclassmethod`](@ref),
20+
[`pystaticmethod`](@ref) or [`pyproperty`](@ref). In all these cases, the arguments passed
21+
to the function always have type `Py`. See the example below.
3222
3323
# Example
3424
3525
```
36-
Foo = pyclass("Foo",
37-
__init__ = function (self, x, y = nothing)
38-
self.x = x
39-
self.y = y
40-
nothing
41-
end,
26+
Foo = pytype("Foo", (), [
27+
"__init__" => pyfunc(
28+
doc = \"\"\"
29+
Specify x and y to store in the Foo.
30+
31+
If omitted, y defaults to None.
32+
\"\"\",
33+
function (self, x, y = nothing)
34+
self.x = x
35+
self.y = y
36+
return
37+
end,
38+
),
4239
43-
__repr__ = function (self)
44-
"Foo(\$(self.x), \$(self.y))"
45-
end,
40+
"__repr__" => function (self)
41+
return "Foo(\$(self.x), \$(self.y))"
42+
end |> pyfunc,
4643
47-
frompair = function (cls, xy)
48-
cls(xy...)
49-
end
50-
|> pyclassmethod,
44+
"frompair" => pyclassmethod(
45+
doc = "Construct a Foo from a tuple of length two.",
46+
(cls, xy) -> cls(xy...),
47+
),
5148
52-
hello = function (name)
53-
println("Hello, \$name")
54-
end
55-
|> pystaticmethod,
49+
"hello" => pystaticmethod(
50+
doc = "Prints a friendly greeting.",
51+
(name) -> println("Hello, \$name"),
52+
)
5653
57-
xy = pyproperty(
58-
get = function (self)
59-
(self.x, self.y)
60-
end,
54+
"xy" => pyproperty(
55+
doc = "A tuple of x and y.",
56+
get = (self) -> (self.x, self.y),
6157
set = function (self, xy)
6258
(x, y) = xy
6359
self.x = x
6460
self.y = y
6561
nothing
6662
end,
6763
),
68-
)
64+
])
6965
```
7066
"""
71-
function pyclass(name, bases=(); members...)
72-
bases2 = ispy(bases) && pyistype(bases) ? pytuple((bases,)) : pytuple(bases)
73-
members2 = pydict(pystr(k) => v isa Function ? pyfunc(v) : Py(v) for (k, v) in members)
74-
pytype(name, bases2, members2)
67+
function pytype(name, bases, dict)
68+
bases2 = ispy(bases) ? bases : pytuple(bases)
69+
dict2 = ispy(dict) ? dict : pydict(dict)
70+
pybuiltins.type(name, bases2, dict2)
7571
end
76-
export pyclass
7772

7873
pyistype(x) = pytypecheckfast(x, C.Py_TPFLAGS_TYPE_SUBCLASS)
7974

src/jlwrap/callback.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,27 @@ end
7272
export pyfunc
7373

7474
"""
75-
pyclassmethod(f)
75+
pyclassmethod(f; ...)
7676
7777
Convert callable `f` to a Python class method.
7878
7979
If `f` is not a Python object (e.g. if `f` is a `Function`) then it is converted to one with
8080
[`pyfunc`](@ref). In particular this means the arguments passed to `f` are always of type
81-
`Py`.
81+
`Py`. Keyword arguments are passed to `pyfunc`.
8282
"""
83-
pyclassmethod(f) = pybuiltins.classmethod(ispy(f) ? f : pyfunc(f))
83+
pyclassmethod(f; kw...) = pybuiltins.classmethod(ispy(f) ? f : pyfunc(f; kw...))
8484
export pyclassmethod
8585

8686
"""
87-
pystaticmethod(f)
87+
pystaticmethod(f; ...)
8888
8989
Convert callable `f` to a Python static method.
9090
9191
If `f` is not a Python object (e.g. if `f` is a `Function`) then it is converted to one with
9292
[`pyfunc`](@ref). In particular this means the arguments passed to `f` are always of type
93-
`Py`.
93+
`Py`. Any keyword arguments are passed to `pyfunc`.
9494
"""
95-
pystaticmethod(f) = pybuiltins.staticmethod(ispy(f) ? f : pyfunc(f))
95+
pystaticmethod(f; kw...) = pybuiltins.staticmethod(ispy(f) ? f : pyfunc(f; kw...))
9696
export pystaticmethod
9797

9898
"""

0 commit comments

Comments
 (0)