chore: enable pyupgrade fixes (#1806)

# What does this PR do?

The goal of this PR is code base modernization.

Schema reflection code needed a minor adjustment to handle UnionTypes
and collections.abc.AsyncIterator. (Both are preferred for latest Python
releases.)

Note to reviewers: almost all changes here are automatically generated
by pyupgrade. Some additional unused imports were cleaned up. The only
change worth of note can be found under `docs/openapi_generator` and
`llama_stack/strong_typing/schema.py` where reflection code was updated
to deal with "newer" types.

Signed-off-by: Ihar Hrachyshka <ihar.hrachyshka@gmail.com>
This commit is contained in:
Ihar Hrachyshka 2025-05-01 17:23:50 -04:00 committed by GitHub
parent ffe3d0b2cd
commit 9e6561a1ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
319 changed files with 2843 additions and 3033 deletions

View file

@ -5,15 +5,15 @@
# the root directory of this source tree.
from datetime import datetime
from typing import List, Optional, Protocol
from typing import Protocol
class KVStore(Protocol):
# TODO: make the value type bytes instead of str
async def set(self, key: str, value: str, expiration: Optional[datetime] = None) -> None: ...
async def set(self, key: str, value: str, expiration: datetime | None = None) -> None: ...
async def get(self, key: str) -> Optional[str]: ...
async def get(self, key: str) -> str | None: ...
async def delete(self, key: str) -> None: ...
async def range(self, start_key: str, end_key: str) -> List[str]: ...
async def range(self, start_key: str, end_key: str) -> list[str]: ...

View file

@ -6,10 +6,9 @@
import re
from enum import Enum
from typing import Literal, Optional, Union
from typing import Annotated, Literal
from pydantic import BaseModel, Field, field_validator
from typing_extensions import Annotated
from llama_stack.distribution.utils.config_dirs import RUNTIME_BASE_DIR
@ -22,7 +21,7 @@ class KVStoreType(Enum):
class CommonConfig(BaseModel):
namespace: Optional[str] = Field(
namespace: str | None = Field(
default=None,
description="All keys will be prefixed with this namespace",
)
@ -69,7 +68,7 @@ class PostgresKVStoreConfig(CommonConfig):
port: int = 5432
db: str = "llamastack"
user: str
password: Optional[str] = None
password: str | None = None
table_name: str = "llamastack_kvstore"
@classmethod
@ -108,7 +107,7 @@ class MongoDBKVStoreConfig(CommonConfig):
port: int = 27017
db: str = "llamastack"
user: str = None
password: Optional[str] = None
password: str | None = None
collection_name: str = "llamastack_kvstore"
@classmethod
@ -126,6 +125,6 @@ class MongoDBKVStoreConfig(CommonConfig):
KVStoreConfig = Annotated[
Union[RedisKVStoreConfig, SqliteKVStoreConfig, PostgresKVStoreConfig, MongoDBKVStoreConfig],
RedisKVStoreConfig | SqliteKVStoreConfig | PostgresKVStoreConfig | MongoDBKVStoreConfig,
Field(discriminator="type", default=KVStoreType.sqlite.value),
]

View file

@ -4,7 +4,6 @@
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.
from typing import List, Optional
from .api import KVStore
from .config import KVStoreConfig, KVStoreType
@ -21,13 +20,13 @@ class InmemoryKVStoreImpl(KVStore):
async def initialize(self) -> None:
pass
async def get(self, key: str) -> Optional[str]:
async def get(self, key: str) -> str | None:
return self._store.get(key)
async def set(self, key: str, value: str) -> None:
self._store[key] = value
async def range(self, start_key: str, end_key: str) -> List[str]:
async def range(self, start_key: str, end_key: str) -> list[str]:
return [self._store[key] for key in self._store.keys() if key >= start_key and key < end_key]

View file

@ -6,7 +6,6 @@
import logging
from datetime import datetime
from typing import List, Optional
from pymongo import AsyncMongoClient
@ -43,12 +42,12 @@ class MongoDBKVStoreImpl(KVStore):
return key
return f"{self.config.namespace}:{key}"
async def set(self, key: str, value: str, expiration: Optional[datetime] = None) -> None:
async def set(self, key: str, value: str, expiration: datetime | None = None) -> None:
key = self._namespaced_key(key)
update_query = {"$set": {"value": value, "expiration": expiration}}
await self.collection.update_one({"key": key}, update_query, upsert=True)
async def get(self, key: str) -> Optional[str]:
async def get(self, key: str) -> str | None:
key = self._namespaced_key(key)
query = {"key": key}
result = await self.collection.find_one(query, {"value": 1, "_id": 0})
@ -58,7 +57,7 @@ class MongoDBKVStoreImpl(KVStore):
key = self._namespaced_key(key)
await self.collection.delete_one({"key": key})
async def range(self, start_key: str, end_key: str) -> List[str]:
async def range(self, start_key: str, end_key: str) -> list[str]:
start_key = self._namespaced_key(start_key)
end_key = self._namespaced_key(end_key)
query = {

View file

@ -6,7 +6,6 @@
import logging
from datetime import datetime
from typing import List, Optional
import psycopg2
from psycopg2.extras import DictCursor
@ -54,7 +53,7 @@ class PostgresKVStoreImpl(KVStore):
return key
return f"{self.config.namespace}:{key}"
async def set(self, key: str, value: str, expiration: Optional[datetime] = None) -> None:
async def set(self, key: str, value: str, expiration: datetime | None = None) -> None:
key = self._namespaced_key(key)
self.cursor.execute(
f"""
@ -66,7 +65,7 @@ class PostgresKVStoreImpl(KVStore):
(key, value, expiration),
)
async def get(self, key: str) -> Optional[str]:
async def get(self, key: str) -> str | None:
key = self._namespaced_key(key)
self.cursor.execute(
f"""
@ -86,7 +85,7 @@ class PostgresKVStoreImpl(KVStore):
(key,),
)
async def range(self, start_key: str, end_key: str) -> List[str]:
async def range(self, start_key: str, end_key: str) -> list[str]:
start_key = self._namespaced_key(start_key)
end_key = self._namespaced_key(end_key)

View file

@ -5,7 +5,6 @@
# the root directory of this source tree.
from datetime import datetime
from typing import List, Optional
from redis.asyncio import Redis
@ -25,13 +24,13 @@ class RedisKVStoreImpl(KVStore):
return key
return f"{self.config.namespace}:{key}"
async def set(self, key: str, value: str, expiration: Optional[datetime] = None) -> None:
async def set(self, key: str, value: str, expiration: datetime | None = None) -> None:
key = self._namespaced_key(key)
await self.redis.set(key, value)
if expiration:
await self.redis.expireat(key, expiration)
async def get(self, key: str) -> Optional[str]:
async def get(self, key: str) -> str | None:
key = self._namespaced_key(key)
value = await self.redis.get(key)
if value is None:
@ -43,7 +42,7 @@ class RedisKVStoreImpl(KVStore):
key = self._namespaced_key(key)
await self.redis.delete(key)
async def range(self, start_key: str, end_key: str) -> List[str]:
async def range(self, start_key: str, end_key: str) -> list[str]:
start_key = self._namespaced_key(start_key)
end_key = self._namespaced_key(end_key)
cursor = 0

View file

@ -6,7 +6,6 @@
import os
from datetime import datetime
from typing import List, Optional
import aiosqlite
@ -33,7 +32,7 @@ class SqliteKVStoreImpl(KVStore):
)
await db.commit()
async def set(self, key: str, value: str, expiration: Optional[datetime] = None) -> None:
async def set(self, key: str, value: str, expiration: datetime | None = None) -> None:
async with aiosqlite.connect(self.db_path) as db:
await db.execute(
f"INSERT OR REPLACE INTO {self.table_name} (key, value, expiration) VALUES (?, ?, ?)",
@ -41,7 +40,7 @@ class SqliteKVStoreImpl(KVStore):
)
await db.commit()
async def get(self, key: str) -> Optional[str]:
async def get(self, key: str) -> str | None:
async with aiosqlite.connect(self.db_path) as db:
async with db.execute(f"SELECT value, expiration FROM {self.table_name} WHERE key = ?", (key,)) as cursor:
row = await cursor.fetchone()
@ -55,7 +54,7 @@ class SqliteKVStoreImpl(KVStore):
await db.execute(f"DELETE FROM {self.table_name} WHERE key = ?", (key,))
await db.commit()
async def range(self, start_key: str, end_key: str) -> List[str]:
async def range(self, start_key: str, end_key: str) -> list[str]:
async with aiosqlite.connect(self.db_path) as db:
async with db.execute(
f"SELECT key, value, expiration FROM {self.table_name} WHERE key >= ? AND key <= ?",