new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Pydantic · Integrations · all subjects

linting

23 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Flake8 Pydantic plugin installation

A Flake8 plugin for Pydantic is available and can be installed using pip install flake8-pydantic.

Flake8 Pydantic plugin error code namespace

The lint errors provided by the flake8-pydantic plugin are namespaced under the PYDXXX code format.

Flake8 Pydantic plugin configuration to ignore rules

To ignore unwanted Pydantic lint rules in Flake8, add an extend-ignore configuration in the [flake8] section of the configuration file with comma-separated error codes like PYD001,PYD002.

Flake8 Pydantic plugin configuration example

Example Flake8 configuration to ignore Pydantic rules: [flake8] section with extend-ignore = PYD001,PYD002.

Pyrefly built-in Pydantic support requires no configuration

Pyrefly is a Python type checker and language server with built-in support for Pydantic-specific features. This support works out-of-the-box with Pydantic and Pyrefly installed, with no additional configuration needed.

Pyrefly available as IDE extension and command-line type checker

Pyrefly is available in two forms: as an IDE extension providing features like go-to-definition and autocomplete on Pydantic models, and as a command-line type checker.

Pyrefly provides signature help for Pydantic validation_alias

Pyrefly's IDE extension provides signature help powered by its understanding of Pydantic-specific features like the validation_alias keyword, showing hints about the correct field names to use when instantiating models.

Pyrefly detects frozen model field mutations via ConfigDict

Pyrefly can catch errors when code attempts to mutate fields on frozen Pydantic models, even when the frozen state is declared via ConfigDict(frozen=True) rather than standard type system features. It reports these as read-only field errors.

Pyrefly example: frozen model field mutation detection

Code example showing Pyrefly's detection of frozen model mutations: ```python from pydantic import BaseModel, ConfigDict class Model1(BaseModel, frozen=True): x: int class Model2(BaseModel): model_config = ConfigDict(frozen=True) x: int model1 = Model1(x=0) model1.x = 1 # validation error: mutating a frozen field model2 = Model2(x=0) model2.x = 1 # validation error: mutating a frozen field ``` Pyrefly catches both mutations as read-only field errors. Standard type checkers only catch Model1 mutations, but Pyrefly additionally catches Model2 mutations from ConfigDict(frozen=True).

Pydantic mypy plugin enables type-checking for models

Pydantic ships with a mypy plugin that adds Pydantic-specific type-checking features. Without the plugin, mypy does not catch missing model field annotations and produces errors about valid Pydantic data conversion. With the plugin enabled, mypy catches untyped fields and validates model field types correctly.

Enable Pydantic mypy plugin in config file

To enable the plugin, add 'pydantic.mypy' to the plugins list in the mypy config file. For mypy.ini, add 'plugins = pydantic.mypy' under the [mypy] section. For pyproject.toml, add 'plugins = ["pydantic.mypy"]' under [tool.mypy]. If using pydantic.v1 models, add 'pydantic.v1.mypy' instead.

Pydantic mypy plugin capabilities

The plugin generates a __init__ signature for Pydantic models, generates a typed signature for model_construct, supports frozen models, respects Field default and default_factory types, warns about untyped fields, and prevents use of required dynamic aliases. Required fields without dynamically-determined aliases are included as required keyword arguments. If validate_by_name is True, the signature uses field names rather than aliases.

init_typed plugin configuration option

The init_typed option controls whether the __init__() method uses typed field annotations or Any. Default is False. When False, the plugin uses Any for field annotations when synthesizing __init__(), allowing Pydantic's data conversion to work. Set to True to enforce typed annotations, unless strict mode is enabled on the model.

init_forbid_extra plugin configuration option

The init_forbid_extra option controls whether the synthesized __init__() method accepts extra arguments. Default is False. When False, the plugin adds **kwargs: Any parameter to match Pydantic's default behavior of ignoring extra arguments. Set to True to forbid extra arguments, or set the extra config value to 'forbid'.

warn_required_dynamic_aliases plugin configuration option

The warn_required_dynamic_aliases option controls whether to error when using a dynamically-determined alias or alias generator with validate_by_name set to False. Default is False. When True, it warns about these aliases because mypy cannot properly type check such calls to __init__(). When such problematic aliases are present, the __init__() defaults to (**kwargs: Any) -> None.

Mypy plugin configuration section location

Plugin settings are configured in a [pydantic-mypy] section in mypy.ini or [tool.pydantic-mypy] section in pyproject.toml. Use key-value pairs to override default plugin settings like init_forbid_extra, init_typed, and warn_required_dynamic_aliases.

Mypy plugin with strict mode example configuration

Example mypy.ini configuration with all plugin strictness flags enabled includes: [mypy] section with plugins = pydantic.mypy, follow_imports = silent, warn_redundant_casts = True, warn_unused_ignores = True, disallow_any_generics = True, no_implicit_reexport = True, disallow_untyped_defs = True; and [pydantic-mypy] section with init_forbid_extra = True, init_typed = True, warn_required_dynamic_aliases = True. Equivalent pyproject.toml uses [tool.mypy] and [tool.pydantic-mypy] sections with string values instead of booleans.

Mypy plugin tested against latest mypy version

The Pydantic mypy plugin is tested against the latest mypy version. Older versions might work but are not tested.

Mypy plugin error detection improvements

With the Pydantic mypy plugin enabled, mypy correctly detects untyped fields with a pydantic-field error code, catches access to non-existent model fields with attr-defined error, and properly validates required model arguments. Without the plugin, mypy incorrectly rejects valid Pydantic data conversions like list item type coercion.

Mypy plugin frozen model support

If the frozen configuration is set to True on a Pydantic model, the mypy plugin will emit an error if you try to mutate a model field, implementing static checking for faux immutability.

Mypy plugin Field default and default_factory validation

The mypy plugin respects the type of Field's default and default_factory values. A field with both a default and a default_factory will result in an error during static checking. The type of the default and default_factory value must be compatible with the field's declared type.

Mypy plugin model_construct type signature

The mypy plugin generates a typed signature for the model_construct method, which is an alternative to model validation when input data is known to be valid and should not be parsed. Static type checking is important for detecting errors in model_construct calls since the method performs no runtime validation.

Configure mypy in VS Code

To enable mypy in VS Code, open User Settings, search for 'Mypy Enabled', find the option under 'Python › Linting: Mypy Enabled', and check the box. By default it is unchecked. This allows mypy error checks to appear inline in the editor, including errors detected by the Pydantic mypy plugin if configured.

Give your agent this brain