Skip to content

Commit dbcdff4

Browse files
committed
Add a test to make sure missing imports generate the right error message.
This verifies we can close #283. Fixes #283
1 parent 80fca1b commit dbcdff4

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

tests/test_server_config_model.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_ThingConfig():
2424
assert direct.kwargs == {}
2525
assert direct.thing_slots == {}
2626

27-
with pytest.raises(ThingImportFailure, match="No module named 'missing.*'"):
27+
with pytest.raises(ThingImportFailure, match="No module named 'missing'"):
2828
ThingConfig(cls="missing.module")
2929

3030

@@ -114,8 +114,15 @@ def test_ThingServerConfig():
114114
def test_unimportable_modules():
115115
"""Test that unimportable modules raise errors as expected."""
116116

117-
with pytest.raises(ThingImportFailure, match="No module named 'missing.*'"):
117+
with pytest.raises(ThingImportFailure, match="No module named 'missing'"):
118+
# If a module is missing, the error should make that clear.
119+
# Note that the error message changed with Pydantic 2.13.
118120
ThingConfig(cls="missing.module:object")
121+
with pytest.raises(ThingImportFailure, match="No module named 'missing_module'"):
122+
# Check that, if a module has a broken import, the error refers
123+
# to that missing import and doesn't suggest the target module
124+
# is missing. This was an upstream bug, fixed in Pydantic 2.13
125+
ThingConfig(cls="tests.unimportable.missing_import:object")
119126

120127
with pytest.raises(
121128
ThingImportFailure,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""A module that fails to import because of a missing import.
2+
3+
This is to help test ImportString error handling.
4+
See test_server_config_model.py.
5+
"""
6+
7+
from missing_module import missing_submodule
8+
9+
10+
missing_submodule.missing_function()

0 commit comments

Comments
 (0)