question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError during import when running with python optimisation level of 2

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
funkyfuturecommented, May 5, 2021

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.

0reactions
funkyfuturecommented, Sep 28, 2020

thanks for your feedback.

Read more comments on GitHub >

github_iconTop Results From Across the Web

KeyError in Gurobi Python for multidimensional variables
But I get a KeyError for the constraint. KeyError: (('Detroit', 'Boston'), 'Pencils'). I cannot see what is the problem with this constraint.
Read more >
How to fix Python KeyError Exceptions in simple steps?
Know about Python KeyError Exception. And learn how to handle exceptions in Python. A detailed guide to Errors and Exceptions in Python.
Read more >
logging.config — Logging configuration — Python 3.11.1 ...
If an error is encountered during configuration, this function will raise a ValueError , TypeError , AttributeError or ImportError with a suitably descriptive ......
Read more >
When setting up python with snowpark connection found here
KeyError Traceback (most recent call last). Input In [44], in <cell line: 5>(). 1 from snowflake.snowpark import Session. 2 import os.
Read more >
MongoDB Python Connection
from pymongo import MongoClient def get_database(): # Provide the mongodb atlas ... Although MongoDB gets the entire data, we get a Python 'KeyError'...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found