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
  List comprehension not working right Cris9855 3 921 Nov-04-2024, 03:46 PM
Last Post: DeaD_EyE
  Problem with List Comprehension in Python laurawoods 3 1,064 Aug-12-2024, 06:26 AM
Last Post: Pedroski55
  using > < for tuple , list,... akbarza 3 1,665 Feb-05-2024, 01:18 PM
Last Post: deanhystad
  unable to remove all elements from list based on a condition sg_python 3 1,640 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  List Comprehension Issue johnywhy 5 1,830 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  mypy, type aliases and type variables tomciodev 1 1,835 Oct-18-2023, 10:08 AM
Last Post: Larz60+
  problem in using mypy akbarza 17 8,766 Sep-06-2023, 01:53 PM
Last Post: snippsat
  Change font in a list or tuple apffal 4 3,693 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  Using list comprehension with 'yield' in function tester_V 5 3,410 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Checking if a string contains all or any elements of a list k1llcod3 1 4,695 Jan-29-2023, 04:34 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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