Python Forum
What is the correct type hint when you want to accept Iterable but not Dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: What is the correct type hint when you want to accept Iterable but not Dictionary (/thread-16557.html)



What is the correct type hint when you want to accept Iterable but not Dictionary - LadySvetlana - Mar-05-2019

How would I indicate, using a type hint, that the function only works with a List type data structure containing only int?

I researched a little deeper and from the documentation, I found:

Quote:An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict, file objects...

So a dictionary is not a sequence type, but List is, so is Sequence the appropriate type hint?

Obviously, I would filter out str, and dict, but I would like a type hint as well.

def unpack(sequence_to_unpack : Sequence[int]):
    pass
Is the above correct?


RE: What is the correct type hint when you want to accept Iterable but not Dictionary - perfringo - Mar-05-2019

(Mar-05-2019, 03:39 AM)LadySvetlana Wrote: How would I indicate, using a type hint, that the function only works with a List type data structure containing only int?

What's wrong with List[int]?

From casual reading of typing - Support for type hints I have impression that this will deliver desired result (hints that function only works with List type containing only int).


RE: What is the correct type hint when you want to accept Iterable but not Dictionary - LadySvetlana - Mar-05-2019

(Mar-05-2019, 11:01 AM)perfringo Wrote: What's wrong with List[int]?

From casual reading of typing - Support for type hints I have impression that this will deliver desired result (hints that function only works with List type containing only int).

I had List[int] at first, but I was given the advice that I should use the abstract version in this case Sequence, but Sequence means alot of things, List, tuple, and str, my method is not meant to work with str.

List[int] solves my problem, but is it code smell?

What I don't understand is, in this case, type hints don't enforce or prevent someone from passing another collection or type. I'm still responsible for checking the types and rejecting what I don't want. I don't see a problem with List[int].


RE: What is the correct type hint when you want to accept Iterable but not Dictionary - woooee - Mar-05-2019

Quote:but I was given the advice that I should use the abstract version in this case Sequence

Sounds like bad advice. Why did you take it.


RE: What is the correct type hint when you want to accept Iterable but not Dictionary - LadySvetlana - Mar-05-2019

(Mar-05-2019, 05:42 PM)woooee Wrote:
Quote:but I was given the advice that I should use the abstract version in this case Sequence

Sounds like bad advice. Why did you take it.

See the whole conversation on Reddit.