Pydantic
Pydantic is a data validation library that defines data models through standard Python type annotations. PyCharm provides dedicated support for Pydantic models, built on top of the same type engine that powers other type-hinting features.
The support covers pydantic.BaseModel and its subclasses, pydantic.dataclasses.dataclass, generic models, and related constructs such as Field, model and field validators, and ConfigDict options. PyCharm recognizes both Pydantic v1 and v2, but it does not read the options of the v1 class Config, such as allow_population_by_field_name.
Code insight for model fields
When you instantiate a Pydantic model, PyCharm treats the declared fields as keyword arguments of __init__ and provides the following code insight:
Completion of field names as keyword arguments, considering their declared types and default values.
Type checking of values passed for each field against the corresponding annotation.
An inspection that highlights missing required fields. A field declared with
Annotated[..., Field(...)]and a default value after=is not required.Navigation from a keyword argument in an
__init__call to the field declaration and back, including across subclasses and base classes.
Field aliases and validation config
PyCharm accepts fields declared with an alias through Field(alias=...) as keyword arguments in constructor calls, without a false "unexpected argument" warning.
PyCharm respects the model configuration, so code insight in the editor matches the runtime validation behavior. PyCharm reads the options from the class keyword arguments, model_config, and the config argument or __pydantic_config__ of pydantic.dataclasses.dataclass. A subclass inherits the options of its base classes.
populate_by_name=Trueorvalidate_by_name=True: PyCharm accepts the field name in addition to its alias.validate_by_alias=False: PyCharm accepts only the field name. By default, PyCharm accepts the alias.
PyCharm completes the names of ConfigDict options when you pass them as keyword arguments in the model's class header, so you do not need to recall the exact option name.
When both options are enabled, PyCharm accepts the field name and its alias as keyword arguments:
PyCharm recognizes Field both in regular annotations and when it is used inside typing.Annotated:
Field constraint validation
PyCharm checks literal values passed to model constructors against the basic constraints declared on Field and reports a warning when a value does not satisfy them. The following constraints are recognized:
min_lengthandmax_lengthfor string fields.gt,ge,lt, andlefor numeric fields.
The constraints are also recognized when declared through typing.Annotated.
The check is performed by the Pydantic field constraint violation inspection. It does not run when an external type engine is selected.
Refactorings and validators
Renaming a model field also renames the keyword arguments in the __init__ calls of the model. Renaming such a keyword argument renames the field declaration.
In @field_validator, @field_serializer, and the legacy @validator, PyCharm resolves the field names that you pass as strings: you can complete them, navigate to the field declaration, and rename the field together with these strings.
When @field_validator, @model_validator, or @validator is combined with @classmethod, the Problematic nesting of decorators inspection does not report it.