-1
UPDATEABLE_FIELDS_KEYS = dict.fromkeys(["subject", "target"]).keys()

update_schema = {
    "internal_id": {
        "required": True,
        "type": "string",
        "regex": UUIDV4,
        "empty": False,
        "excludes": "message_code",
    },
    "message_code": {
        "required": True,
        "type": "string",
        "empty": False,
        "excludes": "internal_id",
        "coerce": to_uppercase_fn,
    },
    "fields": {
        "required": True,
        "type": "dict",
        "keysrules": {
            "required": False,
            "type": "string",
            "allowed": UPDATEABLE_FIELDS_KEYS,
        },
    },
}

If I validate something like this:

data = {
  "message_code": "ABC123",
  "fields": {
    "this_one_not_valid": "some words",
    "subject": "a thing",
    "target": "something else"
  }
}

validator = Validator(update_schema, allow_unknown=False)
validator(data)
validator.errors

I get this error: TypeError: cannot pickle 'dict_keys' object

Am I doing something wrong here? It works acceptably in the "correct" cases, but not when I provide an invalid key name.

Stacktrace, some words altered but the code provided above is otherwise the code I am using.

  File "/Users/c/Development/ub/app/blueprints/p/p.py", line 201, in update_existing
    return jsonify(validator.errors), 422
  File "/Users/c/.pyenv/versions/3.8.1/envs/ub/lib/python3.8/site-packages/cerberus/validator.py", line 464, in errors
    return self.error_handler(self._errors)
  File "/Users/c/.pyenv/versions/3.8.1/envs/ub/lib/python3.8/site-packages/cerberus/errors.py", line 493, in __call__
    self.extend(errors)
  File "/Users/c/.pyenv/versions/3.8.1/envs/ub/lib/python3.8/site-packages/cerberus/errors.py", line 397, in extend
    self.add(error)
  File "/Users/c/.pyenv/versions/3.8.1/envs/ub/lib/python3.8/site-packages/cerberus/errors.py", line 510, in add
    error = deepcopy(error)
  File "/Users/c/.pyenv/versions/3.8.1/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/Users/c/.pyenv/versions/3.8.1/lib/python3.8/copy.py", line 269, in _reconstruct
    state = deepcopy(state, memo)
  File "/Users/c/.pyenv/versions/3.8.1/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/c/.pyenv/versions/3.8.1/lib/python3.8/copy.py", line 229, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/c/.pyenv/versions/3.8.1/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/c/.pyenv/versions/3.8.1/lib/python3.8/copy.py", line 229, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/c/.pyenv/versions/3.8.1/lib/python3.8/copy.py", line 161, in deepcopy
    rv = reductor(4)
TypeError: cannot pickle 'dict_keys' object
3
  • please show the full stacktrace and actual code you're using -- you're probably passing foo.keys() somewhere where you mean to pass list(foo) instead Jun 18, 2020 at 20:28
  • Additional data added, but this code is essentially what I'm running, with some variables changed to protect their identity. 🕵🏻‍♂️
    – Chris
    Jun 18, 2020 at 20:43
  • well the code you've shared does not reproduce the error. please post a MCVE Jun 18, 2020 at 20:53

1 Answer 1

3

The only way I could see this being actually broken is my guess above about UPDATEABLE_FIELDS_KEYS

your code is using some_dict.keys() to set that value which returns a keys view (dict_keys type in python)

adjusting the code in your question if I change:

-UPDATEABLE_FIELDS_KEYS = ["subject", "target"]
+UPDATEABLE_FIELDS_KEYS = dict.fromkeys(["subject", "target"]).keys()

I can then reproduce the stacktrace.

The easy fix is to not call .keys() (the only valid case I've seen for calling .keys() in python is to use it as a setlike, every other case I've seen is better done as either (1) iterate over the dictionary directly or (2) use in for containment (3) convert to the type you want via iterator)

In this case, you probably want a list or a tuple or a set, for example:

SOME_DICT = {"subject": 1, "target": 1}

UPDATEABLE_FIELDS_KEYS = frozenset(SOME_DICT)
1
  • Thank you for the helpful insight. This solves the problem and opened my eyes to the issues of using .keys() when I probably wanted something different.
    – Chris
    Jun 19, 2020 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.