Skip to content

"Missing required positional argument" for default field #81

Closed

Description

@fortierq

Hello,

In the following code, I get: TypeError: init() missing 1 required positional argument: 'a'

from pyfields import autoclass, field

@autoclass
class A:
    a: int = field(default = 1)

@autoclass
class B(A):
    b = field()
    
B(b=3)

It works when I add a type hint to b (b : int = field()).
Is it a bug or a feature?
pyfields version: 1.6.0

Activity

  1. smarie commented on Nov 22, 2020

    @smarie
    Owner

    Thanks @fortierq for opening this ! Indeed I am able to reproduce the issue.
    It seems that the issue comes from the fact that I misunderstood how B.__annotations__ works: apparently when B does not have any annotations of its own, this is set to th annotations of the parent(s). So it contains {'a': int}, For this reason autofields does not work correctly, as it does not assume inherited annotations: it recreates a definition for a in B, overriding the inherited one. When you add the type hint in B, then B.__annotations__ does not contain the annotations from A anymore, this is why it works. Tricky !

    I need to investigate how to fix this, the key would be to find a reliable way to know when there are new annotations in a class, or if they are just the inherited ones. If you have an idea do not hesitate to suggest it here ! Note that it should support multiple inheritance with parent classes in any order, having annotations or not...

  2. fortierq commented on Nov 23, 2020

    @fortierq
    Author

    Thanks for your answer.
    Not sure I understand the issue, but I am definitely interested by looking at the code when I find the time.
    I like the flexibility of the default_factory option, which seems missing from dataclasses.

  3. smarie commented on Nov 23, 2020

    @smarie
    Owner

    Not sure I understand the issue, but I am definitely interested by looking at the code when I find the time.

    Well the issue is that in python, cls.__annotations__ is the official way to get the type annotations from a class, but apparently when the class does not define any, you get the annotations from one of the class parents. That was not the intuitive behaviour I was expecting from python, so my code was always assuming that when it finds some field in the annotations, it was something overridden in the class. So my code was finding that a was overriden by B.

    Anyway I found a good way to do the check I was talking about here so that should now be easy to fix, I'll do it in the upcoming days. Thanks again for spotting this issue !

    Note for reference: the bug was also happening with the shortcut autofields notation:

    from pyfields import autoclass, field
    
    @autoclass
    class A:
        a: int = 1
    
    @autoclass
    class B(A):
        b = field()
        
    B(b=3)
  4. smarie commented on Nov 24, 2020

    @smarie
    Owner

    This should be fixed in 1.6.1, that will be available in a few minutes. Thanks again @fortierq !

  5. fortierq commented on Nov 24, 2020

    @fortierq
    Author

    Great! Thanks for your work.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions