Thanks to Ian Haywood's review, I've finally managed to land a big upgrade to 
Twisted's annotations for Deferred: 
https://github.com/twisted/twisted/pull/11770#issuecomment-1625648066

This is a fix for a bunch of broken annotations within Twisted, both ones that 
would give you the wrong type sometimes and ones that would give up and give 
you `Deferred[Any]` when enough information was available to do otherwise, and 
may help you chase down bugs or otherwise remove spurious type:ignore's from 
your projects.  

In particular, the following methods should provide richer type information now:

maybeDeferred
Deferred.fromFuture 
Deferred.addErrback
Deferred.addCallbacks

I would like to note that due to limitations in the way ParamSpec is specified, 
you'll get less strict validation if you use `addCallbacks`; it's not possible 
to check argument-signature compatibility for your callbacks with the structure 
of `addCallbacks`'s signature.  Using some combination of `addCallback` and 
`addErrback` when possible is probably better.

Per the linked comment though, while this is not new in terms of the way 
Twisted's type annotations should have been working, some of those `Any`s might 
create situations where you were doing `d.addCallback(...); d.addCallback(...); 
d.addCallback(...); return d` where you will need to switch to `return 
d.addCallback(...).addCallback(...).addCallback(...)` in order to get the types 
to line up properly.  (I think if you were literally using `addCallback` you 
won't have this problem, but combinations of `addCallbacks`, `addErrback`, or 
`addBoth` might have issues.)

I'm still open to trading reviews, and my next types project (which is 
considerably smaller in scope and less confusing) is to make FilePath properly 
generic so you can tell whether `.path` is `str` or `bytes`: 
https://github.com/twisted/twisted/pull/11823 so if anyone has any interest in 
reviewing that please let me know what PR you'd like me to look at.

-g
_______________________________________________
Twisted mailing list -- twisted@python.org
To unsubscribe send an email to twisted-le...@python.org
https://mail.python.org/mailman3/lists/twisted.python.org/
Message archived at 
https://mail.python.org/archives/list/twisted@python.org/message/4E6LRIEJSN37WBKJ536CFNHSZDTVMQBN/
Code of Conduct: https://twisted.org/conduct

Reply via email to