- 
                Notifications
    
You must be signed in to change notification settings  - Fork 312
 
          Fix: dataclass InitVars shouldn't be required on serialization
          #1602
        
          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
Changes from all commits
022e730
              2d0b850
              327c60a
              109749a
              2d73a66
              3a027b5
              369bed3
              42a98c1
              e2f256f
              9ab1140
              825fb9e
              5cc59c2
              14b6931
              3e82624
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -192,3 +192,46 @@ class Model: | |
| m = v.validate_python({'extra': 'extra'}) | ||
| 
     | 
||
| assert s.to_python(m) == {'extra': 'extra bam!'} | ||
| 
     | 
||
| 
     | 
||
| def test_dataclass_initvar_not_required_on_union_ser() -> None: | ||
| @dataclasses.dataclass | ||
| class Foo: | ||
| x: int | ||
| init_var: dataclasses.InitVar[int] = 1 | ||
| 
     | 
||
| @dataclasses.dataclass | ||
| class Bar: | ||
| x: int | ||
| 
     | 
||
| schema = core_schema.union_schema( | ||
| [ | ||
| core_schema.dataclass_schema( | ||
| Foo, | ||
| core_schema.dataclass_args_schema( | ||
| 'Foo', | ||
| [ | ||
| core_schema.dataclass_field(name='x', schema=core_schema.int_schema()), | ||
| core_schema.dataclass_field( | ||
| name='init_var', | ||
| init_only=True, | ||
| schema=core_schema.with_default_schema(core_schema.int_schema(), default=1), | ||
| ), | ||
| ], | ||
| ), | ||
| ['x'], | ||
| post_init=True, | ||
| ), | ||
| core_schema.dataclass_schema( | ||
| Bar, | ||
| core_schema.dataclass_args_schema( | ||
| 'Bar', [core_schema.dataclass_field(name='x', schema=core_schema.int_schema())] | ||
| ), | ||
| ['x'], | ||
| ), | ||
| ] | ||
| ) | ||
| 
     | 
||
| s = SchemaSerializer(schema) | ||
| assert s.to_python(Foo(x=1), warnings='error') == {'x': 1} | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please can you add a test with just the  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and can you add a test for  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 Not quite sure what you mean here - I wasn't able to repro the issue without the union. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still it's still worth adding tests for the simplest cases.  | 
||
| assert s.to_python(Foo(x=1, init_var=2), warnings='error') == {'x': 1} | ||
Uh oh!
There was an error while loading. Please reload this page.