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

Undefined variable in Custom Coercers snippet from documentation #285

6 tasks done
gilbsgilbs opened this issue Nov 22, 2016 · 1 comment
6 tasks done


Copy link

gilbsgilbs commented Nov 22, 2016

Used Cerberus version: 1.0.1

Use-case abstract

I want to be able to set finer error messages when defining custom coercers.

Support request / Bug report

Here is the excerpt from the documentation (

class MyNormalizer(Validator):
        def __init__(self, multiplier, *args, **kwargs):
            super(MyNormalizer, self).__init__(*args, **kwargs)
            self.multiplier = multiplier

        def _normalize_coerce_multiply(self, value):
                return value * self.multiplier
            except Exception as e:
                self._error(field, errors.COERCION_FAILED, e)

This code snippet can't work since field variable in the last line is undefined. We either need to pass the field argument to the _normalize_coerce_multiply method or avoid the try/except. Correct me if I am wrong, but I reckon that the try/except is already done in the __normalize_coerce method which would mean that it's just a documentation issue.

Ideally, I would like to be able to define custom error messages in coercers, and generally be finer in the way that I handle my error messages using Cerberus. I have already encountered the same kind of limitation with 1.x custom types. In 0.9, there was no limitation in this regard (field argument was passed to the child method, but that was removed in 1.0 for some reason). This actually prevents me from migrating to Cerberus 1.x in one of my projects (I relied on _error method in a custom type). I think it would be a good idea to pass the field argument everywhere (types, coercers, validators, …) if requested by the child method (maybe using argspec or something similar to prevent breaking changes). But that's maybe a bit out of the scope of this issue.

Many thanks for the amazing work :) .


Copy link

yes, it's a docs issue. i guess the example was just adjusted to fit the methods' signatures over the time. the try/except-block is redundant.

this has nothing to do with the changed logic how types are checked. (if you want a type check with a custom error, do a custom rule.)

the normalization part could be smarter, sure.

@nicolaiarocci nicolaiarocci added this to the Unreleased milestone Nov 23, 2016
gilbsgilbs added a commit to gilbsgilbs/cerberus that referenced this issue Nov 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants