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

Pydantic v2 Overhaul [DNM yet] #321

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4777b4b
Start migrating to Pydantic v2 still very much a WIP
Lnaden Aug 1, 2023
538a62f
Finish initial pass converting to v2 pydantic API. Have not done any …
Lnaden Aug 3, 2023
5bd215b
More headway, starting on tests. have to be careful with serializers …
Lnaden Aug 4, 2023
76b565b
Why does nested serialization behavior change between versions? WHY!?
Lnaden Aug 4, 2023
248676d
Finally have all tests passing. Huzzah! Still need to cleanup
Lnaden Aug 9, 2023
a2e3501
Handled CI and Lint. All tests pass locally.
Lnaden Aug 9, 2023
86d88f9
This is the dumbest dependency tree resolution. Autodoc-pydantic requ…
Lnaden Aug 10, 2023
e7f4549
fix black
Lnaden Aug 10, 2023
4b4c977
and apparently isort
Lnaden Aug 10, 2023
541beb0
So apparently I made a fun problem where black and isort undid each o…
Lnaden Aug 10, 2023
e32570b
Types from typing before Python 3.10 didn't have a `__name__` attribu…
Lnaden Aug 10, 2023
6201290
A found an even more frustrating bug with Numpy NDArray from numpy.ty…
Lnaden Aug 11, 2023
ad7d43b
One last doc bug and black
Lnaden Aug 11, 2023
a4d0ed2
Fixed a serialization problem in Datum because you cannot chain seria…
Lnaden Aug 15, 2023
37fcc09
Fixed serialization of complex ndarrays in Datum
Lnaden Aug 15, 2023
31f0b7c
Dont cast listified complex or ndarray to string on jsonify of data
Lnaden Aug 15, 2023
ad3b633
black
Lnaden Aug 15, 2023
6af962b
Fixed NumPy native types being unknown on how to serialize by Pydantic
Lnaden Aug 17, 2023
01f19ae
Debugging more of the JSON serializing
Lnaden Aug 30, 2023
f2b5989
Fixed typo in Array serialization to JSON where flatten was not calle…
Lnaden Aug 30, 2023
2ca4b14
Removed leftover debugging lines
Lnaden Aug 30, 2023
75e6aee
For serializing models, just try dumping them with model_dump_json wh…
Lnaden Aug 30, 2023
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
Prev Previous commit
Next Next commit
Types from typing before Python 3.10 didn't have a __name__ attribu…
…te, so split it up the hard way since there doesn't appear to be any other way to acess the name.

This is the single most frustrating bug to track down since the error is thrown in pydantic but doesn't backtrace through the pydantic.__getattr__ -> `AutoPydanticDocGenerator` -> `__get__` -> `doc_formatter` -> `annotation.__name__` function because all the exceptions are caught the whole way up. GYA!
  • Loading branch information
Lnaden committed Aug 10, 2023
commit e32570b6dba8bb0395b94a1cb78cc739a1062ac7
4 changes: 4 additions & 0 deletions qcelemental/models/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import Any, Dict

import numpy as np
Expand Down Expand Up @@ -66,6 +67,9 @@ def __get_pydantic_json_schema__(cls, _core_schema, handler) -> Dict[str, Any]:
items = {"type": "string"}
elif dt is bool or np.issubdtype(dt, np.bool_):
items = {"type": "boolean"}
else:
items = {"type": "Unknown"}
warnings.warn(f"Unknown dtype to handle type [{dt}] for array. May result in weird serialization or typing")
output_schema.update(type="array", items=items)
Fixed Show fixed Hide fixed
return output_schema

Expand Down
8 changes: 7 additions & 1 deletion qcelemental/util/autodocs.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched over to autodoc-pydantic Spring 2022 (or thereabouts), so I should check if this is still in use at all. Native Sphinx on pydantic is awful, so DGAS had something better, but autodoc-pydantic is far better still.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ def parse_type_str(prop: Union[FieldInfo, type]) -> str:
elif annotation.__module__ == "typing":
# Typing Types
prop_type_str = ""
base_name = annotation.__name__
# In python 3.9 and below, annotations didn't have __name__... so... do it the hard way
try:
base_name = annotation.__name__
except AttributeError:
splits = re.split(r"\.|\[", str(annotation))
# typing, {actual object name}, args... So get index 1
base_name = splits[1]
# Special case Optional
annotation_args = get_args(annotation)
prop_type_str += f"{base_name}"
Expand Down
Loading