8

I'm writing a function that returns a tuple and using type annotations. To do so, I've written:

def foo(bar) -> Tuple[int, int]:
    pass

This runs, but I've been getting a warning that says:

"Tuple" is not defined Pylance report UndefinedVariable

Given that I'm writing a number of functions that return a tuple type, I'd like to get rid of the warning. I'm assuming I just need to import the package Tuple refers to, but what is the right Python package for it?

From my research, I'm inclined to think it's the typing package, but I'm not sure.

dorian.
  • 134
  • 1
  • 1
  • 10

1 Answers1

14

Depending on your exact python version there might be subtle differences here, but what's bound to work is

from typing import Tuple

def foo(bar) -> Tuple[int, int]:
  pass

Alternatively I think since 3.9 or 3.10 you can just outright say tuple[int, int] without importing anything.

cadolphs
  • 9,014
  • 1
  • 24
  • 41
  • Thanks, I've implemented the second solution, although as written, it initially gave a `Subscript for class "tuple" will generate runtime exception; enclose type annotation in quotes`. – dorian. Jan 20 '22 at 03:05
  • 1
    This worked with `def foo(bar) -> "tuple[int, int]":` – dorian. Jan 20 '22 at 03:06