Skip to content

Commit 3b91441

Browse files
committed
rename methods
1 parent 5b35d36 commit 3b91441

17 files changed

+207
-183
lines changed

pydantic_core/core_schema.py

Lines changed: 76 additions & 69 deletions
Large diffs are not rendered by default.

src/validators/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ fn destructure_function_schema(schema: &PyDict) -> PyResult<(bool, &PyAny)> {
2121
let call: &PyAny = func.get_as_req(intern!(schema.py(), "call"))?;
2222
let func_type: &str = func.get_as_req(intern!(schema.py(), "type"))?;
2323
let is_model_instance_method = match func_type {
24-
"method" => true,
25-
"function" => false,
24+
"field" => true,
25+
"general" => false,
2626
_ => unreachable!(),
2727
};
2828
Ok((is_model_instance_method, call))

tests/benchmarks/complete_schema.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,31 @@ def wrap_function(input_value, validator, info):
110110
'schema': {
111111
'type': 'function',
112112
'mode': 'before',
113-
'function': {'type': 'function', 'call': append_func},
113+
'function': {'type': 'general', 'call': append_func},
114114
'schema': {'type': 'str'},
115115
}
116116
},
117117
'field_after': {
118118
'schema': {
119119
'type': 'function',
120120
'mode': 'after',
121-
'function': {'type': 'function', 'call': append_func},
121+
'function': {'type': 'general', 'call': append_func},
122122
'schema': {'type': 'str'},
123123
}
124124
},
125125
'field_wrap': {
126126
'schema': {
127127
'type': 'function',
128128
'mode': 'wrap',
129-
'function': {'type': 'function', 'call': wrap_function},
129+
'function': {'type': 'general', 'call': wrap_function},
130130
'schema': {'type': 'str'},
131131
}
132132
},
133133
'field_plain': {
134134
'schema': {
135135
'type': 'function',
136136
'mode': 'plain',
137-
'function': {'type': 'function', 'call': append_func},
137+
'function': {'type': 'general', 'call': append_func},
138138
}
139139
},
140140
},

tests/benchmarks/test_micro_benchmarks.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ def test_dont_raise_error(benchmark):
910910
def f(input_value, info):
911911
return input_value
912912

913-
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f}})
913+
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f}})
914914

915915
@benchmark
916916
def t():
@@ -922,7 +922,7 @@ def test_raise_error_value_error(benchmark):
922922
def f(input_value, info):
923923
raise ValueError('this is a custom error')
924924

925-
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f}})
925+
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f}})
926926

927927
@benchmark
928928
def t():
@@ -939,7 +939,7 @@ def test_raise_error_custom(benchmark):
939939
def f(input_value, info):
940940
raise PydanticCustomError('my_error', 'this is a custom error {foo}', {'foo': 'FOOBAR'})
941941

942-
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f}})
942+
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f}})
943943

944944
@benchmark
945945
def t():
@@ -1038,7 +1038,7 @@ def test_chain_list(benchmark):
10381038
{
10391039
'type': 'function',
10401040
'mode': 'plain',
1041-
'function': {'type': 'function', 'call': lambda v, info: Decimal(v)},
1041+
'function': {'type': 'general', 'call': lambda v, info: Decimal(v)},
10421042
},
10431043
],
10441044
}
@@ -1055,7 +1055,7 @@ def test_chain_function(benchmark):
10551055
'type': 'function',
10561056
'mode': 'after',
10571057
'schema': {'type': 'str'},
1058-
'function': {'type': 'function', 'call': lambda v, info: Decimal(v)},
1058+
'function': {'type': 'general', 'call': lambda v, info: Decimal(v)},
10591059
}
10601060
)
10611061
assert validator.validate_python('42.42') == Decimal('42.42')
@@ -1073,9 +1073,9 @@ def test_chain_two_functions(benchmark):
10731073
{
10741074
'type': 'function',
10751075
'mode': 'plain',
1076-
'function': {'type': 'function', 'call': lambda v, info: Decimal(v)},
1076+
'function': {'type': 'general', 'call': lambda v, info: Decimal(v)},
10771077
},
1078-
{'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': lambda v, info: v * 2}},
1078+
{'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': lambda v, info: v * 2}},
10791079
],
10801080
}
10811081
)
@@ -1093,10 +1093,10 @@ def test_chain_nested_functions(benchmark):
10931093
'type': 'function',
10941094
'schema': {'type': 'str'},
10951095
'mode': 'after',
1096-
'function': {'type': 'function', 'call': lambda v, info: Decimal(v)},
1096+
'function': {'type': 'general', 'call': lambda v, info: Decimal(v)},
10971097
},
10981098
'mode': 'after',
1099-
'function': {'type': 'function', 'call': lambda v, info: v * 2},
1099+
'function': {'type': 'general', 'call': lambda v, info: v * 2},
11001100
}
11011101
)
11021102
assert validator.validate_python('42.42') == Decimal('84.84')
@@ -1119,7 +1119,7 @@ def generator_gen_python(v, validator, info):
11191119

11201120
@pytest.mark.benchmark(group='generator')
11211121
def test_generator_python(benchmark):
1122-
schema = core_schema.function_wrap_schema(generator_gen_python, {'type': 'int'})
1122+
schema = core_schema.general_wrap_validation_callback(generator_gen_python, {'type': 'int'})
11231123
v = SchemaValidator(schema)
11241124
input_value = tuple(range(100))
11251125

tests/serializers/test_other.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ def test_chain():
1616

1717

1818
def test_function_plain():
19-
s = SchemaSerializer(core_schema.function_plain_schema(lambda v, info: v + 1))
19+
s = SchemaSerializer(core_schema.general_plain_validation_callback(lambda v, info: v + 1))
2020
# can't infer the type from plain function validators
2121
# insert_assert(plain_repr(s))
2222
assert plain_repr(s) == 'SchemaSerializer(serializer=Any(AnySerializer),slots=[])'
2323

2424

2525
def test_function_before():
26-
s = SchemaSerializer(core_schema.function_before_schema(lambda v, info: v + 1, core_schema.int_schema()))
26+
s = SchemaSerializer(
27+
core_schema.general_before_validation_callback(lambda v, info: v + 1, core_schema.int_schema())
28+
)
2729
# insert_assert(plain_repr(s))
2830
assert plain_repr(s) == 'SchemaSerializer(serializer=Int(IntSerializer),slots=[])'
2931

3032

3133
def test_function_after():
32-
s = SchemaSerializer(core_schema.function_after_schema(lambda v, info: v + 1, core_schema.int_schema()))
34+
s = SchemaSerializer(core_schema.general_after_validation_callback(lambda v, info: v + 1, core_schema.int_schema()))
3335
# insert_assert(plain_repr(s))
3436
assert plain_repr(s) == 'SchemaSerializer(serializer=Int(IntSerializer),slots=[])'
3537

tests/test_errors.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_pydantic_value_error_usage():
3636
def f(input_value, info):
3737
raise PydanticCustomError('my_error', 'this is a custom error {foo} {bar}', {'foo': 'FOOBAR', 'bar': 42})
3838

39-
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f}})
39+
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f}})
4040

4141
with pytest.raises(ValidationError) as exc_info:
4242
v.validate_python(42)
@@ -56,7 +56,7 @@ def test_pydantic_value_error_invalid_dict():
5656
def my_function(input_value, info):
5757
raise PydanticCustomError('my_error', 'this is a custom error {foo}', {(): 'foobar'})
5858

59-
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': my_function}})
59+
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': my_function}})
6060

6161
with pytest.raises(ValidationError) as exc_info:
6262
v.validate_python(42)
@@ -74,7 +74,7 @@ def test_pydantic_value_error_invalid_type():
7474
def f(input_value, info):
7575
raise PydanticCustomError('my_error', 'this is a custom error {foo}', [('foo', 123)])
7676

77-
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f}})
77+
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f}})
7878

7979
with pytest.raises(TypeError, match="argument 'context': 'list' object cannot be converted to 'PyDict'"):
8080
v.validate_python(42)
@@ -95,7 +95,7 @@ def validate(self, input_value, info):
9595
'type': 'function',
9696
'mode': 'plain',
9797
'metadata': {'instance': c},
98-
'function': {'type': 'function', 'call': c.validate},
98+
'function': {'type': 'general', 'call': c.validate},
9999
}
100100
)
101101
c.foo += 1
@@ -120,7 +120,7 @@ def validate(self, input_value, info):
120120
'type': 'function',
121121
'mode': 'after',
122122
'metadata': {'instance': c},
123-
'function': {'type': 'function', 'call': c.validate},
123+
'function': {'type': 'general', 'call': c.validate},
124124
'schema': {'type': 'str'},
125125
}
126126
)
@@ -144,7 +144,7 @@ def f(input_value, info):
144144
raise PydanticKnownError('finite_number')
145145

146146
v = SchemaValidator(
147-
{'type': 'function', 'mode': 'before', 'function': {'type': 'function', 'call': f}, 'schema': {'type': 'int'}}
147+
{'type': 'function', 'mode': 'before', 'function': {'type': 'general', 'call': f}, 'schema': {'type': 'int'}}
148148
)
149149

150150
with pytest.raises(ValidationError) as exc_info:
@@ -160,7 +160,7 @@ def f(input_value, info):
160160
raise PydanticKnownError('greater_than', {'gt': 42})
161161

162162
v = SchemaValidator(
163-
{'type': 'function', 'mode': 'before', 'function': {'type': 'function', 'call': f}, 'schema': {'type': 'int'}}
163+
{'type': 'function', 'mode': 'before', 'function': {'type': 'general', 'call': f}, 'schema': {'type': 'int'}}
164164
)
165165

166166
with pytest.raises(ValidationError) as exc_info:
@@ -309,7 +309,7 @@ def test_pydantic_value_error_plain(py_and_json: PyAndJson):
309309
def f(input_value, info):
310310
raise PydanticCustomError
311311

312-
v = py_and_json({'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f}})
312+
v = py_and_json({'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f}})
313313
with pytest.raises(TypeError, match='missing 2 required positional arguments'):
314314
v.validate_test('4')
315315

@@ -328,7 +328,7 @@ def f(input_value, info):
328328
'type': 'function',
329329
'schema': {'type': 'int'},
330330
'mode': 'after',
331-
'function': {'type': 'function', 'call': f},
331+
'function': {'type': 'general', 'call': f},
332332
},
333333
}
334334
)

tests/test_isinstance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def omit(v, info):
7878
else:
7979
return v
8080

81-
v = py_and_json(core_schema.function_plain_schema(omit))
81+
v = py_and_json(core_schema.general_plain_validation_callback(omit))
8282
assert v.validate_test('foo') == 'foo'
8383
assert v.isinstance_test('foo') is True
8484

tests/test_schema_functions.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,39 @@ def args(*args, **kwargs):
8686
{'type': 'dict', 'keys_schema': {'type': 'str'}, 'values_schema': {'type': 'int'}},
8787
),
8888
(
89-
core_schema.function_before_schema,
89+
core_schema.general_before_validation_callback,
9090
args(val_function, {'type': 'int'}),
9191
{
9292
'type': 'function',
9393
'mode': 'before',
94-
'function': {'type': 'function', 'call': val_function},
94+
'function': {'type': 'general', 'call': val_function},
9595
'schema': {'type': 'int'},
9696
},
9797
),
9898
(
99-
core_schema.function_after_schema,
99+
core_schema.general_after_validation_callback,
100100
args(val_function, {'type': 'int'}),
101101
{
102102
'type': 'function',
103103
'mode': 'after',
104-
'function': {'type': 'function', 'call': val_function},
104+
'function': {'type': 'general', 'call': val_function},
105105
'schema': {'type': 'int'},
106106
},
107107
),
108108
(
109-
core_schema.function_wrap_schema,
109+
core_schema.general_wrap_validation_callback,
110110
args(val_function, {'type': 'int'}),
111111
{
112112
'type': 'function',
113113
'mode': 'wrap',
114-
'function': {'type': 'function', 'call': val_function},
114+
'function': {'type': 'general', 'call': val_function},
115115
'schema': {'type': 'int'},
116116
},
117117
),
118118
(
119-
core_schema.function_plain_schema,
119+
core_schema.general_plain_validation_callback,
120120
args(val_function),
121-
{'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': val_function}},
121+
{'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': val_function}},
122122
),
123123
(
124124
core_schema.with_default_schema,

tests/test_typing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ def test_schema_typing() -> None:
9494
schema: CoreSchema = {
9595
'type': 'function',
9696
'mode': 'wrap',
97-
'function': {'type': 'function', 'call': wrap_validator},
97+
'function': {'type': 'general', 'call': wrap_validator},
9898
'schema': {'type': 'str'},
9999
}
100100
SchemaValidator(schema)
101-
schema: CoreSchema = {'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': validator}}
101+
schema: CoreSchema = {'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': validator}}
102102
SchemaValidator(schema)
103103
schema: CoreSchema = {
104104
'ref': 'Branch',
@@ -165,15 +165,15 @@ def test_correct_function_signature() -> None:
165165
def my_validator(value: Any, info: Any) -> str:
166166
return str(value)
167167

168-
v = SchemaValidator(core_schema.function_plain_schema(my_validator))
168+
v = SchemaValidator(core_schema.general_plain_validation_callback(my_validator))
169169
assert v.validate_python(1) == '1'
170170

171171

172172
def test_wrong_function_signature() -> None:
173173
def wrong_validator(value: Any) -> Any:
174174
return value
175175

176-
v = SchemaValidator(core_schema.function_plain_schema(wrong_validator)) # type: ignore
176+
v = SchemaValidator(core_schema.general_plain_validation_callback(wrong_validator)) # type: ignore
177177

178178
# use this instead of pytest.raises since pyright complains about input when pytest isn't installed
179179
try:

tests/test_validation_context.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def f(input_value, info):
1010
return input_value + f'| context: {info.context}'
1111

1212
v = py_and_json(
13-
{'type': 'function', 'mode': 'after', 'function': {'type': 'function', 'call': f}, 'schema': {'type': 'str'}}
13+
{'type': 'function', 'mode': 'after', 'function': {'type': 'general', 'call': f}, 'schema': {'type': 'str'}}
1414
)
1515

1616
assert v.validate_test('foobar') == 'foobar| context: None'
@@ -24,7 +24,7 @@ def f(input_value, info):
2424
return input_value
2525

2626
v = py_and_json(
27-
{'type': 'function', 'mode': 'before', 'function': {'type': 'function', 'call': f}, 'schema': {'type': 'str'}}
27+
{'type': 'function', 'mode': 'before', 'function': {'type': 'general', 'call': f}, 'schema': {'type': 'str'}}
2828
)
2929
mutable_context = {}
3030
assert v.validate_test('foobar', None, mutable_context) == 'foobar'
@@ -44,8 +44,8 @@ def f2(input_value, info):
4444
{
4545
'type': 'typed-dict',
4646
'fields': {
47-
'f1': {'schema': {'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f1}}},
48-
'f2': {'schema': {'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f2}}},
47+
'f1': {'schema': {'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f1}}},
48+
'f2': {'schema': {'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f2}}},
4949
},
5050
}
5151
)
@@ -61,7 +61,7 @@ def f(input_value, validator, info):
6161
return validator(input_value) + f'| context: {info.context}'
6262

6363
v = py_and_json(
64-
{'type': 'function', 'mode': 'wrap', 'function': {'type': 'function', 'call': f}, 'schema': {'type': 'str'}}
64+
{'type': 'function', 'mode': 'wrap', 'function': {'type': 'general', 'call': f}, 'schema': {'type': 'str'}}
6565
)
6666

6767
assert v.validate_test('foobar') == 'foobar| context: None'
@@ -76,7 +76,7 @@ def f(input_value, validator, info):
7676
return validator(input_value)
7777

7878
v = py_and_json(
79-
{'type': 'function', 'mode': 'wrap', 'function': {'type': 'function', 'call': f}, 'schema': {'type': 'str'}}
79+
{'type': 'function', 'mode': 'wrap', 'function': {'type': 'general', 'call': f}, 'schema': {'type': 'str'}}
8080
)
8181

8282
assert v.validate_python('foobar', None, {}) == 'foobar'
@@ -112,8 +112,8 @@ def f2(input_value, info):
112112
{
113113
'type': 'typed-dict',
114114
'fields': {
115-
'f1': {'schema': {'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f1}}},
116-
'f2': {'schema': {'type': 'function', 'mode': 'plain', 'function': {'type': 'function', 'call': f2}}},
115+
'f1': {'schema': {'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f1}}},
116+
'f2': {'schema': {'type': 'function', 'mode': 'plain', 'function': {'type': 'general', 'call': f2}}},
117117
},
118118
}
119119
)

tests/validators/test_arguments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ def test_internal_error(py_and_json: PyAndJson):
623623
'schema': {
624624
'type': 'function',
625625
'mode': 'plain',
626-
'function': {'type': 'function', 'call': double_or_bust},
626+
'function': {'type': 'general', 'call': double_or_bust},
627627
},
628628
},
629629
],

0 commit comments

Comments
 (0)