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

KeyError during import when running with python optimisation level of 2 #557

Closed
2 of 3 tasks
sujanadiga opened this issue Sep 17, 2020 · 6 comments
Closed
2 of 3 tasks
Milestone

Comments

@sujanadiga
Copy link

Used Cerberus version / latest commit: 1.3.2

  • I have the capacity to improve the docs when my problem is solved.
  • I have the capacity to submit a patch when a bug is identified.
  • My question does not concern a practical use-case that I can't figure out
    to solve.

Use-case abstract

Firstly, this is a great package, this has saved a lot of my time in handling JSON payload validations. Thanks.

I'm using this in a Flask application and in development environment everything works as expected. However, when I try this on staging environment(where I run with python optimisation level of 2) import fails with a KeyError


Bug report

Python versions tried on: 3.8.5 and 3.6.6

To reproduce, run:

$ python -OO -c 'import cerberus'

Error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "cerberus/__init__.py", line 15, in <module>
    from cerberus.validator import DocumentError, Validator
  File "cerberus/validator.py", line 1615, in <module>
    Validator = InspectedValidator('Validator', (BareValidator,), {})
  File "cerberus/validator.py", line 1566, in __init__
    x = cls.validation_rules['check_with']['oneof']
KeyError: 'oneof'

Looks like this is because doc strings won't be available when running with optimisation level of 2 and I can see use of doc strings in __get_rule_schema and few other functions.

At least for __get_rule_schema use case, it can be obtained from a dictionary instead of relying on doc strings, not sure of other use cases. Not sure how should I proceed with this. Any suggestions?

@funkyfuture
Copy link
Member

this should work for your application:

if __debug__:
    from cerberus import Validator
else:
    from cerberus import UnconcernedValidator as Validator

the latter import isn't depending on a valid schema and hence not the docstrings.

and i think we can add this logic to the package's __init__ module with the next bug fix release.

@funkyfuture funkyfuture added this to the 1.3.3 milestone Sep 24, 2020
@sujanadiga
Copy link
Author

@funkyfuture, from cerberus import UnconcernedValidator as Validator too doesn't work with optimisation level 2

Fails with the same error. Import fails when executing https://github.com/pyeve/cerberus/blob/1.3.x/cerberus/__init__.py#L15

@funkyfuture
Copy link
Member

thanks for trying out my guess. i directly pushed a fix to the 1.3.x branch and i'd be happy if you could try it out in your use-case. with a REPL it worked.

@sujanadiga
Copy link
Author

sujanadiga commented Sep 28, 2020

@funkyfuture, import is working fine on 1.3.x branch with python optimisation level 2. Thank you.

@funkyfuture
Copy link
Member

thanks for your feedback.

@funkyfuture
Copy link
Member

sorry, it turned out that the fix for this issue caused more trouble than it solved (see #567).

i'm gonna reverse the fix and instead let Cerberus raise a runtime error when Python is invoked with optimization level 2. the architecture of the 1.x series is just depending from doc strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants