def get_schema(cls: type, /) -> Dict[str, Dict[str, Any]]
Introspect all in-memory instances of a @dbzero.memo class to deduce dynamic schema.
Provides current overview of attributes and their most common data types across all objects of the class. Schema adapts to runtime changes.
Parameters
clstype
A class decorated with @dbzero.memo for which to generate the schema.
Returns
Dictionary where keys are attribute names found across all instances. Values are metadata dictionaries containing type information.
The schema is dynamic! The output of get_schema() is not static. It is calculated on-the-fly and reflects the exact state of your objects at the moment of the call. The schema will change if you modify attribute types, add new attributes to instances, or delete objects.
Examples
Basic Schema Inference
Let's define a Car class and create a few instances. Notice how the photo attribute contains different data types.
import dbzero as db0
@db0.memo
class Car:
def __init__(self, brand, model, year, photo):
self.brand = brand
self.model = model
self.year = year
self.photo = photo
# Create instances with varied data for the 'photo' attribute
Car("Toyota", "Corolla", 2020, "[https://example.com/toyota.jpg](https://example.com/toyota.jpg)") # photo is a str
Car("BMW", "X5", 2021, "[https://example.com/bmw.jpg](https://example.com/bmw.jpg)") # photo is a str
Car("Audi", "A4", 2022, b'\x89PNG...') # photo is bytes
Car("Mercedes", "C-Class", 2023, None) # photo is None
# Get the current schema for the Car class
car_schema = db0.get_schema(Car)
print(car_schema)The output will look similar to this:
{
"brand": {"primary_type": "<class 'str'>", "all_types": ["<class 'str'>"]},
"model": {"primary_type": "<class 'str'>", "all_types": ["<class 'str'>"]},
"year": {"primary_type": "<class 'int'>", "all_types": ["<class 'int'>"]},
"photo": {"primary_type": "<class 'str'>", "all_types": ["<class 'str'>", "<class 'bytes'>", "<class 'NoneType'>"]}
}Here, the primary_type for the photo attribute is inferred as str because it is the most common type (it appears twice), while bytes appears only once. The all_types list shows all the types that have been used for this attribute, including NoneType.
Schema with Dynamic Attributes
dbzero also tracks attributes that are added to an object after it's been created.
car = Car("Honda", "Civic", 2024, None)
# Add a new attribute at runtime
car.service_due_date = "2025-10-01"
# The new attribute is now part of the schema
updated_schema = db0.get_schema(Car)
print(updated_schema)The new schema will now include service_due_date:
{
"brand": {"primary_type": "<class 'str'>", "all_types": ["<class 'str'>"]},
"model": {"primary_type": "<class 'str'>", "all_types": ["<class 'str'>"]},
"year": {"primary_type": "<class 'int'>", "all_types": ["<class 'int'>"]},
"photo": {"primary_type": "<class 'str'>", "all_types": ["<class 'str'>", "<class 'bytes'>", "<class 'NoneType'>"]},
"service_due_date": {"primary_type": "<class 'str'>", "all_types": ["<class 'str'>"]}
}