python TypeGuard (PEP 647) (intermediate) anthony explains #439

preview_player
Показать описание
today we go over narrowings and `TypeGuard` -- which enables you to write your own user-defined narrowings!

==========

I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below!
Рекомендации по теме
Комментарии
Автор

Wow, now I don't have to use C++ to ponder at template typing mismatch errors, now I can do it with Python!

luck
Автор

Anthony: "What!?"
That's what I find myself saying way more often than I'd like.

ChrisBNisbet
Автор

I know this is an old video, but for those watching: instead of binding T to type, type[T] should be used as the parameter type.

tannerbobanner
Автор

i suppose you can still do `bound=type` for the TypeVar there, it's just like `bound=object` but accepts any class instead of any object.

sadhlife
Автор

consider this case for the negative case:

```
def is_zero(x: int | None) -> TypeGuard[int]:
return x == 0

def foo(x: int | None) -> None:
if is_zero(x):
print("got zero")
else:
reveal_type(x) # int | None
```

sadhlife
Автор

@4:40 you say this is a variance issue. Do you have a video that explains more about it?

RoyAAD
Автор

Oh. I just want to write comment that this is not your style to let unclear stuff exist and then i see this addition. You met my expectation :)

marsma
Автор

when i try to import TypeGuard mypy tells me that its not a valid type. I tried importing it from the typing module and the typing_extensions module and I still get the same error.

aldrickdev
Автор

I see that "fumble" not as a failure of you Anthony, but as a testament to how clumsy and unintuitive python typing becomes at higher levels

OrCarmi
Автор

Why did you used `object` instead of `typing.Any` ? is there any practical difference?

xaro
Автор

Anthony: "This is a disaster..." Sometimes I think that about type hints in Python in general.

alexeiguirik