Eric Traut wrote:
"""It must be possible to express the type guard within the function signature. In other words, the implementation should not need to be present. This is important for compatibility with type stubs and to guarantee consistent behaviors between type checkers."""
Can type stubs include a docstring? If so, then adding subsequent lines that are only parameter annotations doesn't seem like a problem.
And frankly, allowing subsequent lines (possibly restricted to first-except-docstrings) doesn't seem any more invasive than adding another magic type that has to be interpreted differently. (I understand it might be more coding in practice, based on one solution having already been written.)
def is_str_list(val: List[object]) -> bool: """Determines whether all objects in the list are strings""" val: NarrowsTo[List[str]]
I agree that Greg's suggestion of
def is_str_list(val: Constrains[List[object]:List[str]) -> bool:
also meets the criteria you list, but ... as a human reader, that signature is getting too heavy.