|
| 1 | +Stub files (`*.pyi`) contain type hints used only by type checkers, not at |
| 2 | +runtime. They were introduced in |
| 3 | +[PEP 484](https://peps.python.org/pep-0484/#stub-files). For example, the |
| 4 | +[`typeshed`](https://github.com/python/typeshed) repository maintains a |
| 5 | +collection of such stubs for the Python standard library and some third-party |
| 6 | +libraries. |
| 7 | + |
| 8 | +The `./stubs` folder contains type information only for the parts of third-party |
| 9 | +dependencies used in the `pydantic-ai` codebase. These stubs must be manually |
| 10 | +maintained. When a dependency's API changes, both the codebase and the stubs |
| 11 | +need to be updated. There are two ways to update the stubs: |
| 12 | + |
| 13 | +(1) **Manual update:** Check the dependency's source code and copy the type |
| 14 | +information to `./stubs`. Take for example the `from_pretrained()` method of the |
| 15 | +`Llama` class in `llama-cpp-python`. The |
| 16 | +[source code](https://github.com/abetlen/llama-cpp-python/blob/main/llama_cpp/llama.py#L2240) |
| 17 | +contains the type information that is copied to `./stubs/llama_cpp.pyi`. This |
| 18 | +eliminates the need for `# type: ignore` comments in the codebase. |
| 19 | + |
| 20 | +(2) **Update with AI coding assistants:** Most dependencies maintain `llms.txt` |
| 21 | +and `llms-full.txt` files with their documentation. This information is compiled |
| 22 | +by [Context7](https://context7.com). For example, the `llama-cpp-python` library |
| 23 | +is documented [here](https://github.com/abetlen/llama-cpp-python). MCP servers |
| 24 | +such as [this one by Upstash](https://github.com/upstash/context7) provide AI |
| 25 | +coding assistants access to Context7. AI coding assistants such as VS Code |
| 26 | +Copilot or Cursor can reliably generate and update the stubs. |
0 commit comments