Skip to content

Unions and recursive type hints #89

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

Closed
python-desert opened this issue Feb 9, 2020 · 4 comments
Closed

Unions and recursive type hints #89

python-desert opened this issue Feb 9, 2020 · 4 comments

Comments

@python-desert
Copy link
Collaborator

There are at least two issues going on with our handling of recursive types.

One is handling of forward references in type hints. Python's representation inside the typing module seems to be evolving, and the tools for dealing with them (typing_inspect and pytypes) may be still catching up. I think that's what's catching us in #88.

There is also a general issue about the robustness of our union fields.

  • Theoretically, a greedy recursive descent should serialize fine in simple cases like the one, but the implementation we're using seems to be having problems. There may be some fixes for marshmallow-union that could address this.
  • There are also more complicated situations where greedy strategies will necessarily yield ambiguous parses. We could look into fancier parser generators that could detect and report ambiguities.
  • A more explicit union representation would eliminate the issue of ambiguity by simply declaring what types are being represented.
@sveinse
Copy link
Collaborator

sveinse commented Feb 10, 2020

In the context of serialization, unions must carry additional information to be able to differentiate between the union variant being used, either implicitly via type Union[str, int], or explicitly using a selector field. For this https://github.com/adamboche/python-marshmallow-union or https://github.com/Bachmann1234/marshmallow-polyfield/ exists.

I think it is error prone to rely on implicit type checks, so I think the first design decision would be if desert should extend into explicit union selectors. The downside of adding union selector fields is that the serialized stream adds information; There will be more fields than what the data class or schema suggests.

@altendky
Copy link
Member

I think that's #36? It kind of got put on hold while I did some CI stuff for desert and now I'm trying to catchup pyqt5-tools... I did get a wrapper approach implemented as ExplicitPolyField over in Bachmann1234/marshmallow-polyfield#34.

@python-desert
Copy link
Collaborator Author

Yeah let's call this a duplicate of #36.

@altendky
Copy link
Member

And #88 for the recursion piece mentioned here, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants