-
Notifications
You must be signed in to change notification settings - Fork 0
Improve schemas for non-total TypedDict #11
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| from enum import StrEnum | ||
| from typing import Annotated, TypedDict | ||
|
|
||
| import py_avro_schema as pas | ||
| from py_avro_schema._alias import Alias, register_type_alias | ||
| from py_avro_schema._testing import assert_schema | ||
|
|
||
|
|
@@ -85,3 +87,29 @@ class User(TypedDict): | |
| } | ||
|
|
||
| assert_schema(User, expected) | ||
|
|
||
|
|
||
| def test_non_total_typed_dict(): | ||
| class Opt(StrEnum): | ||
| val = "invalid-val" | ||
|
|
||
| class PyType(TypedDict, total=False): | ||
| name: str | ||
| nickname: str | None | ||
| age: int | None | ||
| opt: Opt | None | ||
|
|
||
| expected = { | ||
| "type": "record", | ||
| "name": "PyType", | ||
| "fields": [ | ||
| { | ||
| "name": "name", | ||
| "type": "string", | ||
| }, | ||
| {"name": "nickname", "type": ["string", "null"]}, | ||
| {"name": "age", "type": ["long", "null", "string"]}, | ||
| {"name": "opt", "type": [{"namedString": "Opt", "type": "string"}, "null"]}, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: so here the regular
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it would. |
||
| ], | ||
| } | ||
| assert_schema(PyType, expected, options=pas.Option.MARK_NON_TOTAL_TYPED_DICTS) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be worth to add a
str | Nonehere just to verify what happens when we doUnion[str, None, str]?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in c514ae0.
Unioncan't have duplicates, so it would just beUnion[str, | None]. If you actually try to create a type alias like this, you'd get aOptional[str]:)