Skip to content

Improve dictionary construction errors - #35

Merged
tuckermcclure merged 3 commits into
mainfrom
tucker/fix-error-2
Aug 24, 2026
Merged

tuckermcclure merged 3 commits into
mainfrom
tucker/fix-error-2

Conversation

@tuckermcclure

Copy link
Copy Markdown
Member

Summary

This PR improves the error reported when from_dict cannot construct a requested type from a dictionary.

Previously, the final error printed the complete dictionary and always suggested adding a type key. That suggestion could be misleading when a type tag had already resolved to a concrete type and construction subsequently failed because its fields did not match.

The new error explains the construction strategies PortableStructs considered and identifies the relevant key mismatch.

Changes

  • Report the requested type and supplied dictionary keys.
  • Explain when no keyword constructor accepts the supplied keys.
  • Report expected, missing, and unexpected fields for positional construction.
  • Avoid printing the complete dictionary, which may be large or contain sensitive values.
  • Suggest identifying a concrete subtype only when the requested type is Any or abstract.
  • Safely handle dictionary keys whose conversion to Symbol fails.
  • Use the same key analysis for keyword-constructor detection, positional matching, and error reporting.

This does not add keywords or change the from_dict extension interface. Construction failures remain ErrorExceptions.

Examples

For a concrete type with mismatched keys:

Could not construct a ConcreteConfig from a dictionary.

Supplied keys: ("name", "extra")
No keyword constructor accepts the supplied keys.
Positional construction requires the supplied keys to match the fields exactly.
Expected fields: (:name, :count)
Missing fields: (:count,)
Unexpected keys: ("extra",)

If a type tag resolves to ConcreteConfig but its dictionary contains the wrong fields, the error describes that concrete construction failure:

Could not construct a ConcreteConfig from a dictionary.

Supplied keys: ("wrong",)
No keyword constructor accepts the supplied keys.
Positional construction requires the supplied keys to match the fields exactly.
Expected fields: (:name, :count)
Missing fields: (:name, :count)
Unexpected keys: ("wrong",)

It does not incorrectly suggest adding another type tag.

For an untagged abstract type, the error retains a useful subtype suggestion:

Could not construct a VehicleConfig from a dictionary.

Supplied keys: ("name",)
No keyword constructor accepts the supplied keys.
Positional construction requires the supplied keys to match the fields exactly.
Expected fields: ()
Unexpected keys: ("name",)
If this dictionary represents a concrete subtype, identify it with a "type" key.

Testing

Added coverage for:

  • Missing and unexpected fields on concrete types.
  • Tagged concrete types with mismatched fields.
  • Untagged abstract types.
  • Keys whose conversion to Symbol throws.
  • Preservation of the existing ErrorException type.

The complete PortableStructs test suite passes:

julia --project=test -e 'import Pkg; Pkg.test("PortableStructs")'

@tuckermcclure tuckermcclure linked an issue Aug 22, 2026 that may be closed by this pull request
@tuckermcclure
tuckermcclure merged commit 2f44d6f into main Aug 24, 2026
6 checks passed
@tuckermcclure
tuckermcclure deleted the tucker/fix-error-2 branch August 24, 2026 17:59
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.

Make better error messages

2 participants