ci: add mypy for static type checking (#1101)

# What does this PR do?

- Enable mypy to run in the CI on a subset of the repository
- Fix a few mypy errors
- Run mypy from pre-commit

Signed-off-by: Sébastien Han <seb@redhat.com>
 
[//]: # (If resolving an issue, uncomment and update the line below)
[//]: # (Closes #[issue-number])

## Test Plan
[Describe the tests you ran to verify your changes with result
summaries. *Provide clear instructions so the plan can be easily
re-executed.*]

[//]: # (## Documentation)

Signed-off-by: Sébastien Han <seb@redhat.com>
This commit is contained in:
Sébastien Han 2025-02-21 22:15:40 +01:00 committed by GitHub
parent 25fddccfd8
commit 9bbe34694d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 125 additions and 91 deletions

View file

@ -91,15 +91,18 @@ ParamType = register_schema(
name="ParamType",
)
"""
# TODO: recursive definition of ParamType in these containers
# will cause infinite recursion in OpenAPI generation script
# since we are going with ChatCompletionInputType and CompletionInputType
# we don't need to worry about ArrayType/ObjectType/UnionType for now
# ArrayType.model_rebuild()
# ObjectType.model_rebuild()
# UnionType.model_rebuild()
ArrayType.model_rebuild()
ObjectType.model_rebuild()
UnionType.model_rebuild()
# class CustomType(BaseModel):
# type: Literal["custom"] = "custom"
# validator_class: str
class CustomType(BaseModel):
pylint: disable=syntax-error
type: Literal["custom"] = "custom"
validator_class: str
"""

View file

@ -5,12 +5,10 @@
# the root directory of this source tree.
from dataclasses import dataclass
from typing import Any, Callable, List, Optional, TypeVar
from typing import Any, Callable, List, Optional, Protocol, TypeVar
from .strong_typing.schema import json_schema_type, register_schema # noqa: F401
T = TypeVar("T")
@dataclass
class WebMethod:
@ -22,6 +20,13 @@ class WebMethod:
raw_bytes_request_body: Optional[bool] = False
class HasWebMethod(Protocol):
__webmethod__: WebMethod
T = TypeVar("T", bound=HasWebMethod) # Bound T to classes that match this protocol
def webmethod(
route: Optional[str] = None,
method: Optional[str] = None,

View file

@ -11,7 +11,7 @@ import subprocess
import sys
from functools import partial
from pathlib import Path
from typing import Iterator
from typing import Iterable
from rich.progress import Progress, SpinnerColumn, TextColumn
@ -39,7 +39,7 @@ class ChangedPathTracker:
return self._changed_paths
def find_template_dirs(templates_dir: Path) -> Iterator[Path]:
def find_template_dirs(templates_dir: Path) -> Iterable[Path]:
"""Find immediate subdirectories in the templates folder."""
if not templates_dir.exists():
raise FileNotFoundError(f"Templates directory not found: {templates_dir}")
@ -90,7 +90,7 @@ def check_for_changes(change_tracker: ChangedPathTracker) -> bool:
return has_changes
def collect_template_dependencies(template_dir: Path) -> tuple[str, list[str]]:
def collect_template_dependencies(template_dir: Path) -> tuple[str | None, list[str]]:
try:
module_name = f"llama_stack.templates.{template_dir.name}"
module = importlib.import_module(module_name)

View file

@ -52,7 +52,7 @@ def main(parser: argparse.ArgumentParser):
pytest_args,
"-s",
"-v",
REPO_ROOT / CLIENT_SDK_TESTS_RELATIVE_PATH,
str(REPO_ROOT / CLIENT_SDK_TESTS_RELATIVE_PATH),
]
)