Repository: cython/cython

Issue:
[BUG] Compiler crash when adding @dataclass to extension types with public/readonly object fields
<!--
**PLEASE READ THIS FIRST:**
- DO NOT use the bug and feature tracker for general questions and support requests.
  Use the `cython-users` mailing list instead.
  It has a wider audience, so you get more and better answers.
- 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**

When adding `@dataclass` to extension types with public/readonly `object` fields, the compiler will crash with the following:

```
Traceback (most recent call last):
  File "/home/.local/bin/cython", line 33, in <module>
    sys.exit(load_entry_point('Cython==3.0.0a10', 'console_scripts', 'cython')())
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Main.py", line 713, in setuptools_main
    return main(command_line = 1)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Main.py", line 731, in main
    result = compile(sources, options)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Main.py", line 629, in compile
    return compile_multiple(source, options)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Main.py", line 606, in compile_multiple
    result = run_pipeline(source, options, context=context)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Main.py", line 504, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Pipeline.py", line 394, in run_pipeline
    data = run(phase, data)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Pipeline.py", line 371, in run
    return phase(data)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Pipeline.py", line 52, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/ModuleNode.py", line 206, in process_implementation
    self.generate_c_code(env, options, result)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/ModuleNode.py", line 518, in generate_c_code
    self.generate_module_init_func(modules[:-1], env, globalstate['init_module'])
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/ModuleNode.py", line 3101, in generate_module_init_func
    self.body.generate_execution_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 409, in generate_execution_code
    stat.generate_execution_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 409, in generate_execution_code
    stat.generate_execution_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 355, in generate_execution_code
    self.body.generate_execution_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 409, in generate_execution_code
    stat.generate_execution_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 5340, in generate_execution_code
    self.body.generate_execution_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 409, in generate_execution_code
    stat.generate_execution_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 355, in generate_execution_code
    self.body.generate_execution_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 409, in generate_execution_code
    stat.generate_execution_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 5773, in generate_execution_code
    self.generate_rhs_evaluation_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 6072, in generate_rhs_evaluation_code
    self.rhs.generate_evaluation_code(code)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/ExprNodes.py", line 1806, in generate_evaluation_code
    self.result_code = code.get_py_string_const(
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Code.py", line 1948, in get_py_string_const
    return self.globalstate.get_py_string_const(
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Code.py", line 1389, in get_py_string_const
    c_string = self.get_string_const(text)
  File "/home/.local/lib/python3.10/site-packages/Cython/Compiler/Code.py", line 1360, in get_string_const
    if text.is_unicode:
AttributeError: 'str' object has no attribute 'is_unicode'

```
**To Reproduce**

This compiles and works fine:

```cython
from cython.dataclasses cimport dataclass

@dataclass
cdef class Foo:
    cdef public int bar
```

Also works with other common types like `str`, `float`, `tuple`.

This does not compile:

```cython
from cython.dataclasses cimport dataclass

@dataclass
cdef class Foo:
    cdef public object bar
```
Also does not work with `bint`

**Environment (please complete the following information):**
 - OS: Linux
 - Python version: 3.10.3
 - Cython version: [c23195ddc3bf4c139e85439c06de9b87c7bc752c](https://github.com/cython/cython/tree/c23195ddc3bf4c139e85439c06de9b87c7bc752c)



Discussion:

Resolving patch:
diff --git a/Cython/Compiler/Dataclass.py b/Cython/Compiler/Dataclass.py
--- a/Cython/Compiler/Dataclass.py
+++ b/Cython/Compiler/Dataclass.py
@@ -590,7 +590,7 @@ def get_field_type(pos, entry):
         # try to return PyType_Type. This case should only happen with
         # attributes defined with cdef so Cython is free to make it's own
         # decision
-        s = entry.type.declaration_code("", for_display=1)
+        s = EncodedString(entry.type.declaration_code("", for_display=1))
         return ExprNodes.StringNode(pos, value=s)
 
 


Test patch:
diff --git a/tests/run/cdef_class_dataclass.pyx b/tests/run/cdef_class_dataclass.pyx
--- a/tests/run/cdef_class_dataclass.pyx
+++ b/tests/run/cdef_class_dataclass.pyx
@@ -197,12 +197,20 @@ cdef class TestVisibility:
     'double'
     >>> hasattr(inst, "c")
     True
+    >>> "d" in TestVisibility.__dataclass_fields__
+    True
+    >>> TestVisibility.__dataclass_fields__["d"].type
+    'object'
+    >>> hasattr(inst, "d")
+    True
     """
     cdef double a
     a = 1.0
     b: double = 2.0
     cdef public double c
     c = 3.0
+    cdef public object d
+    d = object()
 
 @dataclass(frozen=True)
 cdef class TestFrozen: