Skip to content

Commit

Permalink
raise TypeError in Field and PrivateAttr, fix #2059 (#5587)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Apr 25, 2023
1 parent a82045c commit f50ae9f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pydantic/fields.py
Expand Up @@ -113,7 +113,7 @@ def __init__(self, **kwargs: Any) -> None:
self.default_factory = kwargs.get('default_factory')

if self.default is not Undefined and self.default_factory is not None:
raise ValueError('cannot specify both default and default_factory')
raise TypeError('cannot specify both default and default_factory')

self.alias = kwargs.get('alias')
self.alias_priority = kwargs.get('alias_priority') or 2 if self.alias is not None else None
Expand Down Expand Up @@ -732,7 +732,7 @@ def PrivateAttr(
ValueError: If both `default` and `default_factory` are set.
"""
if default is not Undefined and default_factory is not None:
raise ValueError('cannot specify both default and default_factory')
raise TypeError('cannot specify both default and default_factory')

return ModelPrivateAttr(
default,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Expand Up @@ -1376,7 +1376,7 @@ class MyModel(BaseModel):


def test_two_defaults():
with pytest.raises(ValueError, match='^cannot specify both default and default_factory$'):
with pytest.raises(TypeError, match='^cannot specify both default and default_factory$'):

class Model(BaseModel):
a: int = Field(default=3, default_factory=lambda: 3)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_private_attributes.py
Expand Up @@ -194,7 +194,7 @@ def __init__(self):


def test_default_and_default_factory_used_error():
with pytest.raises(ValueError, match='cannot specify both default and default_factory'):
with pytest.raises(TypeError, match='cannot specify both default and default_factory'):
PrivateAttr(default=123, default_factory=lambda: 321)


Expand Down

0 comments on commit f50ae9f

Please sign in to comment.