Hello, On Sat, 13 Feb 2021 18:08:30 +1100 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? This is obfuscated code. It looks like it returns an object of type `TypeGuard`. Even having read the PEP, it's still misleading: instead of a straightforward return type, I have to memorise that `TypeGuard[...]` is a magic thing that means
* the return type is actually a bool;
* and the type checker can narrow the type of the first parameter.
Yuck.
My quick reading of PEP647 shows that it's yet another patchy/throw-away PEP whose purpose is to work around deficiencies in the current generation of Python typecheckers. In that regard, I'd take it easy, and be +1 with it - it's better to support gradual evolution of Python typecheckers with hacks like that, rather than block it with proverbial "we can solve all the problems of universe right in the version 1". [] -- Best regards, Paul mailto:pmiscml@gmail.com