Repository: cython/cython

Issue:
[BUG] Annotated attributes of cclass are not supporting pointers
<!--
**PLEASE READ THIS FIRST:**
- Do not use the bug and feature tracker for support requests. Use the `cython-users` mailing list instead.
- Did you search for similar issues already? Please do, it helps to save us precious time that we otherwise could not invest into development.
- Did you try the latest master branch or pre-release? It might already have what you want to report. Also see the [Changelog](https://github.com/cython/cython/blob/master/CHANGES.rst) regarding recent changes.
-->

**Describe the bug**
The compilation is failing, when attribute of cclass is declared using annotated type containing pointer.

**To Reproduce**
Following code:
```python
import cython

@cython.cclass
class Foo:
    a: cython.pointer(cython.int)

    def bar(self):
        self.a = cython.NULL
```
fails during compilation with error:
```
$ cython -3 test.py

Error compiling Cython file:
------------------------------------------------------------
...
@cython.cclass
class Foo:
    a: cython.pointer(cython.int)

    def bar(self):
        self.a = cython.NULL
                      ^
------------------------------------------------------------

test.py:8:23: Cannot convert 'void *' to Python object
```

**Expected behavior**
Compilation should be successfull.

**Environment (please complete the following information):**
 - OS: Linux
 - Python version: Python 3.9.2
 - Cython version: master

**Additional context**
When `declare()` statement or `cython.p_int` type is used, compilation is successful:

```python
import cython

@cython.cclass
class Foo:
    a = cython.declare(cython.pointer(cython.int))

    def bar(self):
        self.a = cython.NULL
```

```python
import cython

@cython.cclass
class Foo:
    a: cython.p_int

    def bar(self):
        self.a = cython.NULL
```



Discussion:
This is a know issue and something that should work with the dataclasses PR #3400.
https://github.com/cython/cython/blob/9f78ed075f7371f931c7fa76b45589841c25668f/Cython/Compiler/ExprNodes.py#L2037-L2038
I don't think this is fixed by #3400? I get the same issue on the dataclass branch (and the same issue again if I mark it as a Cython dataclass, just to check that this wasn't making a difference).

If I remove the function with the NULL assignment then it compiles to

```
struct __pyx_obj_9ptr_annot_Foo {
  PyObject_HEAD
  PyObject *a;
};
```

(i.e. not an `int*` pointer).

So I don't think there's anything I can cherry-pick for a quick solution to this...
The warning:

```
 warning(annotation.pos, "Annotation ignored since class-level attributes must be Python objects. " 
         "Were you trying to set up an instance attribute?", 2) 
```

Is catching things like:

```
cdef class X:
    x : cython.int = 5
```

which are treated differently in regular classes and dataclasses. But it's the assignment at the point of definition that's relevant here.
This bug can be closed since it seems working on master branch.
```
(cython39) matus@MacBook-Pro:~/dev/cython/test$ cat test.py
import cython

@cython.cclass
class Foo:
    a: cython.pointer(cython.int)

    def bar(self):
        self.a = cython.NULL
(cython39) matus@MacBook-Pro:~/dev/cython/test$ cython -3 test.py
(cython39) matus@MacBook-Pro:~/dev/cython/test$
```

Resolving patch:
diff --git a/docs/examples/tutorial/clibraries/queue.py b/docs/examples/tutorial/clibraries/queue.py
--- a/docs/examples/tutorial/clibraries/queue.py
+++ b/docs/examples/tutorial/clibraries/queue.py
@@ -2,7 +2,7 @@
 
 @cython.cclass
 class Queue:
-    _c_queue = cython.declare(cython.pointer(cqueue.Queue))
+    _c_queue: cython.pointer(cqueue.Queue)
 
     def __cinit__(self):
         self._c_queue = cqueue.queue_new()


Test patch: