summaryrefslogtreecommitdiff
path: root/docs/examples/userguide/extension_types/dataclass.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/userguide/extension_types/dataclass.pyx')
-rw-r--r--docs/examples/userguide/extension_types/dataclass.pyx21
1 files changed, 21 insertions, 0 deletions
diff --git a/docs/examples/userguide/extension_types/dataclass.pyx b/docs/examples/userguide/extension_types/dataclass.pyx
new file mode 100644
index 000000000..b03d5f7b1
--- /dev/null
+++ b/docs/examples/userguide/extension_types/dataclass.pyx
@@ -0,0 +1,21 @@
+cimport cython
+try:
+ import typing
+ import dataclasses
+except ImportError:
+ pass # The modules don't actually have to exists for Cython to use them as annotations
+
+
+@cython.dataclasses.dataclass
+cdef class MyDataclass:
+ # fields can be declared using annotations
+ a: cython.int = 0
+ b: double = cython.dataclasses.field(default_factory = lambda: 10, repr=False)
+
+ # fields can also be declared using `cdef`:
+ cdef str c
+ c = "hello" # assignment of default value on a separate line
+
+ # typing.InitVar and typing.ClassVar also work
+ d: dataclasses.InitVar[cython.double] = 5
+ e: typing.ClassVar[list] = []