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

Cythonized data classes allow omission of required arguments. #3776

Closed
kollmats opened this issue Aug 12, 2020 · 1 comment
Closed

Cythonized data classes allow omission of required arguments. #3776

kollmats opened this issue Aug 12, 2020 · 1 comment

Comments

@kollmats
Copy link

Describe the bug

When instantiating a data class, it usually detects if a required argument is missing and raises an error. In Cython, nothing happens until you try to access the missing field and an AttributeError is thrown:

Traceback (most recent call last):
File "", line 1, in
File "main.py", line 24, in init main
AttributeError: 'Bb' object has no attribute 'a1'

To Reproduce

from dataclasses import dataclass

@dataclass                                                                                           
class Aa:                                                                                            
    a1: str                                                                                          
    a2: str = '100'                                                                                  
    a3: str = None                                                                                   
                                                                                                      
class Bb(Aa):                                                                                        
    b1: str = '123'                                                                                  
    b2: str = None                                                                                   
                                                                                                      
bb = Bb()
print(bb.a1)

Expected behavior
Without Cython, the above yields the following traceback when calling the constructor:

Traceback (most recent call last):
File "main.py", line 23, in
bb = Bb()
TypeError: init() missing 1 required positional argument: 'a1'

Environment (please complete the following information):

OS: Ubuntu 
Version: 18.0.4

Additional context
Python 3.6.9, Cython 0.29.21, dataclasses 0.7 (backport)

@scoder
Copy link
Contributor

scoder commented Aug 12, 2020

Duplicate of #2552.

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