This is another somewhat large PR that tackles the difficult area of typing search results. I started adding types to document_base.py and {_async|_sync}/document.py, plus any other related files that were necessary, which are many.
I set out to support fully typed results when you are searching using a document class. Here is an example from the vectors.py example, where the search automagically returns WorkplaceDoc instances as search results, just because the search object was created by calling WorkplaceDoc.search():
This is implemented using generics. The search class is now represented as Search[DocumentClass], where results are returned as instances of DocumentClass. If using Search alone, the default Hit object is used for typing, matching the runtime behavior.
Adding this required plumbing the generic type through a large part of the library. I also made some other changes along the way, to help me reach the goal of fully typing the document source files:
I simplified the AttrDict[K, V] generic class to just AttrDict[V]. Using any key types other than str creates lots of issues with the attribute dunder methods, which require attribute names to be strings. Really this class is never used with key types other than str anyway.
I've added a AsyncUsingType, UsingType and AnyUsingType to represent valid values to pass to the using argument that many classes have. These can be connection names or client instances, for the async and sync variants. The 3rd option is used in generic places where async vs. sync does not matter.
The AttrList class in utils.py does not have a generic type yet, and maybe it should, but I'm going to leave that for later.
I was using vectors.py as test bed to verify that typed searches were working fine, so I also added all the types to that example.
There are many things that still need to be typed in responses, for now I have only concentrated on typing the top level search results. Things such as aggregations, inner hits, etc. need to be done yet and are all Any at this time, I'll eventually get to do them as well.
This is another somewhat large PR that tackles the difficult area of typing search results. I started adding types to document_base.py and {_async|_sync}/document.py, plus any other related files that were necessary, which are many.
I set out to support fully typed results when you are searching using a document class. Here is an example from the vectors.py example, where the search automagically returns
WorkplaceDoc
instances as search results, just because the search object was created by callingWorkplaceDoc.search()
:This is implemented using generics. The search class is now represented as
Search[DocumentClass]
, where results are returned as instances ofDocumentClass
. If usingSearch
alone, the defaultHit
object is used for typing, matching the runtime behavior.Adding this required plumbing the generic type through a large part of the library. I also made some other changes along the way, to help me reach the goal of fully typing the document source files:
AttrDict[K, V]
generic class to justAttrDict[V]
. Using any key types other thanstr
creates lots of issues with the attribute dunder methods, which require attribute names to be strings. Really this class is never used with key types other thanstr
anyway.AsyncUsingType
,UsingType
andAnyUsingType
to represent valid values to pass to theusing
argument that many classes have. These can be connection names or client instances, for the async and sync variants. The 3rd option is used in generic places where async vs. sync does not matter.AttrList
class in utils.py does not have a generic type yet, and maybe it should, but I'm going to leave that for later.Any
at this time, I'll eventually get to do them as well.