mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
c24d33e5f1
* Generate docs exampels for Python 3.10 and above Code quality is not great and main intent here is to show the result. * Fix docs build on 3.9 * Build docs on 3.10 * What's Python 3.1? * Create temp dir if not exists * Refactor and improve imlementetion * Keep runtime typing in examples * Revert unrelated formatting changes * Add changes file * Allow specifying requirements in examples * Pin autoflake and pyupgrade * Add docs/build to Makefile lint/format/mypy * ignore_missing_imports for ansi2html and devtools * Add .tmp-projections to .gitignore * Remove dont-upgrade now when Pattern is supported * Update postponed evaluation examples Co-authored-by: Samuel Colvin <s@muelcolvin.com>
47 lines
1.3 KiB
Markdown
47 lines
1.3 KiB
Markdown
*pydantic* models work with [mypy](http://mypy-lang.org/) provided you use the annotation-only version of
|
|
required fields:
|
|
|
|
{!.tmp_examples/mypy_main.md!}
|
|
|
|
You can run your code through mypy with:
|
|
|
|
```bash
|
|
mypy \
|
|
--ignore-missing-imports \
|
|
--follow-imports=skip \
|
|
--strict-optional \
|
|
pydantic_mypy_test.py
|
|
```
|
|
|
|
If you call mypy on the example code above, you should see mypy detect the attribute access error:
|
|
```
|
|
13: error: "Model" has no attribute "middle_name"
|
|
```
|
|
|
|
## Strict Optional
|
|
|
|
For your code to pass with `--strict-optional`, you need to to use `Optional[]` or an alias of `Optional[]`
|
|
for all fields with `None` as the default. (This is standard with mypy.)
|
|
|
|
Pydantic provides a few useful optional or union types:
|
|
|
|
* `NoneStr` aka. `Optional[str]`
|
|
* `NoneBytes` aka. `Optional[bytes]`
|
|
* `StrBytes` aka. `Union[str, bytes]`
|
|
* `NoneStrBytes` aka. `Optional[StrBytes]`
|
|
|
|
If these aren't sufficient you can of course define your own.
|
|
|
|
## Mypy Plugin
|
|
|
|
Pydantic ships with a mypy plugin that adds a number of important pydantic-specific
|
|
features to mypy that improve its ability to type-check your code.
|
|
|
|
See the [pydantic mypy plugin docs](../mypy_plugin.md) for more details.
|
|
|
|
|
|
## Other pydantic interfaces
|
|
|
|
Pydantic [dataclasses](dataclasses.md) and the [`validate_arguments` decorator](validation_decorator.md)
|
|
should also work well with mypy.
|