Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14040

Why do python type hints sometimes worsen IDE recommendations?

$
0
0

I'm going through an exercise of adding type hints through a large codebase, but I'm sometimes seeing that less-than-optimal type hint worsens IDE recommendations:

before, the IDE is able to figure out that y['result'] is a string:

enter image description here

after, it does not know:

enter image description here

I know I could fix this with a more specific type hint:

enter image description here

But in general, is there a way to know if the type hint being added is less specific that what the LSP was able to infer from the code?

I've looked to add very detailed type hints to avoid this issue (e.g. subclassing TypeDict), but keen to avoid the effort if the LSP is able to figure things out itself.

EDIT #1:

To clarify, I'm asking if there is a way to warn the user when they are adding a type hint that will worsen the LSP's understanding of the code:

We have warnings when the type hints contradict the code:

enter image description here

What I'm looking for is some warning for when the type hint is worse than no type hint at all.

enter image description here

EDIT #2:

To elaborate further, you can see here that the LSP was easily able to infer a pretty detailed return type.

enter image description here

Which would have been lost if the user added a "bad" type hint.

enter image description here

But I take the overall feedback, maybe this is an ask to the pylance project more than a stack overflow question.


Viewing all articles
Browse latest Browse all 14040

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>