Skip to content

chore: add type hints to schema registration functions#5264

Open
Elbehery wants to merge 3 commits intollamastack:mainfrom
Elbehery:20260324_fix_type_annotations_schema
Open

chore: add type hints to schema registration functions#5264
Elbehery wants to merge 3 commits intollamastack:mainfrom
Elbehery:20260324_fix_type_annotations_schema

Conversation

@Elbehery
Copy link
Contributor

What does this PR do?

This change adds complete type annotations to json_schema_type and
register_schema functions in schema_utils.py to satisfy mypy's
--disallow-untyped-defs requirement. Both functions are decorators used
for registering Pydantic models and schema types in the OpenAPI generation
system.

Uses modern Python 3.12+ generic function syntax (def func[T]) for type
parameters, matching the existing pattern used in ExtraBodyField[T]. This
provides proper type preservation through the decorator chain while
maintaining backward compatibility.

Type ignore comments are used for dynamic attribute assignment where the
decorator adds runtime attributes to classes. All changes pass mypy strict
checking and maintain full backward compatibility.

Test Plan

 # 1. Run standard mypy check
  ./scripts/uv-run-with-index.sh run --group dev --group type_checking mypy \
    src/llama_stack_api/datatypes.py \
    src/llama_stack_api/schema_utils.py

  # 2. Run strict type checking (the key test)
  ./scripts/uv-run-with-index.sh run --group dev --group type_checking mypy --disallow-untyped-defs \
    src/llama_stack_api/datatypes.py \
    src/llama_stack_api/schema_utils.py

  # 3. Run all checks in one command
  ./scripts/uv-run-with-index.sh run --group dev --group type_checking mypy --disallow-untyped-defs \
    src/llama_stack_api/datatypes.py \
    src/llama_stack_api/schema_utils.py && \
  uv run ruff check \
    src/llama_stack_api/datatypes.py \
    src/llama_stack_api/schema_utils.py && \
  uv run black --check \
    src/llama_stack_api/datatypes.py \
    src/llama_stack_api/schema_utils.py && \
  ./scripts/uv-run-with-index.sh run --group dev --group type_checking mypy

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 24, 2026
@Elbehery Elbehery changed the title chore: add type hints to schema registration functions chore: add type hints to schema registration functions Mar 24, 2026
@Elbehery Elbehery force-pushed the 20260324_fix_type_annotations_schema branch from 959bcd3 to 05be5f0 Compare March 24, 2026 14:48
@Elbehery
Copy link
Contributor Author

cc @leseb @skamenan7 @derekhiggins

leseb
leseb previously requested changes Mar 24, 2026
Copy link
Collaborator

@leseb leseb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove that entry from pyproject.toml

@Elbehery Elbehery force-pushed the 20260324_fix_type_annotations_schema branch from 05be5f0 to b2dc849 Compare March 24, 2026 15:22
@Elbehery
Copy link
Contributor Author

please remove that entry from pyproject.toml

sry, I am trying to keep the changes minimal 👍🏽

@Elbehery
Copy link
Contributor Author

please remove that entry from pyproject.toml

the exclude list in pyproject.toml contains only files under llama_stack/ not llama_stack_api

ref

Core Module Files (10 entries)

  src/llama_stack/core/client.py
  src/llama_stack/core/request_headers.py
  src/llama_stack/core/routers/                    (entire directory)
  src/llama_stack/core/routing_tables/             (entire directory)
  src/llama_stack/core/server/endpoints.py
  src/llama_stack/core/server/server.py
  src/llama_stack/core/stack.py
  src/llama_stack/core/store/registry.py
  src/llama_stack/core/utils/exec.py
  src/llama_stack/core/utils/prompt_for_config.py

  Inline Providers (8 entries)

  src/llama_stack/providers/inline/agents/builtin/           (entire directory)
  src/llama_stack/providers/inline/datasetio/localfs/        (entire directory)
  src/llama_stack/providers/inline/eval/builtin/eval.py
  src/llama_stack/providers/inline/inference/sentence_transformers/sentence_transformers.py
  src/llama_stack/providers/inline/post_training/common/validator.py
  src/llama_stack/providers/inline/safety/code_scanner/      (entire directory)
  src/llama_stack/providers/inline/safety/llama_guard/       (entire directory)
  src/llama_stack/providers/inline/scoring/basic/            (entire directory)
  src/llama_stack/providers/inline/scoring/braintrust/       (entire directory)
  src/llama_stack/providers/inline/scoring/llm_as_judge/     (entire directory)

  Remote Providers (21 entries)

  src/llama_stack/providers/remote/agents/sample/                              (entire directory)
  src/llama_stack/providers/remote/datasetio/huggingface/                      (entire directory)
  src/llama_stack/providers/remote/datasetio/nvidia/                           (entire directory)
  src/llama_stack/providers/remote/inference/oci/                              (entire directory)
  src/llama_stack/providers/remote/inference/bedrock/                          (entire directory)
  src/llama_stack/providers/remote/inference/nvidia/                           (entire directory)
  src/llama_stack/providers/remote/inference/passthrough/                      (entire directory)
  src/llama_stack/providers/remote/inference/runpod/                           (entire directory)
  src/llama_stack/providers/remote/inference/tgi/                              (entire directory)
  src/llama_stack/providers/remote/inference/watsonx/                          (entire directory)
  src/llama_stack/providers/remote/safety/bedrock/                             (entire directory)
  src/llama_stack/providers/remote/safety/nvidia/                              (entire directory)
  src/llama_stack/providers/remote/safety/sambanova/                           (entire directory)
  src/llama_stack/providers/remote/safety/sample/                              (entire directory)
  src/llama_stack/providers/remote/tool_runtime/bing_search/                   (entire directory)
  src/llama_stack/providers/remote/tool_runtime/brave_search/                  (entire directory)
  src/llama_stack/providers/remote/tool_runtime/model_context_protocol/        (entire directory)
  src/llama_stack/providers/remote/tool_runtime/tavily_search/                 (entire directory)
  src/llama_stack/providers/remote/tool_runtime/wolfram_alpha/                 (entire directory)
  src/llama_stack/providers/remote/post_training/nvidia/                       (entire directory)
  src/llama_stack/providers/remote/vector_io/chroma/                           (entire directory)
  src/llama_stack/providers/remote/vector_io/milvus/                           (entire directory)
  src/llama_stack/providers/remote/vector_io/pgvector/                         (entire directory)
  src/llama_stack/providers/remote/vector_io/qdrant/                           (entire directory)
  src/llama_stack/providers/remote/vector_io/weaviate/                         (entire directory)
  src/llama_stack/providers/remote/vector_io/elasticsearch/                    (entire directory)

  Provider Utils (13 entries)

  src/llama_stack/providers/utils/bedrock/client.py
  src/llama_stack/providers/utils/bedrock/refreshable_boto_session.py
  src/llama_stack/providers/utils/inference/embedding_mixin.py
  src/llama_stack/providers/utils/inference/model_registry.py
  src/llama_stack/providers/utils/inference/prompt_adapter.py
  src/llama_stack/providers/utils/kvstore/kvstore.py
  src/llama_stack/providers/utils/kvstore/postgres/postgres.py
  src/llama_stack/providers/utils/kvstore/redis/redis.py
  src/llama_stack/providers/utils/scoring/aggregation_utils.py
  src/llama_stack/providers/utils/scoring/base_scoring_fn.py
  src/llama_stack/providers/utils/telemetry/dataset_mixin.py
  src/llama_stack/providers/utils/telemetry/trace_protocol.py
  src/llama_stack/providers/utils/telemetry/tracing.py

  Distributions (1 entry)

  src/llama_stack/distributions/template.py

@Elbehery Elbehery force-pushed the 20260324_fix_type_annotations_schema branch from b2dc849 to 4da0d10 Compare March 24, 2026 15:33
@leseb leseb dismissed their stale review March 24, 2026 17:20

was thinking static type checking

@Elbehery Elbehery force-pushed the 20260324_fix_type_annotations_schema branch from e530b43 to 4e6d21c Compare March 25, 2026 08:55
  This change adds complete type annotations to json_schema_type and
  register_schema functions in schema_utils.py to satisfy mypy's
  --disallow-untyped-defs requirement. Both functions are decorators used
  for registering Pydantic models and schema types in the OpenAPI generation
  system.

  Uses modern Python 3.12+ generic function syntax (def func[T]) for type
  parameters, matching the existing pattern used in ExtraBodyField[T]. This
  provides proper type preservation through the decorator chain while
  maintaining backward compatibility.

  Type ignore comments are used for dynamic attribute assignment where the
  decorator adds runtime attributes to classes. All changes pass mypy strict
  checking and maintain full backward compatibility.

  Signed-off-by: Mustafa Elbehery <melbeher@redhat.com>
@Elbehery Elbehery force-pushed the 20260324_fix_type_annotations_schema branch from 4e6d21c to a11705c Compare March 25, 2026 08:56
Copy link
Collaborator

@mattf mattf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

thank you for starting this. it sounds like mypy shouldn't have been letting this pass.

what can you do to prevent adding the type ignore instructions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants