s-knibbs / dataclasses-jsonschema

JSON schema generation from dataclasses
MIT License
166 stars 38 forks source link

JsonSchemaMixin not working with multiple inheritance. #166

Closed Robospecta closed 2 years ago

Robospecta commented 3 years ago

Hi,

I'm hoping you can tell me whether I'm doing something wrong here when trying to use the JsonSchemaMixin.

I have the following MeasuredTide class which inherits from JsonSchemaMixin and Record. It gets passed a coerce keyword argument intended for the Record subclass.

@dataclasses.dataclass()
class MeasuredTide(JsonSchemaMixin, Record, coerce=True):
    height_m: float
    residual_m: float
    astro_m: float
    validTime_Utc: datetime

The following is what would be the resultant MRO.

0:<class '__main__.MeasuredTide'>
1:<class 'dataclasses_jsonschema.JsonSchemaMixin'>
2:<class 'faust.models.record.Record'>
3:<class 'faust.models.base.Model'>
4:<class 'faust.types.models.ModelT'>
5:<class 'object'>

When running the script containing the MeasuredTide class definition I get the following error. image

If my understanding is correct I believe this is because the JsonSchemaMixin.__init_subclass__ method doesn't currently accept **kwargs as a parameter and pass these through to the next class in the MRO using super.

e.g. super.__init_subclass__(**kwargs)

Implementing this would allow the Record subclass to receive the keyword arguments it expects. I believe this behaviour would also fit the general behaviour of a mixin and what I see documented elsewhere in my reading.

I'm open to hearing any mistakes or drawbacks with the bug fix I'm suggesting. Also happy to hear alternative approaches, keeping in mind I don't maintain the JsonSchemaMixin or Record class. I'm also happy to submit a pull request if helpful.

Robospecta commented 2 years ago

Thanks @s-knibbs for getting to this. Much appreciated. 👍