Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

attrs – how to validate an instance of a Literal or None

This is what I have. I believe there are two problems here – the Literal and the None.

from attrs import frozen, field
from attrs.validators import instance_of

OK_ARGS = ['a', 'b']

@field
class MyClass:
    my_field: Literal[OK_ARGS] | None = field(validator=instance_of((Literal[OK_ARGS], None)))

Error:

TypeError: Subscripted generics cannot be used with class and instance checks

Edit: I’ve made a workaround with a custom validator. Not that pretty however:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def _validator_literal_or_none(literal_type):
    def inner(instance, attribute, value):
        if (isinstance(value, str) and (value in literal_type)) or (value is None):
            pass
        else:
            raise ValueError(f'You need to provide a None, or a string in this list: {literal_type}')
    return inner

>Solution :

You can’t do isinstance() checks on Literals/Nones and that’s what the is_instance Validator is using internally (it predates those typing features by far).

While we’ve resisted adding a complete implementation of the typing language due to its complexity, having one dedicated to such cases mind be worth exploring if you’d like to open an issue.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading