python-attrs / python-attrs/cattrs

Most elegant way of doing error-checking and post-processing on `structure`d data?

Open
#58 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Description

  • cattrs version: 0.9.0
  • Python version: 3.7.2
  • Operating System: Windows 10 x64
Description

I want to turn a JSON file containing some mandatory and some optional fields into a set of frozen data classes, that are fully sanity-checked and have all optional fields filled in with inferred data, so that I can subsequently leave out any further checks before I use it in a different context. Example file:

{
  "axes": [
    {
      "name": "Inline Skeleton"
    },
    {
      "name": "Worm Skeleton"
    },
    {
      "name": "Stripes"
    }
  ]
}

Data classes:

from typing import List, Optional

import attr

@attr.s(auto_attribs=True, frozen=True, slots=True)
class Axis:
    name: str
    ordering: Optional[int] = None

@attr.s(auto_attribs=True, frozen=True, slots=True)
class Axes:
    axes: List[Axis]
In [51]: cattr.structure(example_json, Axes)
Out[51]: Axes(axes=[Axis(name='Inline Skeleton', ordering=None), Axis(name='Worm Skeleton', ordering=None), Axis(name='Stripes', ordering=None)])

In this case,

  1. if the user specified ordering for at least one Axis, she needs to specify it for all of them and the orderings have to make sense (i.e. no out of bounds, ...)
  2. if the user did not specify any, fill in the implicit order, so the first axis gets a zero, the second a one, etc.

I can do 1. in __attrs_post_init__, but how to best do 2. on the frozen classes? I can use the object.__setattr__(self, "attribute_name", value) escape hatch described in the attrs API reference to get around the freeze, but is there a more elegant way? I thought about writing a custom structure hook for Axes, but the problem is that to fill stuff in, I first need to structure it entirely, so this would recurse? Also, I'd still need to use the escape hatch because everything is frozen?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the cattr.structure call, the Axes and Axis attrs definitions, and the attrs attrs_post_init behavior described in the issue. Compare the proposed custom structure hook with frozen-class initialization constraints; done means there is a decided, documented approach for validating orderings and inferring missing ones. No repository file or test is named.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.