Skip to content

Commit b55f610

Browse files
authored
community[patch]: Add linter to prevent further usage of root_validator and validator (langchain-ai#24613)
This linter is meant to move development to use __init__ instead of root_validator and validator. We need to investigate whether we need to lint some of the functionality of Field (e.g., `lt` and `gt`, `alias`) `alias` is the one that's most popular: (community) ➜ community git:(eugene/add_linter_to_community) ✗ git grep " Field(" | grep "alias=" | wc -l 144 (community) ➜ community git:(eugene/add_linter_to_community) ✗ git grep " Field(" | grep "ge=" | wc -l 10 (community) ➜ community git:(eugene/add_linter_to_community) ✗ git grep " Field(" | grep "gt=" | wc -l 4
1 parent 4585eae commit b55f610

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

libs/community/scripts/check_pydantic.sh

+24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ fi
1313

1414
repository_path="$1"
1515

16+
# Check that we are not using features that cannot be captured via init.
17+
# pre-init is a custom decorator that we introduced to capture the same semantics
18+
# as @root_validator(pre=False, skip_on_failure=False) available in pydantic 1.
19+
count=$(git grep -E '(@root_validator)|(@validator)|(@pre_init)' -- "*.py" | wc -l)
20+
# PRs that increase the current count will not be accepted.
21+
# PRs that decrease update the code in the repository
22+
# and allow decreasing the count of are welcome!
23+
current_count=336
24+
25+
if [ "$count" -gt "$current_count" ]; then
26+
echo "The PR seems to be introducing new usage of @root_validator and/or @field_validator."
27+
echo "git grep -E '(@root_validator)|(@validator)' | wc -l returned $count"
28+
echo "whereas the expected count should be equal or less than $current_count"
29+
echo "Please update the code to instead use __init__"
30+
echo "For examples, please see: "
31+
echo "https://gist.github.com/eyurtsev/d1dcba10c2f35626e302f1b98a0f5a3c "
32+
echo "This linter is here to make sure that its easier to upgrade pydantic in the future."
33+
exit 1
34+
elif [ "$count" -lt "$current_count" ]; then
35+
echo "Please update the $current_count variable in ./scripts/check_pydantic.sh to $count"
36+
exit 1
37+
fi
38+
39+
1640
# Search for lines matching the pattern within the specified repository
1741
result=$(git -C "$repository_path" grep -En '^import pydantic|^from pydantic')
1842

0 commit comments

Comments
 (0)