-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add type stubs for some third-party libraries #3443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+274
−50
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
eea1136
mlx only on Apple silicon
lars20070 eb32ac4
stubs for mlx, vllm, outlines and transformers
lars20070 6498440
add stubs to ruff config
lars20070 265a0d1
deferred evaluation
lars20070 9756d30
ignore more in stubs
lars20070 bb4d9c3
remove further type ignore
lars20070 8335128
remove further type check
lars20070 9241013
remove further type ignore
lars20070 cda13d9
remove further type ignore
lars20070 ad6b275
remove further type ignore
lars20070 d627aa2
add linter rules again
lars20070 6f117d7
ignore rules again
lars20070 3b64931
Trigger CI
lars20070 76d684b
Merge branch 'main' into experiment-wide-evals
DouweM a8554f0
fix stub inconsistencies and duplications
lars20070 5cc2e78
docu for stubs
lars20070 7c11f09
further stubs for outlines
lars20070 e343876
fix error in outlines stub
lars20070 ca13700
fix linter
lars20070 bfeee98
check the stubs
lars20070 0c09656
Merge branch 'main' into experiment-wide-evals
DouweM b0b7aab
rename /stubs folder to /typings
lars20070 5b9fc1c
fix markdown formatting
lars20070 489d9c1
fix docu
lars20070 10bce90
Merge branch 'main' into experiment-wide-evals
DouweM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| Stub files (`*.pyi`) contain type hints used only by type checkers, not at | ||
| runtime. They were introduced in | ||
| [PEP 484](https://peps.python.org/pep-0484/#stub-files). For example, the | ||
| [`typeshed`](https://github.com/python/typeshed) repository maintains a | ||
| collection of such stubs for the Python standard library and some third-party | ||
| libraries. | ||
|
|
||
| The `./typings` folder contains type information only for the parts of | ||
| third-party dependencies used in the `pydantic-ai` codebase. These stubs must be | ||
| manually maintained. When a dependency's API changes, both the codebase and the | ||
| stubs need to be updated. There are two ways to update the stubs: | ||
|
|
||
| 1. **Manual update:** Check the dependency's source code and copy the type | ||
| information to `./typings`. For example, take the `from_pretrained()` method | ||
| of the `Llama` class in `llama-cpp-python`. The | ||
| [source code](https://github.com/abetlen/llama-cpp-python/blob/main/llama_cpp/llama.py#L2240) | ||
| contains the type information that is copied to `./typings/llama_cpp.pyi`. | ||
| This eliminates the need for `# type: ignore` comments in the codebase. | ||
|
|
||
| 2. **Update with AI coding assistants:** Most dependencies maintain `llms.txt` | ||
| and `llms-full.txt` files with their documentation. This information is | ||
| compiled by [Context7](https://context7.com). For example, the | ||
| `llama-cpp-python` library is documented | ||
| [here](https://github.com/abetlen/llama-cpp-python). MCP servers such as | ||
| [this one by Upstash](https://github.com/upstash/context7) provide AI coding | ||
| assistants access to Context7. AI coding assistants such as VS Code Copilot | ||
| or Cursor can reliably generate and update the stubs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from collections.abc import Sequence | ||
| from os import PathLike | ||
| from typing import Any, Literal | ||
|
|
||
| from typing_extensions import Self | ||
|
|
||
| class Llama: | ||
| def __init__(self, *args: Any, **kwargs: Any) -> None: ... | ||
| @classmethod | ||
| def from_pretrained( | ||
| cls, | ||
| repo_id: str, | ||
| filename: str | None = None, | ||
| additional_files: Sequence[str] | None = None, | ||
| local_dir: str | PathLike[str] | None = None, | ||
| local_dir_use_symlinks: bool | Literal['auto'] = 'auto', | ||
| cache_dir: str | PathLike[str] | None = None, | ||
| **kwargs: Any, | ||
| ) -> Self: ... | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from typing import Any | ||
|
|
||
| from . import nn | ||
|
|
||
| # mlx is imported as a package, primarily for mlx.nn | ||
| __all__: list[str] = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from typing import Any | ||
|
|
||
| class Module: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from typing import Any | ||
|
|
||
| from mlx.nn import Module | ||
| from transformers.tokenization_utils import PreTrainedTokenizer | ||
|
|
||
| def load(model_path: str | None = None, *args: Any, **kwargs: Any) -> tuple[Module, PreTrainedTokenizer]: ... | ||
| def generate_step(*args: Any, **kwargs: Any) -> Any: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from . import models | ||
|
|
||
| __all__: list[str] = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from typing import Any | ||
|
|
||
| from PIL.Image import Image as PILImage | ||
|
|
||
| class Chat: | ||
| def __init__(self, messages: list[dict[str, Any]] | None = None) -> None: ... | ||
| def add_system_message(self, content: str) -> None: ... | ||
| def add_user_message(self, content: str | Sequence[str | Image]) -> None: ... | ||
| def add_assistant_message(self, content: str | list[str | Image]) -> None: ... | ||
| def extend(self, messages: list[dict[str, Any]]) -> None: ... | ||
| def append(self, message: dict[str, Any]) -> None: ... | ||
| def pop(self) -> dict[str, Any] | None: ... | ||
|
|
||
| class Image: | ||
| def __init__(self, image: PILImage) -> None: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from . import base, llamacpp, mlxlm, sglang, transformers, vllm_offline | ||
|
|
||
| __all__: list[str] = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from collections.abc import AsyncIterable, Iterable | ||
| from typing import Any | ||
|
|
||
| class Model: | ||
| def __call__(self, *args: Any, **kwargs: Any) -> Any: ... | ||
| def stream(self, *args: Any, **kwargs: Any) -> Iterable[Any]: ... | ||
|
|
||
| class AsyncModel: | ||
| async def __call__(self, *args: Any, **kwargs: Any) -> Any: ... | ||
| def stream(self, *args: Any, **kwargs: Any) -> AsyncIterable[Any]: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from outlines.models.base import Model | ||
|
|
||
| if TYPE_CHECKING: | ||
| from llama_cpp import Llama | ||
|
|
||
| class LlamaCpp(Model): ... | ||
|
|
||
| def from_llamacpp(model: Llama) -> LlamaCpp: ... |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.