Strong -1 from me. The pathlib case should already be handled by typeshed, and the second can already be handled by a type declaration:
list_here = [1,'2',3]
for i in list_here:
For this, mypy gives the error
ttyp.py:3: error: Incompatible types in assignment (expression has type "object", variable has type "int") [assignment]
Found 1 error in 1 file (checked 1 source file)
For your other examples, you’re assuming untyped functions with your “let’s assume X comes from an external library”. This isn’t a reasonable assumption - you can add type hints for external functions using stub files. This is an explicitly documented and intended use case for stubs.
Overall, type checkers seem to already have this well covered. And in addition, the proposed syntax is intrusive and ugly (IMO) so I’d very strongly object to seeing it in any projects I control.
Wow this is cool! Haven’t seen this function before - you’re right it does provide the same utility. I suppose the only point remaining is brevity - the ! syntax I describe is quite a bit shorter than using this typing function, and also would come as a built-in - so no need for the import at the top. That said, with a short choice of import name it can be quite brief:
from typing import assert_type as at
# here let's assume get_list comes from an external library
r = random.uniform(1)
if r <=0.5:
return list(['test2', 'test3'])
if at(get_list(), list[str]):
In some way I suppose python progressed from the Union syntax, to using | - similarly perhaps there is some way assert_type could have a syntax upgrade