Home > Net >  Python Type Annotations: Mark item in tuple as None
Python Type Annotations: Mark item in tuple as None

Time:03-05

How can I type annotate a tuple on a return where either of them might be None?

from typing import Tuple

def foo(bar:int) -> Tuple[int:
    if bar > 10:
      return 1, None
    elif bar < 5:
      return None, 1
    else:
      return 1, 2

CodePudding user response:

def foo(bar:int) -> tuple[int | None, int | None]:
if bar > 10:
  return 1, None
elif bar < 5:
  return None, 1
else:
  return 1, 2

Like this ?

  • Related