Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception error message #301

Merged
merged 3 commits into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Extend type hints in `model_bakery.recipe` module, make `Recipe` class generic [PR #292](https://github.com/model-bakers/model_bakery/pull/292)
- Explicitly add _fill_optional parameters to baker.make and baker.prepare to aid IDE autocomplete function. [PR #264](https://github.com/model-bakers/model_bakery/pull/264)
- Fixed errors with reverse M2M relationships [PR #299](https://github.com/model-bakers/model_bakery/pull/299)
- Improve exception message for unknown field types [PR #301](https://github.com/model-bakers/model_bakery/pull/301)

### Removed

Expand Down
4 changes: 3 additions & 1 deletion model_bakery/baker.py
Expand Up @@ -672,7 +672,9 @@ def generate_value(self, field: Field, commit: bool = True) -> Any:
elif field.__class__ in self.type_mapping:
generator = self.type_mapping[field.__class__]
else:
raise TypeError("%s is not supported by baker." % field.__class__)
raise TypeError(
f"field {field.name} type {field.__class__} is not supported by baker."
)

# attributes like max_length, decimal_places are taken into account when
# generating the value.
Expand Down
1 change: 1 addition & 0 deletions tests/test_baker.py
Expand Up @@ -578,6 +578,7 @@ def test_unsupported_model_raises_an_explanatory_exception(self):
assert False, "Should have raised a TypeError"
except TypeError as e:
assert "not supported" in repr(e)
assert "field unsupported_field" in repr(e)


@pytest.mark.django_db
Expand Down