Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

docs: Add an example for read-only types.

Zixuan James Li8e472402fef1
diff --git a/docs/testing/mypy.md b/docs/testing/mypy.md
index 0e5c526cb3a..7aaedd0eb4c 100644
--- a/docs/testing/mypy.md
+++ b/docs/testing/mypy.md
@@ -381,6 +381,21 @@ types might be appropriate. (But don’t use `Iterable` for a value
that might be iterated multiple times, since a one-use iterator is
`Iterable` too.)
+For example, if a function gets called with either a `list` or a `QuerySet`,
+and it only iterates the object once, the parameter can be typed as `Iterable`.
+
+```python
+def f(items: Iterable[Realm]) -> None:
+ for item in items:
+ ...
+
+realms_list: List[Realm] = [zulip, analytics]
+realms_queryset: QuerySet[Realm] = Realm.objects.all()
+
+f(realms_list) # OK
+f(realms_queryset) # Also OK
+```
+
A function's return type can be mutable if the return value is always
a freshly created collection, since the caller ends up with the only
reference to the value and can freely mutate it without risk of