Bottom Page

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?
Quote
#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

Life of Brian: Conjugate the verb, "to go" !
Quote
#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].
Quote
#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.
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Type Error: bad operand type for unary +: 'str' Psypher1 2 113 Aug-12-2019, 02:30 PM
Last Post: Psypher1
  Not quite getting the correct Output from a function twinpiques 3 163 Aug-04-2019, 11:53 PM
Last Post: twinpiques
  read text file and write into html with correct link jacklee26 4 161 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  Parsing json - dictionary with in dictionary krish216 6 297 Jul-30-2019, 10:01 PM
Last Post: cvsae
  __getattr__ and type hint itaybardugo 0 137 Jul-04-2019, 09:50 PM
Last Post: itaybardugo
  Help with finding correct topic in Python learning yahya01 1 284 Jun-06-2019, 05:01 PM
Last Post: buran
  Return in correct json format UtiliseIT 3 349 May-13-2019, 11:24 AM
Last Post: snippsat
  How to continue in loop until correct input received sunnyarora 10 485 May-04-2019, 02:37 PM
Last Post: Yoriz
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 290 Apr-27-2019, 03:13 AM
Last Post: perfringo
  Pyodbc does not accept variable from tkinter diegoctn 9 491 Feb-12-2019, 05:12 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)