Source code for pinecone.models.namespaces.models

"""Namespace response models."""

from __future__ import annotations

from collections.abc import Iterator
from typing import Any, overload

from msgspec import Struct

from pinecone.models._mixin import StructDictMixin
from pinecone.models.vectors.responses import Pagination


class NamespaceFieldConfig(StructDictMixin, Struct, kw_only=True):
    """Configuration for a single metadata field in a namespace schema."""

    filterable: bool = False


class NamespaceSchema(StructDictMixin, Struct, kw_only=True):
    """Schema configuration for a namespace's metadata index."""

    fields: dict[str, NamespaceFieldConfig] = {}


class IndexedFields(StructDictMixin, Struct, kw_only=True):
    """List of indexed metadata fields in a namespace."""

    fields: list[str] = []


[docs] class NamespaceDescription(StructDictMixin, Struct, kw_only=True): """Description of a namespace including name, record count, and schema. Attributes: name: The name of the namespace. record_count: The total number of records in the namespace. schema: Schema configuration for metadata indexing, or None. indexed_fields: List of indexed metadata fields, or None. """ name: str = "" record_count: int = 0 schema: NamespaceSchema | None = None indexed_fields: IndexedFields | None = None def __getitem__(self, key: str) -> Any: """Support bracket access (e.g. ns['name']).""" if key not in self.__struct_fields__: raise KeyError(key) return getattr(self, key) def __contains__(self, key: object) -> bool: """Support ``in`` operator (e.g. ``'name' in ns``).""" return key in self.__struct_fields__
[docs] class ListNamespacesResponse(StructDictMixin, Struct, kw_only=True): """Response from a list namespaces operation. Attributes: namespaces: List of namespace descriptions in this page. pagination: Pagination token for the next page, or None if last page. total_count: Total number of namespaces matching the query. """ namespaces: list[NamespaceDescription] = [] pagination: Pagination | None = None total_count: int = 0 @overload def __getitem__(self, key: int) -> NamespaceDescription: ... @overload def __getitem__(self, key: str) -> Any: ... def __getitem__(self, key: int | str) -> Any: """Support integer indexing into namespaces and string bracket access. Args: key: An integer index into ``namespaces``, or a string field name. Returns: The namespace at the given index, or the field value. """ if isinstance(key, int): return self.namespaces[key] if key not in self.__struct_fields__: raise KeyError(key) return getattr(self, key) def __contains__(self, key: object) -> bool: """Support ``in`` for field names (str) and namespace membership.""" if isinstance(key, str): return key in self.__struct_fields__ return key in self.namespaces def __len__(self) -> int: return len(self.namespaces) def __iter__(self) -> Iterator[NamespaceDescription]: # type: ignore[override] return iter(self.namespaces)