Skip to content

App initialization utilizes import and decorator side effects #339

@Glutexo

Description

@Glutexo

The application initialization script uses and import with a side effect. The app.validators module is imported, which uses the jsonschema.draft4_format_checker decorator. It has a side effect of globally registering the validation function. That itself is not a very good thing, but it’s how this external library behaves.

Moreover this all happens not when the app is created (by the create_app function), but when the app module is imported. That makes the app module import itself having a side effect. No no no.

  • Remove the decorator side effect
  • Remove the import side effect

The decorators can be resolved by using them as a plain function. It doesn’t modify the original function in any way, it just registers it in the module.

# app.validators
@draft4_format_checker.checks('uuid')def verify_uuid_format(uuid_str):
    …

def register_validators():
    validations = (
        "uuid": verify_uuid_format
        …
    )
    for name, func in validations:
        validator = draft4_format_checker.checks(name)
        validator(function)
…
# appfrom app.validators import register_validatorsdef create_app(config_name):
    …
    register_validators()
    …
…

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions