Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm type hinting weirdness

  • PyCharm version 4.5.4

  • Using Python 3.4.3

For some reason I'm seeing this warning in PyCharm, it seems odd.

Expected 'collections.Iterable', got 'range[int]' instead

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.

for _ in range(x):

To me this seems like literally the most idiomatic for loop you can write.

enter image description here

like image 553
jgritty Avatar asked Dec 16 '15 17:12

jgritty


People also ask

Are Python type hints enforced?

Not enforced at runtime The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.

What are type hints PyCharm?

Type hinting in PyCharm Last modified: 19 September 2022. PyCharm provides various means to assist inspecting and checking the types of the objects in your script. PyCharm supports type hinting in function annotations and type comments using the typing module and the format defined by PEP 484.

How do you type hints in Python?

Here's how you can add type hints to our function: Add a colon and a data type after each function parameter. Add an arrow ( -> ) and a data type after the function to specify the return data type.

What are type hints?

Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. Here's an example of adding type information to a function.


1 Answers

Given your report, this appears to be a bug in PyCharm. A range object is an iterable, and an Iterable. In 3.4.3:

>>> import collections
>>> x = 3
>>> isinstance(range(x), collections.Iterable)
True

Try reporting the issue to PyCharm people.

like image 187
Terry Jan Reedy Avatar answered Oct 17 '22 03:10

Terry Jan Reedy