Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #385: Dyslexics retyping hosts file on servers


comp / comp.lang.python / Serializing pydantic enums

SubjectAuthor
o Serializing pydantic enumsLarry Martell

1
Subject: Serializing pydantic enums
From: Larry Martell
Newsgroups: comp.lang.python
Date: Tue, 28 May 2024 12:48 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: larry.martell@gmail.com (Larry Martell)
Newsgroups: comp.lang.python
Subject: Serializing pydantic enums
Date: Tue, 28 May 2024 05:48:22 -0700
Lines: 24
Message-ID: <mailman.50.1716900518.2909.python-list@python.org>
References: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de m8TQMx2IXSlfuvNnEmcOAgs2aMMZr7zS6wH52hCMserQ==
Cancel-Lock: sha1:wjXFeti9qTX7h6PfDHzQhVJxW1o= sha256:j1z6O3ov/XVYNm1NU2rs06uCD19TsHgo8X/i1BQh9Bo=
Return-Path: <larry.martell@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=D+ApxKj/;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.010
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'this:': 0.03; 'json':
0.09; 'typeerror:': 0.09; '***': 0.16; 'skip:s 150': 0.16;
'this:<pre': 0.16; 'to:addr:python-list': 0.20; 'cannot': 0.25;
'tried': 0.26; '<pre': 0.26; 'object': 0.26; 'to:name:python':
0.32; 'message-id:@mail.gmail.com': 0.32; 'but': 0.32; 'same':
0.34; 'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'missing': 0.37; 'class': 0.37; 'skip:u 20': 0.39
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1716900515; x=1717505315; darn=python.org;
h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
:date:message-id:reply-to;
bh=0Xb5JrefIcocVHx12EU+FKB4U4wkHRRpq8ePuLqM2/k=;
b=D+ApxKj/DpXry9ZvOBpsBZhhFOBgRKf7L4lrHnCyMbbwrrU9X3SuuqW98rUOCbpyeO
0lNX88J1bXPgrJCLMrgT3c0Kov2kJZk2mIMqJ5+lXrG/DqwZ4eAuBOYIa4P2jG7ymVJ3
pbE0hbpkzLdfA3OWmxlK6JzZNpZccb2Y2g7osOGEpJjiBKaTx8GT83zTQ3VEPoLY31dg
Ke9Fkat8vmNQe4Y+2V3MErQxrNjpY/UdQV4YrUp1F81Qy+rNjN5Fi4bxB2DfUoaqhh7y
+kc1rNZ6UEf85h1QCoSw9euWtmtz952dBCLNkc3W1uVNEmmQ1f4P81yfdNKDM2cfZTSF
lb1g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1716900515; x=1717505315;
h=to:subject:message-id:date:from:mime-version:x-gm-message-state
:from:to:cc:subject:date:message-id:reply-to;
bh=0Xb5JrefIcocVHx12EU+FKB4U4wkHRRpq8ePuLqM2/k=;
b=JtA6SJTHL+A77D4pZpIwFXEchPG3wIlWP3y53sVzdKePvmwJLK5lL5ndfyZW9BPnYj
OdZXAcQ8vpN9+92bFDuDekFkXwfuTYLXfsYuTuZvhgYb7D6og/tG81rwj03nfaBC1n4c
HXG5q5zFG7tHg5Qsu1t6ThPHAM4ccvWENJXJ/lI4q7I3TbLjot382PEISH62zBvJgwj1
tau6ClQeVc0HhJ9vfDrOA84w9Ref5ZKvHO5p2Kf/Wvjh4KkHCUL34KL4jNcCPSoHYR3X
Lx0OemWjM1WKjUTYIsSsbc/5syDflHCzvR1/3NJ1gBV5JzyFUaKhGdZJVi/ARF71xvUj
s5Hw==
X-Gm-Message-State: AOJu0YxWB6skmtchO0YBn1ZAImi++DwPu8Z5ZY+CofGHhbKg4XD0sx5s
EljnyM90AYPlTGuWBQSmZwPGFHsxE+07KZo77u/Zjqcng+IfHXYO1+bUPtxfz08QTURil8ZKJ6Z
H0b9/49jembTJrymZ/bRybL9jCpAj
X-Google-Smtp-Source: AGHT+IF0dk4g0MYcGqrkOBlTLKqgebuK4N9pvmcgMiXzWiOY/0zzqurwm39MtUA+0aYGx6XrY5ANq3vIi7spQUVcLZE=
X-Received: by 2002:a25:5f08:0:b0:de8:bdd3:c113 with SMTP id
3f1490d57ef6-df7721baeaemr10003924276.25.1716900515205; Tue, 28 May 2024
05:48:35 -0700 (PDT)
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
View all headers

Just getting started with pydantic. I have this example code:

class FinishReason(Enum):
stop = 'stop'

class Choice(BaseModel):
finish_reason: FinishReason = Field(...)

But I cannot serialize this:

json.dumps(Choice(finish_reason=FinishReason.stop).dict())
*** TypeError: Object of type FinishReason is not JSON serializable

I get the object not the value:

(Pdb) Choice(finish_reason=FinishReason.stop)
Choice(finish_reason=<FinishReason.stop: 'stop'>)

Also tried it with .value, same result.

What am I missing here?

1

rocksolid light 0.9.8
clearnet tor