5
def func(df_a: pd.DataFrame, df_b: pd.DataFrame) -> (pd.DataFrame, pd.DataFrame):

Pylance is advising to modify this line with two solution proposed. What would be the pros and cons of each one if there is any significant difference?

Tuple expression not allowed in type annotation

Use Tuple[T1, ..., Tn] to indicate a tuple type or Union[T1, T2] to indicate a union type

2 Answers 2

12

2023 edit

In newer versions of Python (>=3.10), you should use:

  • tuple[A, B, C] instead of Tuple[A, B, C] (yes, that's the built-in tuple function)
  • A | B instead of Union[A, B]

The answer itself is still relevant, even if the newer style makes the difference between Tuple/tuple and Union/| more apparent.

Original answer

They mean different things:

  • Tuple[A, B, C] means that the function returns a three-element tuple with the A B C data types:
def f() -> Tuple[str, int, float]:
    return 'hello', 10, 3.33
  • Union[A, B] means that the function returns an object of either A or B data type:
import random 

def f() -> Union[str, int]:
    if random.random() > 0.5:
        return 'hello'
    else:
        return 10

In your case, it looks like you want to use Tuple[pd.DataFrame, pd.DataFrame].

2
  • Thank you for this clear answer ! Commented Jan 31, 2022 at 17:04
  • 3
    You should use tuple rather than Tuple here, as the latter is depreciated. Commented Jan 31, 2022 at 17:06
1

A union type wouldn't make sense here since the types are the same.

If you want to indicate that a tuple is being returned, you'd use Tuple[pd.DataFrame, pd.DataFrame] in older versions of Python, or tuple[pd.DataFrame, pd.DataFrame] in newer versions of Python. See here for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.