Metadata-Version: 2.1
Name: flake8-pydantic
Version: 0.1.0.post0
Summary: A flake8 plugin to check Pydantic related code.
Author-email: Victorien <contact@vctrn.dev>
License: MIT License
        
        Copyright (c) 2023 Victorien
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/Viicos/flake8-pydantic
Project-URL: Source, https://github.com/Viicos/flake8-pydantic
Project-URL: Changelog, https://github.com/Viicos/flake8-pydantic/blob/main/CHANGELOG.md
Project-URL: Documentation, https://viicos.github.io/flake8-pydantic/
Classifier: Development Status :: 4 - Beta
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Typing :: Typed
Classifier: License :: OSI Approved :: MIT License
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Quality Assurance
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: flake8
Requires-Dist: typing-extensions>=4.4.0; python_version < "3.11"

# Flake8 Pydantic

[![Python versions](https://img.shields.io/pypi/pyversions/flake8-pydantic.svg)](https://www.python.org/downloads/)
[![PyPI version](https://img.shields.io/pypi/v/flake8-pydantic.svg)](https://pypi.org/project/flake8-pydantic/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

A `flake8` plugin to check Pydantic related code.

## Class detection

`flake8_pydantic` parses the [AST](https://docs.python.org/3/library/ast.html) 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`](https://docs.pydantic.dev/latest/api/fields/#pydantic.fields.Field) function is positional.

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

Although allowed at runtime by Pydantic, it does not comply with the [typing specification (PEP 681)](https://typing.readthedocs.io/en/latest/spec/dataclasses.html#field-specifier-parameters) and type checkers will not be able to synthesize a correct `__init__` method.

Instead, consider using an explicit keyword argument:

```python
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.

```python
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__`](https://docs.pydantic.dev/dev/concepts/config/#configuration-with-dataclass-from-the-standard-library-or-typeddict).

```python
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`](https://docs.python.org/3/library/typing.html#typing.TypedDict).

Instead, consider using the [`with_config`](https://docs.pydantic.dev/dev/api/config/#pydantic.config.with_config) decorator:

```python
@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](https://github.com/astral-sh/ruff), with autofixes when possible.
