Code Inspector possibly misuses type hints
Hello!
I'm currently using PyCharm for a web project with Django, and I recently ran into an inspector problem I'm not sure to understand: I use type hints for most of my functions, and it seems that a return type hint broke the inspector.
Here is the function (without its parameter, as there is quite a few optionnal ones) def line. Its return type may be a list or a dict, as it is the result of json.loads().
def get_from(...) -> Union[dict, list]:
And here is the code snippet that uses it and where it broke:
darksky_results = get_from(request_url, 'DarkSky')
hourly_forecasts = darksky_results['hourly']['data']
More exactly, the inspector warns me that 'data' is of an unexpected type (even if, in case of a dict return type, it is absolutely possible to have another dict at the 'hourly' key). Seeing the complete warning just below, it seems like the inspector is sure darksky_results['hourly'] is some kind of list.
Unexpected type(s): (str) Possible types: (int) (slice)
Inspection info: This inspection detects type errors in function call expressions.
Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases.
Types of function parameters can be specified in docstrings or in Python 3 function annotations.
This problem doesn't really break my code (it still runs perfectly fine), but it bugs me a bit and I didn't find how to correct it (except by stopping using annotations, which I'd prefer not to).
Thanks for your help
Please sign in to leave a comment.
Hi, it's possible the issue is related to some other part of your code. I would suggest to submit a bug report to https://youtrack.jetbrains.com/issues with a minimal code/project sample, so we could investigate.
I will do this. I didn't know of youtrack.jetbrains.com at the time I wrote my initial comment, but it seems like the best platform for this kind of thing.
Thanks for the redirection.