Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataclasses do not work with Cython due to annotations being stripped out. #3336

Open
nekokatt opened this issue Jan 29, 2020 · 1 comment
Open

Comments

@nekokatt
Copy link

@nekokatt nekokatt commented Jan 29, 2020

It would appear that dataclasses now do not work since 3dc2b9d was implemented.

Using the most recent master commit at the time of writing:

# test.py
import dataclasses

@dataclasses.dataclass()
class Foo:
    bar: str = dataclasses.field(default="hello")

print(Foo("hi"))
print(Foo())
>>> cythonize test.py -3i
Compiling /home/.../code/cython-bug/test.py because it changed.
[1/1] Cythonizing /home/.../code/cython-bug/test.py
running build_ext
building 'test' extension
creating /home/.../code/cython-bug/tmpbh4j4lqe/home
creating /home/.../code/cython-bug/tmpbh4j4lqe/home/
creating /home/.../code/cython-bug/tmpbh4j4lqe/home//code
creating /home/.../code/cython-bug/tmpbh4j4lqe/home///code/cython-bug
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fPIC -I/home/.../code/hikari/.venv/include -I/usr/include/python3.8 -c /home/.../code/cython-bug/test.c -o /home/.../code/cython-bug/tmpbh4j4lqe/home/.../code/cython-bug/test.o
gcc -pthread -shared -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now /home/.../code/cython-bug/tmpbh4j4lqe/home/.../code/cython-bug/test.o -L/usr/lib -o /home/.../code/cython-bug/test.cpython-38-x86_64-linux-gnu.so

>>> python -c "import test"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 3, in init test
    @dataclasses.dataclass()
  File "/usr/lib/python3.8/dataclasses.py", line 1011, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
  File "/usr/lib/python3.8/dataclasses.py", line 885, in _process_class
    raise TypeError(f'{name!r} is a field but has no type annotation')
TypeError: 'bar' is a field but has no type annotation

Version info:

>>> python -V
Python 3.8.1
>>> pip freeze | grep -i cython
Cython==3.0a0
@scoder

This comment has been minimized.

Copy link
Contributor

@scoder scoder commented Jan 29, 2020

Isn't this just #2552? Did this ever work for you before? What does Foo.__annotations__ give?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.