How to mock an attr.ib validator

Viewed 234

I have an attrs class:

@attr.s    
class Example:
    my_int = attr.ib(validator=attr.validator.instance_of(MyComplexType))

I need to mock this validator.

2 Answers

Since everything after initialization is baked and immutable, mocking is not gonna work I think. However you you can temporarily disable validators using attr.set_run_validators().

You might try subclassing Example and overriding my_int with your mock validator, assuming you can control the instantiation of the object from your test.

Related