-
Notifications
You must be signed in to change notification settings - Fork 803
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
Use Python types to declare document fields #1845
Conversation
fd3713a
to
95b2a59
Compare
c0b35e1
to
33bedf8
Compare
33bedf8
to
9fc6740
Compare
@pquentin I suggest you start from the new documentation in persistence.rst when reviewing this. |
9fc6740
to
7a40527
Compare
.. list-table:: Python type to DSL field mappings | ||
:header-rows: 1 | ||
|
||
* - Python type | ||
- DSL field | ||
* - ``str`` | ||
- ``Text(required=True)`` | ||
* - ``bool`` | ||
- ``Boolean(required=True)`` | ||
* - ``int`` | ||
- ``Integer(required=True)`` | ||
* - ``float`` | ||
- ``Float(required=True)`` | ||
* - ``bytes`` | ||
- ``Binary(required=True)`` | ||
* - ``datetime`` | ||
- ``Date(required=True)`` | ||
* - ``date`` | ||
- ``Date(format="yyyy-MM-dd", required=True)`` |
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 is very helpful, should we also add List and Optional?
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 call, I'll add them to the table.
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.
Did you decide against adding Optional
?
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.
I didn't. Sorry, I added List, but left out Optional
. I'm adding it.
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.
Okay, so I tried a couple of different formats and found that growing the table makes it too long and more difficult to visually parse. So in the end I kept the table small with just the required variants of the Python native types, and right after the table I show examples of Optional
, List
, Object
and Nested
. Let me know what you think.
By the way, I forgot to mention it, but this is very cool 👍 |
Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>
Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>
@pquentin Thanks for all the feedback! I think I have addressed all of your notes. I rewrote the |
2bf0080
to
90d30fb
Compare
90d30fb
to
2997d3c
Compare
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.
Thanks! Only left two questions.
.. list-table:: Python type to DSL field mappings | ||
:header-rows: 1 | ||
|
||
* - Python type | ||
- DSL field | ||
* - ``str`` | ||
- ``Text(required=True)`` | ||
* - ``bool`` | ||
- ``Boolean(required=True)`` | ||
* - ``int`` | ||
- ``Integer(required=True)`` | ||
* - ``float`` | ||
- ``Float(required=True)`` | ||
* - ``bytes`` | ||
- ``Binary(required=True)`` | ||
* - ``datetime`` | ||
- ``Date(required=True)`` | ||
* - ``date`` | ||
- ``Date(format="yyyy-MM-dd", required=True)`` |
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.
Did you decide against adding Optional
?
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.
Thanks! LGTM, even if the table doesn't have List or Optional. Maybe better to explain it afterwards as you did indeed.
* basic type annotation support * support for optional, list, and other type hints * additional typing support * dataclass-like behavior for Document and InnerDoc * unit tests * support InstrumentedField in Search class * documentation * Update docs/persistence.rst Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com> * Update elasticsearch_dsl/document_base.py Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com> * addressed review feedback * better docs for Optional * fix optional in test --------- Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com> (cherry picked from commit 0c3ffcd)
* basic type annotation support * support for optional, list, and other type hints * additional typing support * dataclass-like behavior for Document and InnerDoc * unit tests * support InstrumentedField in Search class * documentation * Update docs/persistence.rst Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com> * Update elasticsearch_dsl/document_base.py Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com> * addressed review feedback * better docs for Optional * fix optional in test --------- Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com> (cherry picked from commit 0c3ffcd) Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
With this change,
Document
andInnerDoc
instances can optionally use Python type hints instead or in addition to DSL field objects, and inherit dataclass-style behaviors which allow type checkers to correctly infer types.