VKBottle Types
vkbottle-types is a companion library to vkbottle, providing machine-generated type hints and Pydantic models for VK API methods and objects. It ensures robust type checking and auto-completion for developers working with the VK API. The current version is 5.199.99.20, with updates frequently released to reflect VK API changes.
Common errors
-
AttributeError: 'MessagesMessage' object has no attribute 'some_new_field'
cause The VK API has introduced a new field that is not yet reflected in your installed `vkbottle-types` version, or the field name was misspelled.fixUpdate `vkbottle-types` to the latest version (`pip install --upgrade vkbottle-types`) or verify the field name against official VK API documentation. -
TypeError: __init__() got an unexpected keyword argument 'old_field_name'
cause The VK API schema (and thus `vkbottle-types`) has removed or renamed a field that you are attempting to provide during model instantiation.fixReview the `vkbottle-types` model definition (e.g., in its source code) or VK API documentation for the correct and current field names. Remove or rename the problematic argument. -
ImportError: cannot import name 'SomeType' from 'vkbottle_types.objects'
cause The specified type name is incorrect, misspelled, or the type has been moved/renamed in a newer version of `vkbottle-types`.fixConsult the `vkbottle-types` documentation or explore the `vkbottle_types` package content (e.g., using an IDE's autocomplete or `dir()`) to find the correct import path and symbol name.
Warnings
- gotcha vkbottle-types versions should always be kept compatible with your vkbottle library version. Mismatched versions can lead to `AttributeError`, `TypeError`, or `ImportError` due to schema changes or internal refactors.
- gotcha As vkbottle-types directly mirrors the VK API schema, rapid changes in the VK API might lead to temporary inconsistencies. The library's types may not exactly match the latest API responses until an update is released.
- gotcha Instantiating some nested or polymorphic types directly from raw data can be challenging. Ensure your data structure precisely matches the expected Pydantic schema for complex objects to avoid `TypeError` or validation errors.
Install
-
pip install vkbottle-types
Imports
- UsersUser
from vkbottle_types.objects import UsersUser
- MessagesMessage
from vkbottle_types.objects import MessagesMessage
- APIMethods
from vkbottle_types.methods import APIMethods
- BaseModel
from vkbottle_types.base_model import BaseModel
Quickstart
import json
from vkbottle_types.objects import UsersUser, MessagesMessage
# Example 1: Instantiating a User type
user_data = {
"id": 12345,
"first_name": "Alex",
"last_name": "Example",
"is_closed": False,
"can_access_closed": True
}
user: UsersUser = UsersUser(**user_data)
print(f"User ID: {user.id}, Full Name: {user.first_name} {user.last_name}")
print(f"User JSON: {user.json(indent=2)}")
# Example 2: Demonstrating another type's structure
message_payload_data = {
"id": 1,
"peer_id": 2000000001,
"from_id": 12345,
"date": 1678886400,
"text": "Hello, vkbottle-types!",
"random_id": 0,
"attachments": [],
"fwd_messages": [],
"important": False,
"is_hidden": False,
"out": 0,
"conversation_message_id": 1
}
message_payload: MessagesMessage = MessagesMessage(**message_payload_data)
print(f"Message text: '{message_payload.text}' from user {message_payload.from_id}")
# These types are primarily used for type hinting and data validation
# in vkbottle applications, not for direct API calls.