Python Forum
mypy unable to analyse types of tuple elements in a list comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mypy unable to analyse types of tuple elements in a list comprehension
#1
Question 
All presented code samples were tested with Python 3.11, and mypy 1.6.0.

And the problem is: mypy sees no problems in the following code:

MyTupleType = tuple[int, str, bool]
Tuples = list[MyTupleType]

def findAllByInt(values : Tuples, ref : int) -> Tuples:
  return [v for v in values if v[1] == ref]  # here is the bug!

vals : Tuples = [ (1, 'a', True), (2, 'b', False),
                  (3, 'c', True), (1, 'd', False) ]

print( findAllByInt(vals, 1) )
The problem is the tuple element index, which is compared to the referenced value. The element index should be 0, not 1. v[1] is a str, while v[0] is an int.

The only solution I was able to achieve is to use a separate function to extract the tuple element:

MyTupleType = tuple[int, str, bool]
Tuples = list[MyTupleType]

def intOfMyTuple(t : MyTupleType) -> int:
  return t[0]

def findAllByInt(values : Tuples, ref : int) -> Tuples:
  return [v for v in values if intOfMyTuple(v) == ref]

vals : Tuples = [ (1, 'a', True), (2, 'b', False),
                  (3, 'c', True), (1, 'd', False) ]

print( findAllByInt(vals, 1) )
Now, when we change the element index in intOfMyTuple, mypy will complain about the type mismatch, which is exactly what mypy should do.

The solution, though, is cumbersome: is it really neccessary to write separate functions for each tuple and element index I use in my code?

Or, perhaps, I'm doing something wrong, and mypy can be used to properly check the types in the first code sample?
Reply
#2
OK, I have found a solution.

The cause of problem is in the return line of this function:

def findAllByInt(values : Tuples, ref : int) -> Tuples:
  return [v for v in values if v[1] == ref]  # here is the bug!
More precisely: the comparison v[1] == ref. The point is, this comparison is fine: there's nothing wrong with comparing apples to oranges. Of course, such comparisons will always be negative, since apples are not oranges (and in Python: ints are not strs).

The solution is to use --strict-equality mypy option, which makes such comparisons an error.

Sorry for bothering you with my problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 487 Feb-05-2024, 01:18 PM
Last Post: deanhystad
  unable to remove all elements from list based on a condition sg_python 3 458 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  List Comprehension Issue johnywhy 5 546 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  mypy, type aliases and type variables tomciodev 1 717 Oct-18-2023, 10:08 AM
Last Post: Larz60+
  problem in using mypy akbarza 17 3,083 Sep-06-2023, 01:53 PM
Last Post: snippsat
  Change font in a list or tuple apffal 4 2,700 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  Using list comprehension with 'yield' in function tester_V 5 1,264 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Checking if a string contains all or any elements of a list k1llcod3 1 1,115 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 2,011 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  search a list or tuple for a specific type ot class Skaperen 8 1,949 Jul-22-2022, 10:29 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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