alecthomas / alecthomas/voluptuous

dataclasses support

Offen
#409 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
1.9k
Forks
237
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Is this in or out of scope?

```python

# requires Python 3.7
from dataclasses import dataclass

from voluptuous import Schema, Object, All, Range
from voluptuous.schema_builder import PREVENT_EXTRA

@dataclass
class InventoryItem:
name: str
unit_price: float
quantity_on_hand: int = 0

def total_cost(self) -> float:
return self.unit_price * self.quantity_on_hand

class DataClassSchema(Schema):
@staticmethod
def dataClassSchema(x):
return {
fieldName: fieldValues.type
for fieldName, fieldValues in (x.__dataclass_fields__).items()
}

@staticmethod
def merge_constraints_of_key(a, b):
return All(a, b)

def merge_constraints_of_schema(self, dclsSchema, addSchema):
result = dict()
for k, v in dclsSchema.items():
if k in addSchema:
result[k] = self.merge_constraints_of_key(v, addSchema[k])
else:
result[k] = v
return result

def __init__(self, dcls, schema, required=False, extra=PREVENT_EXTRA):
return super(DataClassSchema, self).__init__(
Object(
self.merge_constraints_of_schema(self.dataClassSchema(dcls), schema),
cls=dcls,
),
required,
extra,
)

s1 = Schema(
Object(
{"name": str, "unit_price": All(float, Range(min=5)), "quantity_on_hand": int}
),
InventoryItem,
)

s2 = DataClassSchema(InventoryItem, {"unit_price": Range(min=5)})

# assert s1 == s2 # fails due to bug: Schema({'a': All(float)}) == Schema({'a': All(float)})

# however, s1 and s2 operate equivalently:

# s1(InventoryItem('nails', 3.33, 4))
# s2(InventoryItem('nails', 3.33, 4))
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.