Replies: 3 comments 16 replies
-
I'm not sure why you are putting the blame on typeshed here: the root of the problem you're seeing is that the |
Beta Was this translation helpful? Give feedback.
-
After giving this a lot more thought I keep coming back to two things: Firstly pythons's ecosystem1 has not converged2 on one single interpretation of type hinting for these cases. Yes I know one way might be "right" and the other "wrong" but that's beside the point. It's unlikely that a common interpretation will be forthcoming in the near future because of the complexity that would involve for some otherwise simple tools. Secondly PEP 484 has some very pertinent words for these situations which are even highlight in bold in the original text:
Therefore it seems most pragmatic to me to simply NOT hint critically problematic functions and instead ensure the documentation is extra clear. If this involves disabling Mypy for those functions then so be it. This ensures that functions are not incorrectly hinted in any interpretation and are ultimately interpreted the same in all. The hope is that someday the ecosystem will converge and hints can be added back at that time.
|
Beta Was this translation helpful? Give feedback.
-
Interesting discussion, thanks! I'm the author of mkdocstrings 🙂 |
Beta Was this translation helpful? Give feedback.
-
I'm rather stuck knowing which way to turn or which way to advise our team. The problem we're facing started with an error reported in mypy which after some digging, was ultimately triggered by typeshed definitions. The typeshed definitions are technically correct but conflict with rational interpretations made by tools that don't use typeshed.
In particular, many tools that don't use typeshed assume that decorators have no effect on function signature. While technically incorrect, this assumption is at least rational and pragmatic.
Specific example
@contextlib.contextmanager
This decorator changes the function signature, so while the internal method returns an
Iterable
orGenerator
, the wrapped method returns aContextManager
. Mypy uses typeshed's definition and so expects the method to be hinted as anIterable
, raising an error otherwise.But many tools do not use typeshed, particularly tools for docstring interpretation like
mkdocstrings-python
. If we hint withIterable
in the source code, then that's exactly what will be printed in our documentation and that is NOT correct. Worse other linters not using typeshed will raise warnings because they do not understand the function signature has changed.It leaves us stuck between a rock and a hard place, either we live with Mypi constantly warning us (disabling it completely for such a function), or we live with many other tools complaining, and we live with incorrect documentation.
General discussion
It seems that having separate type information in typeshed that's not in the origin source creates something of a mess, where it can conflict with reasonable interpretations from those tools that don't use typeshed. As much as it could be argued that all such tools should be using it, the fact is they don't.
For now, I think our decision is to disable Mypy warnings using
# type: disable
and deliberately typehint "wrong" in a way that static code analysers can more reliably interpret. I certainly don't want incorrect documentation.Honestly I don't like this choice, but I don't see a practical way to proceed while the existence of typeshed creates a big split between different tools.
Beta Was this translation helpful? Give feedback.
All reactions