Skip to content

Commit 141ad0e

Browse files
chore(firestore): improve typing for collection (#16999)
Previously, `CollectionReference` didn't define `.document()`, so it inherited the typing of `BaseCollectionReference`. This PR adds an override to ensure that documents are annotated as returning `DocumentReference` types --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 690c15d commit 141ad0e

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

  • packages/google-cloud-firestore/google/cloud/firestore_v1

packages/google-cloud-firestore/google/cloud/firestore_v1/collection.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from __future__ import annotations
1818

19-
from typing import TYPE_CHECKING, Any, Callable, Generator, Optional, Tuple, Union
19+
from typing import TYPE_CHECKING, Any, Callable, Generator, Optional, Tuple, Union, cast
2020

2121
from google.api_core import gapic_v1
2222
from google.api_core import retry as retries
@@ -34,6 +34,7 @@
3434
import datetime
3535

3636
from google.cloud.firestore_v1.base_document import DocumentSnapshot
37+
from google.cloud.firestore_v1.document import DocumentReference
3738
from google.cloud.firestore_v1.query_profile import ExplainOptions
3839
from google.cloud.firestore_v1.stream_generator import StreamGenerator
3940

@@ -134,6 +135,22 @@ def add(
134135
write_result = document_ref.create(document_data, **kwargs)
135136
return write_result.update_time, document_ref
136137

138+
def document(self, document_id: Union[str, None] = None) -> "DocumentReference":
139+
"""Create a sub-document underneath the current collection.
140+
141+
Args:
142+
document_id (Optional[str]): The document identifier
143+
within the current collection. If not provided, will default
144+
to a random 20 character string composed of digits,
145+
uppercase and lowercase and letters.
146+
147+
Returns:
148+
:class:~google.cloud.firestore_v1.document.DocumentReference:
149+
The child document.
150+
"""
151+
doc = super().document(document_id)
152+
return cast("DocumentReference", doc)
153+
137154
def list_documents(
138155
self,
139156
page_size: Union[int, None] = None,

0 commit comments

Comments
 (0)