Home > OS >  Python: "Tuple" is not defined warning
Python: "Tuple" is not defined warning

Time:01-20

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?

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

CodePudding user response:

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.

  •  Tags:  
  • Related