Comments (8)
Then...
If validator
method has 2 params (parameter names doesn't matter, but numbers do):
from pydantic import BaseModel, validator
class User(BaseModel):
first_names: List[str]
last_names: List[str]
@validator("first_name", "last_name", each_item=True)
def names_must_not_be_empty_strings(cls, v):
if v:
return v
raise ValueError("Empty strings are not permitted")
Then we'd get:
from pydantic import BaseModel, validator, BeforeValidator
from typing_extensions import Annotated
from typing import List
# This function goes to the outer scope.
# I think we can get the `ClassDef` parent node with `LibCST.ParentProvider`. But I'm not sure...
def names_must_not_be_empty_strings(v):
if v:
return v
raise ValueError("Empty strings are not permitted")
class User(BaseModel):
# Check if the `AnnAssign.annotation` (List in this case) is a `Subscriptable` - and if it's, apply `BeforeValidator`.
first_names: List[Annotated[str, BeforeValidator(names_must_not_be_empty_strings)]]
last_names: List[Annotated[str, BeforeValidator(names_must_not_be_empty_strings)]]
Remember to import typing_extensions.Annotated
, and BeforeValidator
, and remove validator
.
Also, you might want to check function name collision on the module level - but I'm not sure if this is needed - but an xfail
would be nice.
Not finished yet... If the number of params is more than 2, then add a note on top of the method:
# TODO: We couldn't replace the validator automatically.
# Please check https://docs.pydantic.dev/latest/migration/#validator-and-root_validator-are-deprecated.
from bump-pydantic.
Well... It's feasible.
I guess what would be faster is to teach how to do it via comment.
from bump-pydantic.
What are you proposing here?
from bump-pydantic.
Rules
BP0xy: Replace each_item
- ✅ Replace
each_item
by something better.
The following code will be transformed:
from pydantic import BaseModel, validator
class User(BaseModel):
first_name: str
last_name: str
@validator("first_name", "last_name", each_item=True)
def names_must_not_be_empty_strings(cls, v):
if v:
return v
raise ValueError("Empty strings are not permitted")
Into:
from pydantic import BaseModel, field_validator
class User(BaseModel):
first_name: str
last_name: str
@field_validator("first_name", "last_name", # I am not sure what comes next...
from bump-pydantic.
I'm using a BeforeValidator
to implement each_item
in v2:
def validator_for_each_list_item(item: str):
...
class A(BaseModel):
my_list: List[Annotated[str, BeforeValidator(validator_for_each_list_item)]]
Not sure if this is doable with a codemod
from bump-pydantic.
teach how to do it via comment.
Given the source
above #70 (comment), what should the expected
be?
from bump-pydantic.
teach how to do it via comment.
Given the
source
above #70 (comment), what should theexpected
be?
Were last_name
and first_name
supposed to be str
, and not List[str]
?
from bump-pydantic.
Let's do List[str]
so last_names
and first_names
(plural).
from bump-pydantic.
Related Issues (20)
- Non-deterministic (and sometimes incorrect) finding of BaseModel classes HOT 3
- RecursionError when processing file with a large number of enums HOT 1
- Field(example=...) needs a codemod. HOT 1
- BP008: Not Handling conint(ge= , lt=) Correctly HOT 1
- Literal values "and" and "or" cause ParserSyntaxError HOT 10
- env -> validation_alias? HOT 1
- License info missing in classifiers and/or meta data HOT 3
- On Windows machines with >= 32 cores (more than 64 logical processors) execution fails HOT 1
- @root_validator will not updated to model_validator HOT 1
- [BUG]: bumb-pydantic doesnt change files HOT 2
- Set libcst dependency version HOT 1
- Rule BP002: Replace Config class by model_config attribute does not keep the arbitrary_types_allowed argument value
- uncovered case in validator->field_validator
- Issue with Literal["$"] being converted HOT 2
- Ignore .tox files HOT 2
- Add support for handling unannotated fields
- Examples in codemod files do not all work
- Log.txt file automatic creation in PWD is kind of an anti-feature HOT 2
- Not Transformed automatically for other parent class except BaseModel
- I have a big pile of improvements to upstream HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bump-pydantic.