this post was submitted on 23 Sep 2023
21 points (95.7% liked)

Python

6347 readers
2 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
 

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
all 14 comments
sorted by: hot top controversial new old
[–] UndercoverUlrikHD@programming.dev 12 points 1 year ago (1 children)

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 (1 children)

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!

[–] PapstJL4U@lemmy.world 9 points 1 year ago

always return more, than you can

data, _ = foo(false)

data, more_data = foo(true)

and write a good documentation in the function, why it has different return amounts.

A boolean toggle should influence the process, but not change the sigmature. Maybe two functions are better?

getfoo() and getmorefoo()?

[–] __init__@programming.dev 6 points 1 year ago (3 children)

You may be able to achieve this using typing.Overload with typing.Literal for your argument. Check out this post about overload: https://adamj.eu/tech/2021/05/29/python-type-hints-how-to-use-overload/

[–] nikaro@jlai.lu 4 points 1 year ago

Nice! It looks like the best solution out there.

[–] 0WN3D@lemmy.cafe 2 points 1 year ago* (last edited 1 year ago)

yea, this is pretty close to what I'm looking for.

The only missing piece is the ability to define the overload methods on the bool

something like

@overload
def foo(return_more: True) -> (Data, Data)

@overload
def foo(return_more: False) -> Data

But I don't think such constructs are possible? I know it is possible in Typescript to define the types using constants, but I don't suppose Python allows for this?

EDIT: At first, when I tried the above, the typechecker said Literal[True] was not expected and I thought it was not possible. But after experimenting some, I figured out that it is actually possible. Added my solution to the OP

Thanks for the tip!

[–] eternacht@programming.dev 1 points 1 year ago

This is the real answer, overloads are meant for exactly this purpose.

It’ll be something like this:

from typing import Literal, overload

@overload
def foo() -> Data: …
@overload
def foo(return_more: Literal[True]) -> tuple[Data, Data]: …
def foo(return_more: bool = False) -> Data | tuple[Data, Data]
    ...
    if return_more:
        return data, more_data
   return data
[–] madwifi@lemmy.world 3 points 1 year ago* (last edited 1 year ago) (2 children)

def foo(return_more: bool) -> Union[Type1, tuple[Type2,Type3]]:

[–] nikaro@jlai.lu 4 points 1 year ago* (last edited 1 year ago)

Python >= 3.10 version:

def foo(return_more: bool) -> DataType | tuple[DataType, MoreDataType]: ...

But i would definitely avoid to do that if possible. I would maybe do something like this instead:

def foo(return_more: bool) -> tuple[DataType, MoreDataType | None]:
    ...
    if return_more:
        return data, more_data
   return data, None

Or if data is a dict, just update it with more_data:

def foo(return_more: bool) -> dict[str, Any]:
    ...
    if return_more:
        return data.update(more_data)
   return data
[–] gigachad@feddit.de 1 points 1 year ago* (last edited 1 year ago)

You can also consider the new union that was introduced with Python 3.10, check PEP604 for details:

def foo(return_more: bool) -> Type1 | tuple[Type2,Type3]:

[–] xigoi@lemmy.sdf.org 1 points 1 year ago
def foo_return_more():
    ...
    return data, more_data

def foo():
    return foo_return_more()[0]