Skip to content

Commit 8616489

Browse files
authored
Merge pull request #1533 from tisnik/lcore-1891-use-final-types-in-constants
LCORE-1891: use `final` types in constants
2 parents d67f84d + d74a856 commit 8616489

2 files changed

Lines changed: 74 additions & 64 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ src/
5757
- Package `__init__.py` files contain brief package descriptions
5858
- Central `constants.py` for shared constants with descriptive comments
5959
- Type aliases defined at module level for clarity
60+
- Use Final[type] as type hint for all constants
6061

6162
#### Configuration
6263
- All config uses Pydantic models extending `ConfigurationBase`

src/constants.py

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
from typing import Final
44

5+
# Use Final[type] as type hint for all constants to ensure that type checkers (Mypy etc.)
6+
# will be able to detect assignements to such constants.
7+
58
# Minimal and maximal supported Llama Stack version
69
MINIMAL_SUPPORTED_LLAMA_STACK_VERSION: Final[str] = "0.2.17"
710
MAXIMAL_SUPPORTED_LLAMA_STACK_VERSION: Final[str] = "0.6.0"
811

9-
UNABLE_TO_PROCESS_RESPONSE = "Unable to process this request"
12+
UNABLE_TO_PROCESS_RESPONSE: Final[str] = "Unable to process this request"
1013

1114
# Response stored in the conversation when the user interrupts a streaming request
12-
INTERRUPTED_RESPONSE_MESSAGE = "You interrupted this request."
15+
INTERRUPTED_RESPONSE_MESSAGE: Final[str] = "You interrupted this request."
1316

1417
# Max seconds to wait for topic summary in background task after interrupt persist.
15-
TOPIC_SUMMARY_INTERRUPT_TIMEOUT_SECONDS = 30.0
18+
TOPIC_SUMMARY_INTERRUPT_TIMEOUT_SECONDS: Final[float] = 30.0
1619

1720
# Supported attachment types
1821
ATTACHMENT_TYPES = frozenset(
@@ -34,11 +37,11 @@
3437

3538
# Default system prompt used only when no other system prompt is specified in
3639
# configuration file nor in the query request
37-
DEFAULT_SYSTEM_PROMPT = "You are a helpful assistant"
40+
DEFAULT_SYSTEM_PROMPT: Final[str] = "You are a helpful assistant"
3841

3942
# Default topic summary system prompt used only when no other topic summary system
4043
# prompt is specified in configuration file
41-
DEFAULT_TOPIC_SUMMARY_SYSTEM_PROMPT = """
44+
DEFAULT_TOPIC_SUMMARY_SYSTEM_PROMPT: Final[str] = """
4245
Instructions:
4346
- You are a topic summarizer
4447
- Your job is to extract precise topic summary from user input
@@ -104,18 +107,18 @@
104107
"""
105108

106109
# Authentication constants
107-
DEFAULT_VIRTUAL_PATH = "/ls-access"
108-
DEFAULT_USER_NAME = "lightspeed-user"
109-
DEFAULT_SKIP_USER_ID_CHECK = True
110-
DEFAULT_USER_UID = "00000000-0000-0000-0000-000"
110+
DEFAULT_VIRTUAL_PATH: Final[str] = "/ls-access"
111+
DEFAULT_USER_NAME: Final[str] = "lightspeed-user"
112+
DEFAULT_SKIP_USER_ID_CHECK: Final[bool] = True
113+
DEFAULT_USER_UID: Final[str] = "00000000-0000-0000-0000-000"
111114
# default value for token when no token is provided
112-
NO_USER_TOKEN = ""
113-
AUTH_MOD_K8S = "k8s"
114-
AUTH_MOD_NOOP = "noop"
115-
AUTH_MOD_NOOP_WITH_TOKEN = "noop-with-token"
116-
AUTH_MOD_APIKEY_TOKEN = "api-key-token"
117-
AUTH_MOD_JWK_TOKEN = "jwk-token"
118-
AUTH_MOD_RH_IDENTITY = "rh-identity"
115+
NO_USER_TOKEN: Final[str] = ""
116+
AUTH_MOD_K8S: Final[str] = "k8s"
117+
AUTH_MOD_NOOP: Final[str] = "noop"
118+
AUTH_MOD_NOOP_WITH_TOKEN: Final[str] = "noop-with-token"
119+
AUTH_MOD_APIKEY_TOKEN: Final[str] = "api-key-token"
120+
AUTH_MOD_JWK_TOKEN: Final[str] = "jwk-token"
121+
AUTH_MOD_RH_IDENTITY: Final[str] = "rh-identity"
119122
# Supported authentication modules
120123
SUPPORTED_AUTHENTICATION_MODULES = frozenset(
121124
{
@@ -129,110 +132,116 @@
129132
)
130133
DEFAULT_AUTHENTICATION_MODULE = AUTH_MOD_NOOP
131134
# Maximum allowed size for base64-encoded x-rh-identity header (bytes)
132-
DEFAULT_RH_IDENTITY_MAX_HEADER_SIZE = 8192
135+
DEFAULT_RH_IDENTITY_MAX_HEADER_SIZE: Final[int] = 8192
133136

134137
# Maximum allowed file upload size (bytes) - 100MB default
135138
# Protects against DoS attacks via large file uploads
136-
DEFAULT_MAX_FILE_UPLOAD_SIZE = 100 * 1024 * 1024 # 100 MB
137-
DEFAULT_JWT_UID_CLAIM = "user_id"
138-
DEFAULT_JWT_USER_NAME_CLAIM = "username"
139+
DEFAULT_MAX_FILE_UPLOAD_SIZE: Final[int] = 100 * 1024 * 1024 # 100 MB
140+
DEFAULT_JWT_UID_CLAIM: Final[str] = "user_id"
141+
DEFAULT_JWT_USER_NAME_CLAIM: Final[str] = "username"
139142

140143
# MCP authorization header special values
141-
MCP_AUTH_KUBERNETES = "kubernetes"
142-
MCP_AUTH_CLIENT = "client"
143-
MCP_AUTH_OAUTH = "oauth"
144+
MCP_AUTH_KUBERNETES: Final[str] = "kubernetes"
145+
MCP_AUTH_CLIENT: Final[str] = "client"
146+
MCP_AUTH_OAUTH: Final[str] = "oauth"
144147

145148
# Media type constants for streaming responses
146-
MEDIA_TYPE_JSON = "application/json"
147-
MEDIA_TYPE_TEXT = "text/plain"
148-
MEDIA_TYPE_EVENT_STREAM = "text/event-stream"
149+
MEDIA_TYPE_JSON: Final[str] = "application/json"
150+
MEDIA_TYPE_TEXT: Final[str] = "text/plain"
151+
MEDIA_TYPE_EVENT_STREAM: Final[str] = "text/event-stream"
149152

150153
# Streaming event type constants
151-
LLM_TOKEN_EVENT = "token"
152-
LLM_TOOL_CALL_EVENT = "tool_call"
153-
LLM_TOOL_RESULT_EVENT = "tool_result"
154-
LLM_TURN_COMPLETE_EVENT = "turn_complete"
154+
LLM_TOKEN_EVENT: Final[str] = "token"
155+
LLM_TOOL_CALL_EVENT: Final[str] = "tool_call"
156+
LLM_TOOL_RESULT_EVENT: Final[str] = "tool_result"
157+
LLM_TURN_COMPLETE_EVENT: Final[str] = "turn_complete"
155158

156159
# PostgreSQL connection constants
157160
# See: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE
158-
POSTGRES_DEFAULT_SSL_MODE = "prefer"
161+
POSTGRES_DEFAULT_SSL_MODE: Final[str] = "prefer"
159162
# See: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-GSSENCMODE
160-
POSTGRES_DEFAULT_GSS_ENCMODE = "prefer"
163+
POSTGRES_DEFAULT_GSS_ENCMODE: Final[str] = "prefer"
161164

162165
# cache constants
163-
CACHE_TYPE_MEMORY = "memory"
164-
CACHE_TYPE_SQLITE = "sqlite"
165-
CACHE_TYPE_POSTGRES = "postgres"
166-
CACHE_TYPE_NOOP = "noop"
166+
CACHE_TYPE_MEMORY: Final[str] = "memory"
167+
CACHE_TYPE_SQLITE: Final[str] = "sqlite"
168+
CACHE_TYPE_POSTGRES: Final[str] = "postgres"
169+
CACHE_TYPE_NOOP: Final[str] = "noop"
167170

168171
# BYOK RAG
169172
# Default RAG type for bring-your-own-knowledge RAG configurations, that type
170173
# needs to be supported by Llama Stack
171-
DEFAULT_RAG_TYPE = "inline::faiss"
174+
DEFAULT_RAG_TYPE: Final[str] = "inline::faiss"
172175

173176
# Default sentence transformer model for embedding generation, that type needs
174177
# to be supported by Llama Stack and configured properly in providers and
175178
# models sections
176-
DEFAULT_EMBEDDING_MODEL = "sentence-transformers/all-mpnet-base-v2"
179+
DEFAULT_EMBEDDING_MODEL: Final[str] = "sentence-transformers/all-mpnet-base-v2"
177180

178181
# Default embedding vector dimension for the sentence transformer model
179-
DEFAULT_EMBEDDING_DIMENSION = 768
182+
DEFAULT_EMBEDDING_DIMENSION: Final[int] = 768
180183

181184
# quota limiters constants
182-
USER_QUOTA_LIMITER = "user_limiter"
183-
CLUSTER_QUOTA_LIMITER = "cluster_limiter"
185+
USER_QUOTA_LIMITER: Final[str] = "user_limiter"
186+
CLUSTER_QUOTA_LIMITER: Final[str] = "cluster_limiter"
184187

185188
# RAG as a tool constants
186-
DEFAULT_RAG_TOOL = "file_search"
187-
TOOL_RAG_MAX_CHUNKS = 10 # retrieved from RAG as a tool
189+
DEFAULT_RAG_TOOL: Final[str] = "file_search"
190+
TOOL_RAG_MAX_CHUNKS: Final[int] = 10 # retrieved from RAG as a tool
188191

189192
# Inline RAG constants
190-
BYOK_RAG_MAX_CHUNKS = 10 # retrieved from BYOK RAG
191-
OKP_RAG_MAX_CHUNKS = 5 # retrieved from OKP RAG
193+
BYOK_RAG_MAX_CHUNKS: Final[int] = 10 # retrieved from BYOK RAG
194+
OKP_RAG_MAX_CHUNKS: Final[int] = 5 # retrieved from OKP RAG
192195

193196
# Solr OKP constants
194-
SOLR_VECTOR_SEARCH_DEFAULT_K = 5
195-
SOLR_VECTOR_SEARCH_DEFAULT_SCORE_THRESHOLD = 0.3
196-
SOLR_VECTOR_SEARCH_DEFAULT_MODE = "hybrid"
197+
SOLR_VECTOR_SEARCH_DEFAULT_K: Final[int] = 5
198+
SOLR_VECTOR_SEARCH_DEFAULT_SCORE_THRESHOLD: Final[float] = 0.3
199+
SOLR_VECTOR_SEARCH_DEFAULT_MODE: Final[str] = "hybrid"
197200

198201
# Internal Solr filter always applied to restrict results to chunk documents
199-
SOLR_CHUNK_FILTER_QUERY = "is_chunk:true"
202+
SOLR_CHUNK_FILTER_QUERY: Final[str] = "is_chunk:true"
200203

201204
# SOLR OKP RAG - default base URL when okp.rhokp_url is unset in configuration
202-
RH_SERVER_OKP_DEFAULT_URL = "http://localhost:8081"
205+
RH_SERVER_OKP_DEFAULT_URL: Final[str] = "http://localhost:8081"
203206

204-
SOLR_PROVIDER_ID = "okp_solr"
207+
SOLR_PROVIDER_ID: Final[str] = "okp_solr"
205208

206209
# Solr default configuration values (can be overridden via environment variables)
207-
SOLR_DEFAULT_VECTOR_STORE_ID = "portal-rag"
208-
SOLR_DEFAULT_VECTOR_FIELD = "chunk_vector"
209-
SOLR_DEFAULT_CONTENT_FIELD = "chunk"
210-
SOLR_DEFAULT_EMBEDDING_MODEL = (
210+
SOLR_DEFAULT_VECTOR_STORE_ID: Final[str] = "portal-rag"
211+
SOLR_DEFAULT_VECTOR_FIELD: Final[str] = "chunk_vector"
212+
SOLR_DEFAULT_CONTENT_FIELD: Final[str] = "chunk"
213+
SOLR_DEFAULT_EMBEDDING_MODEL: Final[str] = (
211214
"sentence-transformers/ibm-granite/granite-embedding-30m-english"
212215
)
213-
SOLR_DEFAULT_EMBEDDING_DIMENSION = 384
216+
SOLR_DEFAULT_EMBEDDING_DIMENSION: Final[int] = 384
214217

215218
# Default score multiplier for BYOK RAG vector stores
216219
DEFAULT_SCORE_MULTIPLIER = 1.0
217220

218221
# Special RAG ID that activates the OKP provider when listed in rag.inline or rag.tool
219-
OKP_RAG_ID = "okp"
222+
OKP_RAG_ID: Final[str] = "okp"
220223

221224
# Logging configuration constants
222225
# Environment variable name for configurable log level
223-
LIGHTSPEED_STACK_LOG_LEVEL_ENV_VAR = "LIGHTSPEED_STACK_LOG_LEVEL"
226+
LIGHTSPEED_STACK_LOG_LEVEL_ENV_VAR: Final[str] = "LIGHTSPEED_STACK_LOG_LEVEL"
224227
# Default log level when environment variable is not set
225-
DEFAULT_LOG_LEVEL = "INFO"
228+
DEFAULT_LOG_LEVEL: Final[str] = "INFO"
226229
# Default log format for plain-text logging in non-TTY environments
227-
DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)-8s %(name)s:%(lineno)d %(message)s"
230+
DEFAULT_LOG_FORMAT: Final[str] = (
231+
"%(asctime)s %(levelname)-8s %(name)s:%(lineno)d %(message)s"
232+
)
228233
# Environment variable to force StreamHandler instead of RichHandler
229234
# Set to any non-empty value to disable RichHandler
230-
LIGHTSPEED_STACK_DISABLE_RICH_HANDLER_ENV_VAR = "LIGHTSPEED_STACK_DISABLE_RICH_HANDLER"
235+
LIGHTSPEED_STACK_DISABLE_RICH_HANDLER_ENV_VAR: Final[str] = (
236+
"LIGHTSPEED_STACK_DISABLE_RICH_HANDLER"
237+
)
231238

232-
DEFAULT_VIOLATION_MESSAGE = "I cannot process this request due to policy restrictions."
239+
DEFAULT_VIOLATION_MESSAGE: Final[str] = (
240+
"I cannot process this request due to policy restrictions."
241+
)
233242

234243
# Input size limits for API request validation
235244
# Maximum character length for the question field in /v1/infer requests (32 KiB)
236-
RLSAPI_V1_QUESTION_MAX_LENGTH = 32_768
245+
RLSAPI_V1_QUESTION_MAX_LENGTH: Final[int] = 32_768
237246
# Maximum character length for the serialized /v1/responses request body (64 KiB)
238-
RESPONSES_REQUEST_MAX_SIZE = 65_536
247+
RESPONSES_REQUEST_MAX_SIZE: Final[int] = 65_536

0 commit comments

Comments
 (0)