Skip to content

Added new error message and test for dict[str, Any] generation#22

Merged
joshvote merged 1 commit intomainfrom
dict_value_error_message
May 7, 2026
Merged

Added new error message and test for dict[str, Any] generation#22
joshvote merged 1 commit intomainfrom
dict_value_error_message

Conversation

@LachlanJW
Copy link
Copy Markdown
Contributor

v0.3.3 added native dict generation, which is great, but silently breaks when the dict value type is unresolvable by assertical (e.g. Any, Path, object). Instead of a clear error, generation crashes deep in the stack:

Exception: Type None does not inherit from one of dict_keys([...])

This is because enumerate_class_properties sets second_type_to_generate = None when the value type can't be resolved, but generate_class_instance then passes None to generate_member, which calls generate_class_instance(None, ...) causing the crash.

This was already handled cleanly by keys:

if member.type_to_generate is None:
if not (optional_is_none and member.is_optional):
raise Exception(f"...cannot be generated")

Now added the error message to values to make it a little easier to trace.

The reason why I think this is useful is from a cryptic issue I found after this upgrade in another repo. When generating a dict[str, Any] prior to assertical v0.3.3, the assertical_extensions fixture registered register_value_generator(dict, lambda _: {}), which intercepted any dict field and returned {} before assertical inspected the type arguments. This masked the fact that Any is not a type assertical can generate. Now these are no longer intercepted and fail with the error described above. This new message would have made things easier!

@LachlanJW LachlanJW requested review from joshvote and mikejturner and removed request for mikejturner February 27, 2026 04:45
@github-actions
Copy link
Copy Markdown

✅ Code coverage on dict_value_error_message is at 91% (compared to main being at 91%)

🎉 No files have reduced coverage 🎉

Full Coverage Report

File Details
src/assertical/__init__.py ✅ 100% → 100%
src/assertical/asserts/__init__.py ✅ 100% → 100%
src/assertical/asserts/generator.py ✅ 100% → 100%
src/assertical/asserts/pandas.py ✅ 88% → 88%
src/assertical/asserts/time.py ✅ 95% → 95%
src/assertical/asserts/type.py ✅ 92% → 92%
src/assertical/fake/__init__.py ✅ 100% → 100%
src/assertical/fake/asyncio.py ✅ 100% → 100%
src/assertical/fake/generator.py ✅ 94% → 94%
src/assertical/fake/http.py ✅ 71% → 71%
src/assertical/fixtures/__init__.py ✅ 100% → 100%
src/assertical/fixtures/environment.py ✅ 100% → 100%
src/assertical/fixtures/fastapi.py ✅ 97% → 97%
src/assertical/fixtures/generator.py ✅ 100% → 100%
src/assertical/snapshot.py ✅ 100% → 100%
TOTAL ✅ 91% → 91%

@joshvote joshvote merged commit 580c5e5 into main May 7, 2026
9 checks passed
@joshvote joshvote deleted the dict_value_error_message branch May 7, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants