Another advantage of annotating the variable is that it moves some stuff out
the signature line.
def is_str_list(val: List[object]) -> TypeGuard[List[str]]:
is probably OK on length, but if there were even a second typed and defaulted
parameter, it would start to get unwieldy. And if the answer is "there won't
be, these are simple functions", then that sort of screams that these are a
special kind of function that should be decorated to alert readers to the
restriction.
def is_str_list(val: List[object]) -> bool:
val: NarrowsTo[List[str]]
is still near the top (like a docstring), and doesn't require a change in how
to interpret existing syntax. Someone who doesn't care about typing can wonder
why you bothered to quote/assert val, just as they can wonder why you did that
to a docstring, but it will be (almost) as obvious that the line doesn't *do*
anything -- so at least they won't assume it is doing something else (such as
returning some custom type that you happen to call TypeGuard).
-jJ
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/IZULNMCU6SHAFDO4BFGWEDE3ZNUHWPFZ/
Code of Conduct: http://python.org/psf/codeofconduct/