question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Classes lack __annotations__ (PEP-526)

See original GitHub issue

With python 3.7 dataclasses were introduced. They work just fine in Cython except for their standard init function.

How to reproduce: test.pyx

from dataclasses import dataclass

@dataclass
class TestClass:
	x: int
	y: int
	some_string: str

This file compiles to Cython code just fine. However, when ran from the python it raises an error: test2.py

import test

test_class = test.TestClass(1, 2, 'string')

This raises the following:

Traceback (most recent call last):
  File ".\test2.py", line 3, in <module>
    test_class = test.TestClass(1, 2, 'string')
TypeError: __init__() takes 1 positional argument but 4 were given

if you rename test.pyx to test.py and do not compile it, no such error occurs.

my current workaround: setting each property after initialization separately: test.pyx remains unchanged. test.py

import test

test_class = test.TestClass()
test_class.x = 1
test_class.y = 2
test_class.some_string = 'string'

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:18
  • Comments:24 (16 by maintainers)

github_iconTop GitHub Comments

9reactions
GliderGeekcommented, Sep 29, 2020

awesome news that this is implemented 🎉 thanks a lot

5reactions
GliderGeekcommented, Apr 30, 2020

thanks for the quick reply. i will try to get some proper time to work on this. in the meantime i have tried a little workaround:

class Annotated:
    x: int
    y: int 

    __annotations__ = {'x': int, 'y': int}

this seems to work properly. is this what you meant by "get away with building the dict in Python and adding it to the class dict "?

Read more comments on GitHub >

github_iconTop Results From Across the Web

PEP 526 – Syntax for Variable Annotations
This PEP aims at adding syntax to Python for annotating the types of variables (including class variables and instance variables), instead of expressing ......
Read more >
[Python-Dev] Please reject or postpone PEP 526
Hi everyone, I think we should reject, or at least postpone PEP 526. PEP 526 represents a major change to the language, however...
Read more >
What are variable annotations? - python
Taken from PEP 526 -- Syntax for Variable Annotations, Python will remain a dynamically typed language, and the authors have no desire to...
Read more >
PEP-526 ready for review: Syntax for Variable and Attribute ...
Actually, the main benefit that I personally think variable annotations can bring isn't actually articulated very well in the PEP, imo -- I ......
Read more >
Dataclasses without type annotations - death and gravity
PEP 526 : (my definition) Titled "Syntax for Variable Annotations", ... @dataclass class Hell: one: "starting with Python 3.10", two: it(s=not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found