9

New to python and pydantic, I come from a typescript background. I was wondering if you can inherit a generic class?

In typescript the code would be as follows

interface GenericInterface<T> {
  value: T
}

interface ExtendsGeneric<T> extends GenericInterface<T> {
  // inherit value from GenericInterface
  otherValue: string
}

const thing: ExtendsGeneric<Number> = {
  value: 1,
  otherValue: 'string'
}

What I have been trying is something along the lines of

#python3.9
from pydantic.generics import GenericModel
from typing import TypeVar
from typing import Generic

T = TypeVar("T", int, str)

class GenericField(GenericModel, Generic[T]):
    value: T

class ExtendsGenericField(GenericField[T]):
    otherValue: str

ExtendsGenericField[int](value=1, otherValue="other value")

And I get the error of TypeError: Too many parameters for ExtendsGenericField; actual 1, expected 0. This sort of checks out because in the Pydantic docs it explicitly states "In order to declare a generic model...Use the TypeVar instances as annotations where you will want to replace them..." The easy workaround is to make ExtendsGeneric inherit from GenericModel and have value in its own class definition, but I was trying to reuse classes.

Is inheriting a value from a generic class possible?

1
  • You need to give one agrument as python dictionary ExtendsGenericField[int]({value:1, otherValue:"other value"}) Commented May 23, 2023 at 5:54

3 Answers 3

15

Generics are a little weird in Python, and the problem is that ExtendsGenericField itself isn't declared as generic. To solve, just add Generic[T] as a super class of ExtendsGenericField:

from pydantic.generics import GenericModel
from typing import TypeVar
from typing import Generic

T = TypeVar("T", int, str)

class GenericField(GenericModel, Generic[T]):
    value: T

class ExtendsGenericField(GenericField[T], Generic[T]):
    otherValue: str

ExtendsGenericField[int](value=1, otherValue="other value")
Sign up to request clarification or add additional context in comments.

1 Comment

Quick fix! I figured I was missing something silly like that. Thank you
2

Since Pydantic v2 (at least with v2.6.x), the following works (reference):

# at least with Python 3.9
from typing import Generic, TypeVar
from pydantic import BaseModel

T = TypeVar("T")

class BaseClass(BaseModel, Generic[T]):
    value: T

class ChildClass(BaseClass[T], Generic[T]):
    other_value: str


x = ChildClass[int](value=1, other_value="other value")

type(x)
# __main__.ChildClass[int]

Comments

1

Not sure but referring yours my code worked :)

from datetime import datetime
from pydantic import BaseModel, parse_obj_as
from enum import Enum
import json
from typing import TypeVar, Generic
from pydantic.generics import GenericModel

class TradeOverlapping(BaseModel):
    COBDate: str
    DBServer: str

class EventType(Enum):
    STH_READY= 'STH_READY'

T = TypeVar("T")
class KafkaEvent(GenericModel,Generic[T]):
    Source: str
    Version: str
    TimeStamp: datetime
    Event: EventType
    Data: T

p = KafkaEvent[TradeOverlapping].parse_obj({"Source":"TOPIC","Version":"1.0","TimeStamp":"2022-08-03 06:49:17","Event":EventType('STH_READY'),"Data":TradeOverlapping(COBDate="2023-04-05", DBServer="DB_SERVER")})
print("Producer:")
print(p.json())

print("----------------------------")

value = '{"Source":"TOPIC","Version":"1.0","TimeStamp":"2022-08-03 06:49:17","Event":"STH_READY","Data":{"COBDate":"2023-04-05","DBServer":"DB_SERVER"}}'
obj = parse_obj_as(KafkaEvent[TradeOverlapping], json.loads(value))
print("Consumer:")
print(obj)

1 Comment

Output: Producer: {"Source": "TOPIC", "Version": "1.0", "TimeStamp": "2022-08-03T06:49:17", "Event": "STH_READY", "Data": {"COBDate": "2023-04-05", "DBServer": "DB_SERVER"}} ---------------------------- Consumer: Source='TOPIC' Version='1.0' TimeStamp=datetime.datetime(2022, 8, 3, 6, 49, 17) Event=<EventType.STH_READY: 'STH_READY'> Data=TradeOverlapping(COBDate='2023-04-05', DBServer='DB_SERVER')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.