22
submitted 1 year ago* (last edited 1 year ago) by 0WN3D@lemmy.cafe to c/python@programming.dev

I know what I am asking is rather niche, but it has been bugging me for quite a while. Suppose I have the following function:

def foo(return_more: bool):
   ....
    if return_more:
        return data, more_data
   return data

You can imagine it is a function that may return more data if given a flag.

How should I typehint this function? When I use the function in both ways

data = foo(False)

data, more_data = foo(True)

either the first or the 2nd statement would say that the function cannot be assigned due to wrong size of return tuple.

Is having variable signature an anti-pattern? Is Python's typehinting mechanism not powerful enough and thus I am forced to ignore this error?

Edit: Thanks for all the suggestions. I was enlightened by this suggestion about the existence of overload and this solution fit my requirements perfectly

from typing import overload, Literal

@overload
def foo(return_more: Literal[False]) -> Data: ...

@overload
def foo(return_more: Literal[True]) -> tuple[Data, OtherData]: ...

def foo(return_more: bool) -> Data | tuple[Data, OtherData]:
   ....
    if return_more:
        return data, more_data
   return data

a = foo(False)
a,b = foo(True)
a,b = foo(False) # correctly identified as illegal
you are viewing a single comment's thread
view the rest of the comments
[-] UndercoverUlrikHD@programming.dev 13 points 1 year ago

from typing import Union is probably what you're looking for, but yes, I'd argue you should try to avoid that kind of pattern, even if it's convenient.

Sorry for the triple(?) notifications. Trying out the beta version of the boost app and it's still a bit buggy.

[-] 0WN3D@lemmy.cafe 1 points 1 year ago

I thought about it, but it isn't as expressive as I wished.

Meaning if I do

a = foo(return_more=True)
or
a, b = foo(return_more=False)

it doesn't catch these errors for me.

In comparison, the other suggested solution does catch these.

Yeah, good point, the linked answer seems better suited (even if I would still recommended not having a variable return). I appreciate the feedback!

this post was submitted on 23 Sep 2023
22 points (95.8% liked)

Python

6356 readers
10 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS