On Sat, 13 Feb 2021 at 07:11, Steven D'Aprano <steve@pearwood.info> wrote:
On Fri, Feb 12, 2021 at 10:27:01AM +0000, Mark Shannon wrote:
It impairs readability, because it muddles the return type. The function in the example returns a bool. The annotation is also misleading as the annotation is on the return type, not on the parameter that is narrowed.
At a glance, most programmers should be able to work out what
def is_str_list(val: List[object]) -> bool:
returns.
But,
def is_str_list(val: List[object]) -> TypeGuard[List[str]]:
is likely to confuse and require careful reading. Type hints are for humans as well as type checkers.
This!
Without reading the PEP, how is anyone supposed to know that this returns a bool?
I have to agree here. I'm not a frequent user of type hints yet, but I am starting to have to maintain code that has type hints, and from a maintenance perspective, I have to say that this would be really hard to deal with. If I saw this for the first time "in the wild", I'd have no idea what to do with it. Paul