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

Validation Code Error #7

Open
ptth222 opened this issue Nov 14, 2022 · 3 comments
Open

Validation Code Error #7

ptth222 opened this issue Nov 14, 2022 · 3 comments

Comments

@ptth222
Copy link
Collaborator

ptth222 commented Nov 14, 2022

I am using the following code to validate a manually built mwTab JSON:
validated_file, errors = mwtab.validator.validate_file(mwtabfile, verbose=True)

It produced an error like:

Error Log:
SCHEMA: Section "COLLECTION" does not match the allowed schema. dictionary changed size during iteration

This is obviously not a proper schema error and is a code error. I tracked it down to the validate_section_schema function which is trying to edit a dictionary while looping over it which results in the "RuntimeError: dictionary changed size during iteration".

This is an obvious problem that needs to be fixed, but there is also another question. Should "validate" be editing the object? In my opinion "validation" should not edit whatever is being validated, it should simply report all problems.

@hunter-moseley
Copy link
Member

hunter-moseley commented Nov 15, 2022 via email

@ptth222
Copy link
Collaborator Author

ptth222 commented Nov 17, 2022

I meant to do this right after lab meeting and forgot, but the offending function is in validator.py at line 227 the del line causing the error is 245.
https://github.com/MoseleyBioinformaticsLab/mwtab/blob/master/mwtab/validator.py

@ptth222
Copy link
Collaborator Author

ptth222 commented Feb 23, 2023

After some discussion we decided that there was supposed to be 2 modes in validation, one that doesn't clean and one that does. Currently, the 2 different modes aren't supported, but the offending del line was to enable some cleaning. If we want to add a cleaning and non-cleaning validation it needs to be propagated through more code than just this function. A quick fix would be to add a "cleaning" parameter and if True then do cleaning. I have the proposed fix below. Since cleaning will have a default value of False we don't even need to worry about replacing everywhere it is called.

def validate_section_schema(section, schema, section_key, cleaning=False):
    """Validate section of ``mwTab`` formatted file.
    :param section: Section of :class:`~mwtab.mwtab.MWTabFile`.
    :type section: :py:class:`collections.OrderedDict`
    :param schema: Schema definition.
    :type schema: :py:class:`~schema.schema`
    :param str section_key: Section key.
    :return: Validated section.
    :rtype: :py:class:`collections.OrderedDict`
    """
    schema_errors = list()

    keys_to_delete = []
    if section_key in ITEM_SECTIONS:
        for key in section.keys():
            if not section[key]:
                schema_errors.append("{}: Contains item \"{}\" with null value.".format(section_key, key))
                keys_to_delete.append(key)

    if cleaning:
        for key in keys_to_delete:
            del section[key]

    return schema.validate(section), schema_errors

ptth222 added a commit that referenced this issue Apr 12, 2024
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

No branches or pull requests

2 participants