Skip to content

Commit

Permalink
Types from typing before Python 3.10 didn't have a __name__ attribu…
Browse files Browse the repository at this point in the history
…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
1 parent 541beb0 commit e32570b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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)
return output_schema

Expand Down
8 changes: 7 additions & 1 deletion qcelemental/util/autodocs.py
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

0 comments on commit e32570b

Please sign in to comment.