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

App initialization utilizes import and decorator side effects #339

Open
2 tasks
Glutexo opened this issue Jul 10, 2019 · 0 comments
Open
2 tasks

App initialization utilizes import and decorator side effects #339

Glutexo opened this issue Jul 10, 2019 · 0 comments

Comments

@Glutexo
Copy link
Collaborator

Glutexo commented Jul 10, 2019

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()
    …
…
@Glutexo Glutexo assigned Glutexo and unassigned Glutexo Jul 10, 2019
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

1 participant