Repository: cython/cython

Issue:
[BUG] Getting Internal Compiler Errors
I am unsure if this is the right place to ask but I am running into an error that doesn't tell me where it happened, just the file it happened in. It appears to be the python end of the compiler but I don't know if that is correct. 
The command used to compile the files: `cythonize *.pyx --inplace -3`
Here's the error: 
```Compiling C:\My files\Programming\Python\Games\Game 3\lib\front__lasers.pyx because it changed.
[1/1] Cythonizing C:\My files\Programming\Python\Games\Game 3\lib\front__lasers.pyx
Traceback (most recent call last):
  File "c:\users\user\appdata\local\programs\python\python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\user\appdata\local\programs\python\python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\Scripts\cythonize.exe\__main__.py", line 7, in <module>
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Build\Cythonize.py", line 223, in main
    cython_compile(path, options)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Build\Cythonize.py", line 97, in cython_compile
    ext_modules = cythonize(
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Build\Dependencies.py", line 1102, in cythonize
    cythonize_one(*args)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Build\Dependencies.py", line 1208, in cythonize_one
    result = compile_single(pyx_file, options, full_module_name=full_module_name)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Main.py", line 727, in compile_single
    return run_pipeline(source, options, full_module_name)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Main.py", line 515, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Pipeline.py", line 355, in run_pipeline
    data = run(phase, data)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Pipeline.py", line 335, in run
    return phase(data)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Pipeline.py", line 52, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\ModuleNode.py", line 143, in process_implementation
    self.generate_c_code(env, options, result)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\ModuleNode.py", line 398, in generate_c_code
    self.generate_module_init_func(modules[:-1], env, globalstate['init_module'])
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\ModuleNode.py", line 2474, in generate_module_init_func
    self.body.generate_execution_code(code)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Nodes.py", line 448, in generate_execution_code
    stat.generate_execution_code(code)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Nodes.py", line 448, in generate_execution_code
    stat.generate_execution_code(code)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Nodes.py", line 4860, in generate_execution_code
    self.body.generate_execution_code(code)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Nodes.py", line 448, in generate_execution_code
    stat.generate_execution_code(code)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Nodes.py", line 5186, in generate_execution_code
    self.generate_assignment_code(code)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\Nodes.py", line 5483, in generate_assignment_code
    self.lhs.generate_assignment_code(self.rhs, code)
  File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\Cython\Compiler\ExprNodes.py", line 2292, in generate_assignment_code
    assert entry.type.is_pyobject, "Python global or builtin not a Python object"
AssertionError: Python global or builtin not a Python object




Discussion:
This is a bug in the sense that it gives internal compiler errors rather that a useful message telling you what's wrong. However, there's definitely little we can tell from the details you've given. You could try:

1. The standard debugging technique of progressively removing code from your source file until you find out what bit's causing it.
2. Upgrade Cython to the 3.0 alpha branch (I think error reporting has been improved a bit, or the underlying issue may have been fixed).
3. If you're prepared to get into the Cython code then add ` + str(self.pos)` to the assertion line and that should given you more information. You may need to recompile.
Where did you get `self.pos` from? Many of the classes in this file have an attribute of `pos` and I don't think the error message mentioned anything about it.
Also I modified the cython compiler's assertion statement and have found what object was causing the error. A class variable was set to None by a parent class and is later reassigned as 0.5 by a subclass. I had normal python type hints for these and removing them stopped this from occuring. I'm not sure why that would cause a problem.
Regardless, thanks for your help.
The bug of showing this type of error still stands so I won't close the issue myself.
> Where did you get `self.pos` from? Many of the classes in this file have an attribute of `pos` and I don't think the error message mentioned anything about it.

All the nodes store a `pos` attribute to represent where in the source code they come from. It's just an internal detail used for error reporting.

>  The bug of showing this type of error still stands so I won't close the issue myself.

Could you show the chunk of code that produces the error?
> Could you show the chunk of code that produces the error?

```
cdef class A:
    COLOR:colors.RGBType = None # For whatever reason this is considered ok, but not the one directly below. Perhaps because this type that it is hinting to is a not a cython primitive?
    DENSITY:float = None # Here is what used to be the culprit, the type hinting. Somehow.
    SPREAD = None
    # rest of the class goes here

cdef class B(A):
    COLOR:colors.RGBType = colors.blue
    DENSITY:float = 1
    SPREAD = 0
    # rest of the class goes here

cdef class C(B):
    SPREAD = 10
    # rest of the class goes here

cdef class B2(A):
    COLOR:colors.RGBType = colors.red
    DENSITY:float = 0.5
    SPREAD = 10
    # rest of the class goes here

cdef class C2(B2):
    SPREAD = 50
    # rest of the class goes here
```
Sorry I'm not sure if these mock up class names are easy to read. I'll edit this comment if you like.
Thanks.

For what it's worth I can reproduce the error with:

```
cdef class A:
    DENSITY:float = 0.5
```

but not with `Density:float = None` which just gives me `Cannot assign None to double` (as expected). I think the issue is type-hinted variables like this become instance variables rather than class variables for a `cdef class`, so it doesn't make sense to assign anything to them.

But the error message should definitely be better
Yea it makes a lot more sense now, but as you said the error message should definitely be better. Thanks for your help.

Resolving patch:
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -2008,7 +2008,12 @@ def declare_from_annotation(self, env, as_target=False):
                           "'%s' cannot be specialized since its type is not a fused argument to this function" %
                           self.name)
                     atype = error_type
-
+            if as_target and env.is_c_class_scope and not (atype.is_pyobject or atype.is_error):
+                # TODO: this will need revising slightly if either cdef dataclasses or
+                # annotated cdef attributes are implemented
+                atype = py_object_type
+                warning(annotation.pos, "Annotation ignored since class-level attributes must be Python objects. "
+                        "Were you trying to set up an instance attribute?", 2)
             entry = self.entry = env.declare_var(name, atype, self.pos, is_cdef=not as_target)
         # Even if the entry already exists, make sure we're supplying an annotation if we can.
         if annotation and not entry.annotation:


Test patch:
diff --git a/tests/run/annotation_typing.pyx b/tests/run/annotation_typing.pyx
--- a/tests/run/annotation_typing.pyx
+++ b/tests/run/annotation_typing.pyx
@@ -262,6 +262,10 @@ def py_float_default(price : float=None, ndigits=4):
     return price, ndigits
 
 
+cdef class ClassAttribute:
+    cls_attr : float = 1.
+
+
 _WARNINGS = """
 8:32: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly.
 8:47: Dicts should no longer be used as type annotations. Use 'cython.int' etc. directly.
@@ -271,6 +275,7 @@ _WARNINGS = """
 8:85: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly.
 242:44: Unknown type declaration in annotation, ignoring
 249:29: Ambiguous types in annotation, ignoring
+266:15: Annotation ignored since class-level attributes must be Python objects. Were you trying to set up an instance attribute?
 # BUG:
 46:6: 'pytypes_cpdef' redeclared
 120:0: 'struct_io' redeclared
diff --git a/tests/run/pep526_variable_annotations.py b/tests/run/pep526_variable_annotations.py
--- a/tests/run/pep526_variable_annotations.py
+++ b/tests/run/pep526_variable_annotations.py
@@ -157,6 +157,7 @@ def iter_declared_dict_arg(d : Dict[float, float]):
 38:12: Unknown type declaration in annotation, ignoring
 39:18: Unknown type declaration in annotation, ignoring
 57:19: Unknown type declaration in annotation, ignoring
+73:11: Annotation ignored since class-level attributes must be Python objects. Were you trying to set up an instance attribute?
 73:19: Unknown type declaration in annotation, ignoring
 # FIXME: these are sort-of evaluated now, so the warning is misleading
 126:21: Unknown type declaration in annotation, ignoring