'Why is my pydantic model not displaying any values after setting it?

from pydantic import BaseModel

class Test(BaseModel):
    val1 = str
    val2 = str

test = {
    "val1": "1010101",
    "val2": "1010101",
}
test_value= Test(**test)

print(test_value) # this doesn't display anything
print(test_value.val1) # this only display `<class 'str'>`

I have this simple structure of using pydantic. But when I try to print the value, it doesn't display anything.

Am I missing something here?



Solution 1:[1]

You have to use : between the field name and its type instead of =.

class Test(BaseModel):
    val1: str
    val2: str

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hernán Alarcón