-
Notifications
You must be signed in to change notification settings - Fork 3.2k
add as_dict support #31028
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
add as_dict support #31028
Conversation
8c5698a to
87db614
Compare
87db614 to
ca18350
Compare
| k: v | ||
| for k, v in sys.modules[module_name].__dict__.items() | ||
| if isinstance(v, type) | ||
| if isinstance(v, type) or isinstance(v, typing._GenericAlias) |
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.
This could resolve type alias problem.
| # is it a forward ref / in quotes? | ||
| if isinstance(annotation, (str, typing.ForwardRef)): | ||
| try: | ||
| model_name = annotation.__forward_arg__ # type: ignore | ||
| except AttributeError: | ||
| model_name = annotation | ||
| if module is not None: | ||
| annotation = _get_model(module, model_name) |
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.
ForwardRef should be resolved at first.
| if getattr(annotation, "__origin__", None) is typing.Union: | ||
|
|
||
| def _deserialize_with_union(union_annotation, obj): | ||
| for t in union_annotation.__args__: | ||
| try: | ||
| return _deserialize(t, obj, module, rf) | ||
| except DeserializationError: | ||
| pass | ||
| raise DeserializationError() | ||
|
|
||
| return functools.partial(_deserialize_with_union, annotation) |
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.
Union should be handled after Optional, bc Optional is also a Union.
| return cls(data) | ||
| return mapped_cls._deserialize(data) # pylint: disable=protected-access | ||
|
|
||
| def as_dict(self, *, exclude_readonly: bool = False, exclude_none: bool = False) -> typing.Dict[str, typing.Any]: |
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.
json.dumps will return a string and need to loads again to get dict. I prefer to use more efficient way.
88527e5 to
f5a67e2
Compare
6f1df72 to
9d9e3f6
Compare
9d9e3f6 to
5f1ddcd
Compare
| p._rest_name for p in o._attr_to_rest_field.values() if _is_readonly(p) | ||
| ] | ||
| return {k: v for k, v in o.items() if k not in readonly_props} | ||
| result = dict(o.items()) |
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.
Why are we creating the dict and then pop:ping read-only properties? This seems less efficient than filtering them out before adding them to the new dict....
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.
Oh, the logic is for easily deal with exclude_none in the same time. Since we decide to add exclude_none later, I've refined the code.
| k: v | ||
| for k, v in sys.modules[module_name].__dict__.items() | ||
| if isinstance(v, type) | ||
| if isinstance(v, (type, typing._GenericAlias)) # type: ignore |
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.
This looks really odd. What are we trying to accomplish?
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.
Actually I want to resolve TypeAlias as follows.
MyNamedUnion = Union["UnionModel1", "UnionModel2"]
class ModelWithNamedUnionProperty(Model):
named_union: "MyNamedUnion" = rest_field(name="namedUnion")
I've changed the logic a little to:
azure-sdk-for-python/sdk/core/azure-core/azure/core/serialization.py
Lines 630 to 633 in ff8f377
| # is it a type alias? | |
| if isinstance(annotation, str): # type: ignore | |
| if module is not None: | |
| annotation = _get_type_alias_type(module, annotation) |
a2e5857 to
ff8f377
Compare
|
close bc all the code and test has been migrated to generator, refer this PR: Azure/autorest.python#2027 |
part of Azure/autorest.python#1990.
also, fix Azure/autorest.python#2034, Azure/autorest.python#2035, Azure/autorest.python#2036