Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: "Tuple" is not defined warning

Tags:

python

pylance

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.

like image 735
dorian. Avatar asked Feb 06 '26 08:02

dorian.


1 Answers

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.

like image 81
Lagerbaer Avatar answered Feb 09 '26 10:02

Lagerbaer