Type annotations in communicate don't account for ttl #63

Open
opened 2026-09-21 16:47:18 +00:00 by msz · 0 comments
Owner

Functions in communicate which take the format parameter (json / ttl) return a dict or str for json and ttl, respectively. However, their return types are always annotated with dict.

I noticed this when using dump-things-pyclient to fetch ttl records and had pyright enabled in my editor.

Consider a following usage of dump-things-pyclient to fetch a ttl record and add it to an RDFLib graph:

from dump_things_pyclient.communicate import collection_read_record_with_pid

g = Graph()
res = collection_read_record_with_pid(
    service_url="https://example.net/api",
    collection="public",
    pid="https://example.net/ns/something",
    format="ttl",
)
g.parse(data=res)

Although this code is correct (the function in this case returns str | None as expected by g.parse) type checker like ty (or pyright) would complain because the annotation implies dict:

❱ ty check snippet2.py
error[invalid-argument-type]: Argument to bound method `Graph.parse` is incorrect
  --> snippet2.py:11:9
   |
11 | g.parse(data=res)
   |         ^^^^^^^^ Expected `str | bytes | None`, found `dict[Unknown, Unknown] | None`
info: element `dict[Unknown, Unknown]` of union `dict[Unknown, Unknown] | None` is not assignable to `str | bytes | None`

I think simply changing the annotation to dict | str | None would make the type checkers even more unhappy - code which calls these functions with json only has to handle dict | None, and code which calls them with ttl only has to handle str | None.

I think (reinforced by this SO post) the correct way is to handle that is with overloads. So, for the collection_read_record_with_pid function (note that ... is part of the syntax):

diff --git a/dump_things_pyclient/communicate.py b/dump_things_pyclient/communicate.py
index 53fbb858d6..f3162e856f 100644
--- a/dump_things_pyclient/communicate.py
+++ b/dump_things_pyclient/communicate.py
@@ -6,6 +6,8 @@
 from typing import (
     Callable,
     Generator,
+    Literal,
+    overload,
 )

 import requests
@@ -147,13 +149,36 @@
             yield match.group(1)


+@overload
+def collection_read_record_with_pid(
+    service_url: str,
+    collection: str,
+    pid: str,
+    format: str = Literal['json'],
+    token: str | None = None,
+    session: Session | None = None,
+) -> dict | None:
+    ...
+
+@overload
+def collection_read_record_with_pid(
+    service_url: str,
+    collection: str,
+    pid: str,
+    format: str = Literal['ttl'],
+    token: str | None = None,
+    session: Session | None = None,
+) -> str | None:
+    ...
+
+
 def collection_read_record_with_pid(service_url: str,
                                     collection: str,
                                     pid: str,
                                     format: str = 'json',
                                     token: str | None = None,
                                     session: Session | None = None,
-                                    ) -> dict | None:
+                                    ) -> dict | str | None:
     """Read record with the given pid from the collection on the service

     Records are read from the curated area of the collection and from the

(the code above is untested, I had some trouble setting up)

However, the same would have to be repeated all over the communicate module (also for functions that return more complex things like tuples etc.), possibly relegated to a .pyi stub file. I could give it a try, but given that it would add a lot of LOC for (petty?) technical correctness, and given that the communicate module has (I think) semi-public API status, I think I would first just ask about your opinion on the matter - WDYT?

Functions in `communicate` which take the `format` parameter (json / ttl) return a `dict` or `str` for json and ttl, respectively. However, their return types are always annotated with `dict`. I noticed this when using `dump-things-pyclient` to fetch `ttl` records and had pyright enabled in my editor. Consider a following usage of dump-things-pyclient to fetch a ttl record and add it to an RDFLib graph: ``` python from dump_things_pyclient.communicate import collection_read_record_with_pid g = Graph() res = collection_read_record_with_pid( service_url="https://example.net/api", collection="public", pid="https://example.net/ns/something", format="ttl", ) g.parse(data=res) ``` Although this code is correct (the function in this case returns `str | None` as expected by `g.parse`) type checker like ty (or pyright) would complain because the annotation implies `dict`: ``` ❱ ty check snippet2.py error[invalid-argument-type]: Argument to bound method `Graph.parse` is incorrect --> snippet2.py:11:9 | 11 | g.parse(data=res) | ^^^^^^^^ Expected `str | bytes | None`, found `dict[Unknown, Unknown] | None` info: element `dict[Unknown, Unknown]` of union `dict[Unknown, Unknown] | None` is not assignable to `str | bytes | None` ``` I think simply changing the annotation to `dict | str | None` would make the type checkers even more unhappy - code which calls these functions with `json` only has to handle `dict | None`, and code which calls them with `ttl` only has to handle `str | None`. I think (reinforced by [this SO post](https://stackoverflow.com/questions/74133458/python-annotate-return-types-for-different-input-cases/74134088#74134088)) the correct way is to handle that is with [overloads](https://typing.python.org/en/latest/spec/overload.html). So, for the `collection_read_record_with_pid` function (note that `...` is part of the syntax): ``` diff diff --git a/dump_things_pyclient/communicate.py b/dump_things_pyclient/communicate.py index 53fbb858d6..f3162e856f 100644 --- a/dump_things_pyclient/communicate.py +++ b/dump_things_pyclient/communicate.py @@ -6,6 +6,8 @@ from typing import ( Callable, Generator, + Literal, + overload, ) import requests @@ -147,13 +149,36 @@ yield match.group(1) +@overload +def collection_read_record_with_pid( + service_url: str, + collection: str, + pid: str, + format: str = Literal['json'], + token: str | None = None, + session: Session | None = None, +) -> dict | None: + ... + +@overload +def collection_read_record_with_pid( + service_url: str, + collection: str, + pid: str, + format: str = Literal['ttl'], + token: str | None = None, + session: Session | None = None, +) -> str | None: + ... + + def collection_read_record_with_pid(service_url: str, collection: str, pid: str, format: str = 'json', token: str | None = None, session: Session | None = None, - ) -> dict | None: + ) -> dict | str | None: """Read record with the given pid from the collection on the service Records are read from the curated area of the collection and from the ``` (the code above is untested, I had some trouble setting up) However, the same would have to be repeated all over the `communicate` module (also for functions that return more complex things like tuples etc.), possibly relegated to a `.pyi` stub file. I could give it a try, but given that it would add a lot of LOC for (petty?) technical correctness, and given that the communicate module has (I think) semi-public API status, I think I would first just ask about your opinion on the matter - WDYT?
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
orinoco/dump-things-pyclient#63
No description provided.