Skip to content

Commit

Permalink
Change rule numbering
Browse files Browse the repository at this point in the history
  • Loading branch information
JensHeinrich authored and JensHeinrich committed Sep 4, 2023
1 parent d5ecb1c commit d5c8282
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
90 changes: 47 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Bump Pydantic is a tool to help you migrate your code from Pydantic V1 to V2.
- [BP006: Replace `__root__` by `RootModel`](#bp006-replace-__root__-by-rootmodel)
- [BP007: Replace decorators](#bp007-replace-decorators)
- [BP008: Replace `con*` functions by `Annotated` versions](#bp008-replace-con-functions-by-annotated-versions)
- [BP009: Mark pydantic "protocol" functions in custom types with proper TODOs](bp009-mark-pydantic-protocol-functions-in-custom-types-with-proper-todos)

- [License](#license)

---
Expand Down Expand Up @@ -301,7 +303,50 @@ class User(BaseModel):
name: Annotated[str, StringConstraints(min_length=1)]
```

<!-- ### BP009: Replace `pydantic.parse_obj_as` by `pydantic.TypeAdapter`
### BP009: Mark pydantic "protocol" functions in custom types with proper TODOs

- ✅ Mark `__get_validators__` as to be replaced by `__get_pydantic_core_schema__`.
- ✅ Mark `__modify_schema__` as to be replaced by `__get_pydantic_json_schema__`.

The following code will be transformed:

```py
class SomeThing:
@classmethod
def __get_validators__(cls):
yield from []
return

@classmethod
def __modify_schema__(
cls, field_schema: Dict[str, Any], field: Optional[ModelField]
):
if field:
field_schema['example'] = "Weird example"
```

Into:

```py
class SomeThing:
@classmethod
# TODO[pydantic]: We couldn't refactor `__get_validators__`, please create the `__get_pydantic_core_schema__` manually.
# Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
def __get_validators__(cls):
yield from []
return

@classmethod
# TODO[pydantic]: We couldn't refactor `__modify_schema__`, please create the `__get_pydantic_json_schema__` manually.
# Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
def __modify_schema__(
cls, field_schema: Dict[str, Any], field: Optional[ModelField]
):
if field:
field_schema['example'] = "Weird example"
```

<!-- ### BP010: Replace `pydantic.parse_obj_as` by `pydantic.TypeAdapter`
- ✅ Replace `pydantic.parse_obj_as(T, obj)` to `pydantic.TypeAdapter(T).validate_python(obj)`.
Expand Down Expand Up @@ -344,7 +389,7 @@ class Users(BaseModel):
users = TypeAdapter(Users).validate_python({'users': [{'name': 'John'}]})
``` -->

<!-- ### BP010: Replace `PyObject` by `ImportString`
<!-- ### BP011: Replace `PyObject` by `ImportString`
- ✅ Replace `PyObject` by `ImportString`.
Expand All @@ -368,48 +413,7 @@ class User(BaseModel):
name: ImportString
``` -->

### BP010: Mark pydantic "protocol" functions in custom types with proper TODOs

- ✅ Mark `__get_validators__` as to be replaced by `__get_pydantic_core_schema__`.
- ✅ Mark `__modify_schema__` as to be replaced by `__get_pydantic_json_schema__`.

The following code will be transformed:

```py
class SomeThing:
@classmethod
def __get_validators__(cls):
yield from []
return

@classmethod
def __modify_schema__(
cls, field_schema: Dict[str, Any], field: Optional[ModelField]
):
if field:
field_schema['example'] = "Weird example"
```

Into:

```py
class SomeThing:
@classmethod
# TODO[pydantic]: We couldn't refactor `__get_validators__`, please create the `__get_pydantic_core_schema__` manually.
# Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
def __get_validators__(cls):
yield from []
return

@classmethod
# TODO[pydantic]: We couldn't refactor `__modify_schema__`, please create the `__get_pydantic_json_schema__` manually.
# Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
def __modify_schema__(
cls, field_schema: Dict[str, Any], field: Optional[ModelField]
):
if field:
field_schema['example'] = "Weird example"
```

---

Expand Down
4 changes: 2 additions & 2 deletions bump_pydantic/codemods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Rule(str, Enum):
"""Replace `@validator` with `@field_validator`."""
BP008 = "BP008"
"""Replace `con*` functions by `Annotated` versions."""
BP010 = "BP010"
BP009 = "BP009"
"""Mark pydantic 'protocol' functions in custom types with proper TODOs"""


Expand Down Expand Up @@ -64,7 +64,7 @@ def gather_codemods(disabled: List[Rule]) -> List[Type[ContextAwareTransformer]]
if Rule.BP007 not in disabled:
codemods.append(ValidatorCodemod)

if Rule.BP010 not in disabled:
if Rule.BP009 not in disabled:
codemods.append(CustomTypeCodemod)

# Those codemods need to be the last ones.
Expand Down

0 comments on commit d5c8282

Please sign in to comment.