Python Forum
What is the correct type hint when you want to accept Iterable but not Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the correct type hint when you want to accept Iterable but not Dictionary
#1
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?
Reply
#2
(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).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(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].
Reply
#4
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.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  501 Server cannot accept argument anna17 0 172 Apr-11-2024, 01:08 AM
Last Post: anna17
  How to accept facebook cookies using python selenium? pablo86ad 0 167 Apr-06-2024, 09:19 PM
Last Post: pablo86ad
  accept command line argument mg24 5 1,296 Sep-27-2022, 05:58 PM
Last Post: snippsat
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 1,561 May-24-2022, 12:03 PM
Last Post: deanhystad
  Import a module for use in type hint? Milosz 0 1,482 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,181 Aug-10-2021, 08:10 AM
Last Post: snippsat
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 8,900 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 5,973 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,058 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  TypeError: 're.Match' object is not iterable charlesauspicks 1 11,602 May-25-2020, 06:14 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020