File tree Expand file tree Collapse file tree 3 files changed +12
-5
lines changed Expand file tree Collapse file tree 3 files changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -264,7 +264,10 @@ def collect_model_fields( # noqa: C901
264
264
# to make sure the decorators have already been built for this exact class
265
265
decorators : DecoratorInfos = cls .__dict__ ['__pydantic_decorators__' ]
266
266
if ann_name in decorators .computed_fields :
267
- raise ValueError ("you can't override a field with a computed field" )
267
+ raise TypeError (
268
+ f'Field { ann_name !r} of class { cls .__name__ !r} overrides symbol of same name in a parent class. '
269
+ 'This override with a computed_field is incompatible.'
270
+ )
268
271
fields [ann_name ] = field_info
269
272
270
273
if typevars_map :
Original file line number Diff line number Diff line change @@ -1421,9 +1421,11 @@ class Child(Parent):
1421
1421
def a(self) -> str:
1422
1422
return 'new a'
1423
1423
1424
- except ValueError as e:
1425
- print(repr(e))
1426
- #> ValueError("you can't override a field with a computed field")
1424
+ except TypeError as e:
1425
+ print(e)
1426
+ '''
1427
+ Field 'a' of class 'Child' overrides symbol of same name in a parent class. This override with a computed_field is incompatible.
1428
+ '''
1427
1429
```
1428
1430
1429
1431
Private properties decorated with `@computed_field` have `repr=False` by default.
Original file line number Diff line number Diff
74AC
line change @@ -774,7 +774,9 @@ def test_computed_field_override_raises():
774
774
class Model (BaseModel ):
775
775
name : str = 'foo'
776
776
777
- with pytest .raises (ValueError , match = "you can't override a field with a computed field" ):
777
+ with pytest .raises (
778
+ TypeError , match = "Field 'name' of class 'SubModel' overrides symbol of same name in a parent class"
779
+ ):
778
780
779
781
class SubModel (Model ):
780
782
@computed_field
You can’t perform that action at this time.
0 commit comments