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

Validate dataclass with optional field and default #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
dist/
/**/__pycache__/

.*sw*
10 changes: 5 additions & 5 deletions latch_data_validation/data_validation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import collections.abc
import dataclasses
import sys
import typing
from enum import Enum
from itertools import chain
import sys
from types import FrameType, NoneType, UnionType
from typing import (
Any,
Expand All @@ -19,7 +20,6 @@
get_origin,
get_type_hints,
)
import typing

from opentelemetry.trace import get_tracer

Expand Down Expand Up @@ -103,7 +103,7 @@ def explain(self, indent: str = ""):

pretty_msg = prettify(
self.msg,
add_colon=True
add_colon=True,
# add_colon=len(self.children) > 0 or len(self.details) > 0
)
res = f"{indent}{pretty_msg}\n"
Expand Down Expand Up @@ -196,7 +196,8 @@ def untraced_validate(x: JsonValue, cls: type[T]) -> T:
for f in dataclasses.fields(cls):
schema_fields.add(f.name)
if f.name not in x:
missing_class_fields.append(f)
if get_origin(f.type) != Union and NoneType not in get_args(f.type):
missing_class_fields.append(f)
continue

try:
Expand Down Expand Up @@ -459,7 +460,6 @@ def untraced_validate(x: JsonValue, cls: type[T]) -> T:
raise DataValidationError("[!Internal Error!] unknown type", x, cls)



def validate(x: JsonValue, cls: type[T]) -> T:
with tracer.start_as_current_span(
validate.__qualname__,
Expand Down