Commit ba6cbed8 authored by da-woods's avatar da-woods Committed by GitHub

Add warning for common user mistake and fix crash relating to annotated attributes. (GH-3850)

Closes https://github.com/cython/cython/issues/3830
parent 82ca1550
...@@ -2008,7 +2008,12 @@ class NameNode(AtomicExprNode): ...@@ -2008,7 +2008,12 @@ class NameNode(AtomicExprNode):
"'%s' cannot be specialized since its type is not a fused argument to this function" % "'%s' cannot be specialized since its type is not a fused argument to this function" %
self.name) self.name)
atype = error_type 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) 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. # Even if the entry already exists, make sure we're supplying an annotation if we can.
if annotation and not entry.annotation: if annotation and not entry.annotation:
......
...@@ -262,6 +262,10 @@ def py_float_default(price : float=None, ndigits=4): ...@@ -262,6 +262,10 @@ def py_float_default(price : float=None, ndigits=4):
return price, ndigits return price, ndigits
cdef class ClassAttribute:
cls_attr : float = 1.
_WARNINGS = """ _WARNINGS = """
8:32: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly. 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. 8:47: Dicts should no longer be used as type annotations. Use 'cython.int' etc. directly.
...@@ -271,6 +275,7 @@ _WARNINGS = """ ...@@ -271,6 +275,7 @@ _WARNINGS = """
8:85: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly. 8:85: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly.
242:44: Unknown type declaration in annotation, ignoring 242:44: Unknown type declaration in annotation, ignoring
249:29: Ambiguous types 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: # BUG:
46:6: 'pytypes_cpdef' redeclared 46:6: 'pytypes_cpdef' redeclared
120:0: 'struct_io' redeclared 120:0: 'struct_io' redeclared
......
...@@ -157,6 +157,7 @@ _WARNINGS = """ ...@@ -157,6 +157,7 @@ _WARNINGS = """
38:12: Unknown type declaration in annotation, ignoring 38:12: Unknown type declaration in annotation, ignoring
39:18: Unknown type declaration in annotation, ignoring 39:18: Unknown type declaration in annotation, ignoring
57:19: 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 73:19: Unknown type declaration in annotation, ignoring
# FIXME: these are sort-of evaluated now, so the warning is misleading # FIXME: these are sort-of evaluated now, so the warning is misleading
126:21: Unknown type declaration in annotation, ignoring 126:21: Unknown type declaration in annotation, ignoring
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment