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

Forward class creation keyword arguments #2627

Closed
wants to merge 1 commit into from

Conversation

smac89
Copy link

@smac89 smac89 commented Oct 10, 2022

https://docs.python.org/3/library/functions.html#type

I wanted to be able to create a template model which other models inherit from and modify, by passing keyword arguments in the class definition. However, the current implementation of ModelBase does not pass along those arguments which results in errors like

TypeError: ModelBase.__new__() got an unexpected keyword argument 'custom_arg'

Example code:

Base class

class Foo(pw.Model):
    foo_type: pw.CharField

    @classmethod
    def __init_subclass__(
        cls, foo_type_default: str
    ) -> None:
        cls.foo_type = pw.CharField(
            choices=(
                ("bar", "Bar"),
                ("foobar", "Foobar"),
            ),
            default=foo_type_default,
        )

Subclass1

class Bar(Foo, foo_type_default="bar"):
    pass

Subclass2

class FooBar(Foo, foo_type_default="foobar"):
    pass

With the current implementation, the above doesn't work. This PR fixes that.

coleifer added a commit that referenced this pull request Oct 10, 2022
@coleifer
Copy link
Owner

Sure, I've merged an equivalent patch w/just the 79 char line limit.

@coleifer coleifer closed this Oct 10, 2022
@smac89 smac89 deleted the patch-1 branch October 11, 2022 00:34
@smac89
Copy link
Author

smac89 commented Oct 11, 2022

That was easy. Thanks!

When do you think a new release will be available?

@coleifer
Copy link
Owner

There's hardly any fixes, I think it may need a bit of time.

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

Successfully merging this pull request may close these issues.

None yet

2 participants