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

[BUG] Using a dataclass with positional and non-positional args causes a runtime exception #4934

Closed
Mnkras opened this issue Jul 28, 2022 · 6 comments

Comments

@Mnkras
Copy link

Mnkras commented Jul 28, 2022

Describe the bug
Execute the code below after cythonizing it and it will generate a runtime exception.
Vanilla Python has no issues

To Reproduce
Code to reproduce the behaviour:

from dataclasses import dataclass
from datetime import datetime
from typing import Optional


@dataclass
class PipelineTiming:
    initiated: datetime
    started: Optional[datetime] = None
    stopped: Optional[datetime] = None


def testing():
    return PipelineTiming(datetime.utcnow())

print(testing())
>>> import test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 16, in init test
    print(testing())
  File "test.py", line 14, in test.testing
    return PipelineTiming(datetime.utcnow())
TypeError: __init__() takes 1 positional argument but 2 were given

Expected behavior
I expect the code to run, and not crash at runtime

PipelineTiming(initiated=datetime.datetime(2022, 7, 28, 18, 14, 8, 100560), started=None, stopped=None)

Environment (please complete the following information):

  • OS: Linux and macOS
  • Python version: 3.8.4
  • Cython version: 0.29.31

Additional context
There seems to be other weirdness with dataclasses as well, I can file issues for those as well.

@da-woods
Copy link
Contributor

Cython 0.29.x doesn't support class annotations (and won't support class annotations). Therefore dataclasses will not usefully work with it.

They will work with the Cython 3 alpha branch.

Duplicate of #2552

@da-woods da-woods closed this as not planned Won't fix, can't repro, duplicate, stale Jul 28, 2022
@da-woods
Copy link
Contributor

It just doesn't generate the __annotations__ attribute so the dataclass decorator has nothing to work with. There's some useful workarounds in the linked issue, but the best advice is to use the Cython 3 alpha branch.

@Mnkras
Copy link
Author

Mnkras commented Jul 28, 2022

Is 3 considered stable enough? I would love to use these python features, but not at the risk of destabilizing our application

@da-woods
Copy link
Contributor

Considered by whom? 0.29.31 isn't a great story of not breaking things right now...

It's reasonably stable I think, but it obviously depends on what the risk is. The current 3.0a10 release has been out for a while without major issue. The next release may well break some typing related things (because it's starting to try to use those intelligently, and new features obviously come with a bit of risk)

@Mnkras
Copy link
Author

Mnkras commented Jul 28, 2022

I'm on an older cython than 0.29.31, I just tried to to make sure it wasn't fixed in the latest.

Thanks, ill give it a try, is 3 published to anaconda?

@da-woods
Copy link
Contributor

is 3 published to anaconda?

It doesn't look it it unfortunately

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

No branches or pull requests

2 participants