Skip to content

Should Port.validate fail if required=False and receives None which does not match valid_type? #226

@sphuber

Description

@sphuber

Original issue reported in aiida-common-workflows:

Consider the following example of a simple port that is not required and expects a string:

In [1]: from plumpy import InputPort

In [3]: port = InputPort('test', valid_type=str, required=False)

In [4]: port.validate('string')

In [5]: port.validate(None)
Out[5]: plumpy.ports.PortValidationError("Error occurred validating port 'test': value 'test' is not of the right type. Got '<class 'NoneType'>', expected '<class 'str'>'")

When we validate None as a value, we hit the validation error, just as in the EOS workchain. Except that None should be a perfectly legal value if the port is optional. It seems weird to raise for this. The only reason I can think off to keep the current behavior is that it might be necessary to have an optional port but where an explicit None value is incorrect and so if None is passed explicitly (instead of the input being omitted entirely) you want the port to raise. But I am wondering if this is not an edge-case and in that case the develop should simply explicitly check for None in a validator or the process body and ignore it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions