Skip to content

Report a clearer error when a transformer class is given - #71

Merged
tcalmant merged 1 commit into
masterfrom
fix-v2-transformer-instance-check
Aug 12, 2026
Merged

Report a clearer error when a transformer class is given#71
tcalmant merged 1 commit into
masterfrom
fix-v2-transformer-instance-check

Conversation

@tcalmant

Copy link
Copy Markdown
Owner

Summary

Giving a transformer class instead of an instance, i.e. load(fd, MyTransformer)
instead of load(fd, MyTransformer()), made the parser call
create_instance() on the class itself. The class description was then given
as the self argument, and the call failed with:

TypeError: create_instance() missing 1 required positional argument: 'classdesc'

which points at the internals of the parser instead of the call of the user.

v2 and v3 now check that argument up front and explain what to do:

TypeError: Transformers must be given as instances, not as classes:
got the class MyTransformer, did you mean MyTransformer() ?

The check only rejects classes: transformers are duck-typed, so an instance
which does not inherit from ObjectTransformer is still accepted, as before.

Test plan

  • TestTransformersArgument added to the v2 and v3 test suites: it checks
    that load() and loads() both reject a class and that the message names
    the transformer, and that instances are still accepted, with and without
    inheriting from ObjectTransformer.
  • Full test suite green.

Fixes #54

Giving a transformer class instead of an instance made the parser call create_instance() on the class itself, with the class description as the self argument, and failed with a confusing missing argument error. v2 and v3 now check that argument up front. Fixes #54.

Signed-off-by: Thomas Calmant <thomas.calmant@gmail.com>
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 92.256% (+0.02%) from 92.232% — fix-v2-transformer-instance-check into master

@tcalmant
tcalmant merged commit 363ca87 into master Aug 12, 2026
15 checks passed
@tcalmant
tcalmant deleted the fix-v2-transformer-instance-check branch August 12, 2026 14:50
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.

Custom transformer - create_instance invoked incorrectly?

2 participants