question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it appropriate to implement a poly model using validate func?

See original GitHub issue

sample code:

from enum import IntEnum

from pydantic import BaseModel


class FooTypeEnum(IntEnum):
    A = 1
    B = 2


class Foo(BaseModel):
    type: int

    @classmethod
    def validate(cls, value):
        type_to_model = {
            FooTypeEnum.A: FooA,
            FooTypeEnum.B: FooB,
            None: cls
        }
        _type = value.get('type', None)
        try:
            return type_to_model[_type](**value)
        except KeyError:
            raise TypeError(f'no model for type: {_type}')


class FooA(Foo):
    a: str


class FooB(Foo):
    b: str


class Bar(BaseModel):
    foo: Foo = ...


m = Bar(foo={'type': FooTypeEnum.A, 'a': 'aaa'})
print(m)
# Bar foo=<FooA type=1 a='aaa'>
print(m.dict())
# {'foo': {'type': 1, 'a': 'aaa'}}
m = Bar(foo={'type': FooTypeEnum.B, 'b': 'bbb'})
print(m)
# Bar foo=<FooB type=2 b='bbb'>
print(m.dict())
# {'foo': {'type': 2, 'b': 'bbb'}}

# m = Bar(foo={'type': FooTypeEnum.B, 'c': 'bbb'})
# error ...
# m = Bar(foo={'type': 3, 'c': 'bbb'})
# error ...
# m = Bar(foo={'c': 'bbb'})
# error ...

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
samuelcolvincommented, Mar 2, 2018

no problem, glad it helped.

1reaction
samuelcolvincommented, Mar 2, 2018

Looks like you just need to use Union:

from typing import Union
from enum import IntEnum

from pydantic import BaseModel


class FooTypeEnum(IntEnum):
    A = 1
    B = 2


class Foo(BaseModel):
    type: FooTypeEnum


class FooA(Foo):
    a: str


class FooB(Foo):
    b: str


class Bar(BaseModel):
    foo: Union[FooA, FooB]


m = Bar(foo={'type': FooTypeEnum.A, 'a': 'aaa'})
print(m)
# Bar foo=<FooA type=1 a='aaa'>
print(m.dict())
# {'foo': {'type': 1, 'a': 'aaa'}}
m = Bar(foo={'type': FooTypeEnum.B, 'b': 'bbb'})
print(m)
# Bar foo=<FooB type=2 b='bbb'>
print(m.dict())
# {'foo': {'type': 2, 'b': 'bbb'}}

# m = Bar(foo={'type': FooTypeEnum.B, 'c': 'bbb'})
# error ...
# m = Bar(foo={'type': 3, 'c': 'bbb'})
# error ...
# m = Bar(foo={'c': 'bbb'})
# error ...

Pydantic tries each of the different types in Union to see if they’re valid and returns the first which is.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it appropriate to implement a poly model using validate func?
A: FooA, FooTypeEnum.B: FooB, None: cls } _type = value.get('type', None) try: return type_to_model[_type](**value) except KeyError: raise ...
Read more >
Lab 12 - Polynomial Regression and Step Functions in R
We first fit the polynomial regression model using the following command: ... the appropriate response vector, and then apply the glm() function using...
Read more >
Python | Implementation of Polynomial Regression
Polynomial Regression is a form of linear regression in which the relationship between the independent variable x and dependent variable y ...
Read more >
Training-validation-test split and cross-validation done right
In this tutorial, you will discover the correct procedure to use cross validation and a dataset to select the best models for a...
Read more >
Lab 2 - andrew.cmu.ed
Describe what the sample() function as used above actually does. ... (e) Use the poly() command to fit a quadratic regression model of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found