@@ -27,6 +27,8 @@ Bump Pydantic is a tool to help you migrate your code from Pydantic V1 to V2.
27
27
- [ BP006: Replace ` __root__ ` by ` RootModel ` ] ( #bp006-replace-__root__-by-rootmodel )
28
28
- [ BP007: Replace decorators] ( #bp007-replace-decorators )
29
29
- [ BP008: Replace ` con* ` functions by ` Annotated ` versions] ( #bp008-replace-con-functions-by-annotated-versions )
30
+ - [ BP009: Mark pydantic "protocol" functions in custom types with proper TODOs] ( bp009-mark-pydantic-protocol-functions-in-custom-types-with-proper-todos )
31
+
30
32
- [ License] ( #license )
31
33
32
34
---
@@ -301,7 +303,50 @@ class User(BaseModel):
301
303
name: Annotated[str , StringConstraints(min_length = 1 )]
302
304
```
303
305
304
- <!-- ### BP009: Replace `pydantic.parse_obj_as` by `pydantic.TypeAdapter`
306
+ ### BP009: Mark pydantic "protocol" functions in custom types with proper TODOs
307
+
308
+ - ✅ Mark ` __get_validators__ ` as to be replaced by ` __get_pydantic_core_schema__ ` .
309
+ - ✅ Mark ` __modify_schema__ ` as to be replaced by ` __get_pydantic_json_schema__ ` .
310
+
311
+ The following code will be transformed:
312
+
313
+ ``` py
314
+ class SomeThing :
315
+ @ classmethod
316
+ def __get_validators__ (cls ):
317
+ yield from []
318
+ return
319
+
320
+ @ classmethod
321
+ def __modify_schema__ (
322
+ cls , field_schema : Dict[str , Any], field : Optional[ModelField]
323
+ ):
324
+ if field:
325
+ field_schema[' example' ] = " Weird example"
326
+ ```
327
+
328
+ Into:
329
+
330
+ ``` py
331
+ class SomeThing :
332
+ @ classmethod
333
+ # TODO [pydantic]: We couldn't refactor `__get_validators__`, please create the `__get_pydantic_core_schema__` manually.
334
+ # Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
335
+ def __get_validators__ (cls ):
336
+ yield from []
337
+ return
338
+
339
+ @ classmethod
340
+ # TODO [pydantic]: We couldn't refactor `__modify_schema__`, please create the `__get_pydantic_json_schema__` manually.
341
+ # Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
342
+ def __modify_schema__ (
343
+ cls , field_schema : Dict[str , Any], field : Optional[ModelField]
344
+ ):
345
+ if field:
346
+ field_schema[' example' ] = " Weird example"
347
+ ```
348
+
349
+ <!-- ### BP010: Replace `pydantic.parse_obj_as` by `pydantic.TypeAdapter`
305
350
306
351
- ✅ Replace `pydantic.parse_obj_as(T, obj)` to `pydantic.TypeAdapter(T).validate_python(obj)`.
307
352
@@ -344,7 +389,7 @@ class Users(BaseModel):
344
389
users = TypeAdapter(Users).validate_python({'users': [{'name': 'John'}]})
345
390
``` -->
346
391
347
- <!-- ### BP010 : Replace `PyObject` by `ImportString`
392
+ <!-- ### BP011 : Replace `PyObject` by `ImportString`
348
393
349
394
- ✅ Replace `PyObject` by `ImportString`.
350
395
@@ -368,48 +413,7 @@ class User(BaseModel):
368
413
name: ImportString
369
414
``` -->
370
415
371
- ### BP010: Mark pydantic "protocol" functions in custom types with proper TODOs
372
-
373
- - ✅ Mark ` __get_validators__ ` as to be replaced by ` __get_pydantic_core_schema__ ` .
374
- - ✅ Mark ` __modify_schema__ ` as to be replaced by ` __get_pydantic_json_schema__ ` .
375
-
376
- The following code will be transformed:
377
-
378
- ``` py
379
- class SomeThing :
380
- @ classmethod
381
- def __get_validators__ (cls ):
382
- yield from []
383
- return
384
-
385
- @ classmethod
386
- def __modify_schema__ (
387
- cls , field_schema : Dict[str , Any], field : Optional[ModelField]
388
- ):
389
- if field:
390
- field_schema[' example' ] = " Weird example"
391
- ```
392
416
393
- Into:
394
-
395
- ``` py
396
- class SomeThing :
397
- @ classmethod
398
- # TODO [pydantic]: We couldn't refactor `__get_validators__`, please create the `__get_pydantic_core_schema__` manually.
399
- # Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
400
- def __get_validators__ (cls ):
401
- yield from []
402
- return
403
-
404
- @ classmethod
405
- # TODO [pydantic]: We couldn't refactor `__modify_schema__`, please create the `__get_pydantic_json_schema__` manually.
406
- # Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
407
- def __modify_schema__ (
408
- cls , field_schema : Dict[str , Any], field : Optional[ModelField]
409
- ):
410
- if field:
411
- field_schema[' example' ] = " Weird example"
412
- ```
413
417
414
418
---
415
419
0 commit comments