Type annotations in communicate don't account for ttl #63
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Functions in
communicatewhich take theformatparameter (json / ttl) return adictorstrfor json and ttl, respectively. However, their return types are always annotated withdict.I noticed this when using
dump-things-pyclientto fetchttlrecords 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:
Although this code is correct (the function in this case returns
str | Noneas expected byg.parse) type checker like ty (or pyright) would complain because the annotation impliesdict:I think simply changing the annotation to
dict | str | Nonewould make the type checkers even more unhappy - code which calls these functions withjsononly has to handledict | None, and code which calls them withttlonly has to handlestr | 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_pidfunction (note that...is part of the syntax):(the code above is untested, I had some trouble setting up)
However, the same would have to be repeated all over the
communicatemodule (also for functions that return more complex things like tuples etc.), possibly relegated to a.pyistub 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?