-
Notifications
You must be signed in to change notification settings - Fork 0
Extend as JSON schema to include list[Any] #12
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 |
|---|---|---|
|
|
@@ -405,13 +405,16 @@ def handles_type(cls, py_type: Type) -> bool: | |
|
|
||
|
|
||
| @register_schema | ||
| class DictAsJSONSchema(Schema): | ||
| """An Avro string schema representing a Python Dict[str, Any] or List[Dict[str, Any]] assuming JSON serialization""" | ||
| class TypeAsJSONSchema(Schema): | ||
| """ | ||
| An Avro string schema representing a Python Dict[str, Any], List[Dict[str, Any]] or List[Any] assuming | ||
| JSON serialization | ||
| """ | ||
|
|
||
| @classmethod | ||
| def handles_type(cls, py_type: Type) -> bool: | ||
| """Whether this schema class can represent a given Python class""" | ||
| return _is_dict_str_any(py_type) or _is_list_dict_str_any(py_type) | ||
| return is_logically_json(py_type) | ||
|
|
||
| def data(self, names: NamesType) -> JSONObj: | ||
| """Return the schema data""" | ||
|
|
@@ -1280,6 +1283,17 @@ def _is_list_dict_str_any(py_type: Type) -> bool: | |
| return False | ||
|
|
||
|
|
||
| def _is_list_any(py_type: Type) -> bool: | ||
| """Return whether a given type is ``List[Any]``""" | ||
| origin = get_origin(py_type) | ||
| return inspect.isclass(origin) and issubclass(origin, list) and get_args(py_type) == (Any,) | ||
|
|
||
|
|
||
| def is_logically_json(py_type: Type) -> bool: | ||
| """Returns whether a given type is logically a JSON and can be serialized as such""" | ||
| return _is_list_any(py_type) or _is_list_dict_str_any(py_type) or _is_dict_str_any(py_type) | ||
|
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: is it worth keeping the 3 different functions as they are? Here, if we're to match the last condition, we will call If they're not used, I could see them being merged into one and we could use recursion on them? Something like: def is_logically_json(py_type: Type) -> bool:
origin = get_origin(py_type)
if not inspect.isclass(origin):
return False
args = get_args(py_type)
if issubclass(origin, list):
if not args:
return False
elif args == (Any,):
return True
return is_logically_json(args[0])
elif issubclass(origin, dict):
return args == (str, Any)Or something like that? I'm not sure it's really clearer, with some comments it would be better but at least we're calling the methods only once every time and we can add more case more easily, and the recursion would handle all cases of "logically JSON"? it might be the case already though I feel like this would match the |
||
|
|
||
|
|
||
| def _is_class(py_type: Any, of_types: Union[Type, Tuple[Type, ...]], include_subclasses: bool = True) -> bool: | ||
| """Return whether the given type is a (sub) class of a type or types""" | ||
| py_type = _type_from_annotated(py_type) | ||
|
|
||
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.
question: should we rename the
DictAsJSONSchemato something likeTypeAsJSONSchemato indicates it can map more?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.
Good suggestion! Done it.