Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Type Annotations: Mark item in tuple as None

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading