Skip to main content

A flake8 plugin to check Pydantic related code.

Project description

Flake8 Pydantic

Python versions PyPI version Ruff

A flake8 plugin to check Pydantic related code.

Class detection

flake8_pydantic parses the AST to emit linting errors. As such, it cannot accurately determine if a class is defined as a Pydantic model. However, it tries its best, using the following heuristics:

  • The class inherits from BaseModel or RootModel.
  • The class has a model_config attribute set.
  • The class has a field defined with the Field function.
  • The class has a field making use of Annotated.
  • The class makes use of Pydantic decorators, such as computed_field or model_validator.
  • The class overrides any of the Pydantic methods, such as model_dump.

Error codes

PYD001 - Positional argument for Field default argument

Raise an error if the default argument of the Field function is positional.

class Model(BaseModel):
    foo: int = Field(1)

Although allowed at runtime by Pydantic, it does not comply with the typing specification (PEP 681) and type checkers will not be able to synthesize a correct __init__ method.

Instead, consider using an explicit keyword argument:

class Model(BaseModel):
    foo: int = Field(default=1)

PYD002 - Non-annotated attribute inside Pydantic model

Raise an error if a non-annotated attribute is defined inside a Pydantic model class.

class Model(BaseModel):
    foo = 1  # Will error at runtime

PYD010 - Usage of __pydantic_config__

Raise an error if a Pydantic configuration is set with __pydantic_config__.

class Model(TypedDict):
    __pydantic_config__ = {}  # Type checkers will emit an error

Although allowed at runtime by Python, type checkers will emit an error as it is not allowed to assign values when defining a TypedDict.

Instead, consider using the with_config decorator:

@with_config({"str_to_lower": True})
class Model(TypedDict):
    pass

And many more to come.

Roadmap

Once the rules of the plugin gets stable, the goal will be to implement them in Ruff, with autofixes when possible.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8-pydantic-0.1.0.post0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

flake8_pydantic-0.1.0.post0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file flake8-pydantic-0.1.0.post0.tar.gz.

File metadata

  • Download URL: flake8-pydantic-0.1.0.post0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.13

File hashes

Hashes for flake8-pydantic-0.1.0.post0.tar.gz
Algorithm Hash digest
SHA256 fa9c2dfc812336e2df946e20d28e5a807468c9cc083321144adf4903ee4449ee
MD5 bdadfa752344e5236d911484228214bb
BLAKE2b-256 94bd21ea3aa562a76adfc7ef33e52421fd8e42a8b1c868308d45b41c40c87e80

See more details on using hashes here.

File details

Details for the file flake8_pydantic-0.1.0.post0-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_pydantic-0.1.0.post0-py3-none-any.whl
Algorithm Hash digest
SHA256 e14cbf63c020e834eb3cc63d5cd15bf214be390c1cb632b8d051618a65317e64
MD5 3076fbe93f60445259e0ffead1175ba5
BLAKE2b-256 958c2ace212f013e73914265084e76d680a05c35de9edba77eea675db71f7bd6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page