NEW generic / alias syntax for python 3.12 (PEP 695) (intermediate) anthony explains

  Рет қаралды 10,500

anthonywritescode

anthonywritescode

Күн бұрын

today we go over the new syntax added in python 3.12 for generics and type aliases!
- what's new in python 3.12? • python 3.12 release hi...
- TypeVarTuple and variadic generics: • python variadic generi...
- ParamSpec and decorator typing: • decorator typing (PEP ...
playlist: • anthony explains
==========
twitch: / anthonywritescode
dicsord: / discord
twitter: / codewithanthony
github: github.com/asottile
stream github: github.com/anthonywritescode
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!

Пікірлер: 50
@MostWantedracer
@MostWantedracer 8 ай бұрын
Here's to Anthony: The only guy who's not afraid of opening a browser window in front of you *while recording* and typing something that starts with "P".
@csanadtemesvari9251
@csanadtemesvari9251 8 ай бұрын
Incognito mode exists
@anthonywritescode
@anthonywritescode 8 ай бұрын
it does exist -- but this is just raw google chrome with history on
@calebparks8318
@calebparks8318 8 ай бұрын
For me "p" directs to "pytorch".
@hamzadlm6625
@hamzadlm6625 2 ай бұрын
​@@csanadtemesvari9251 either you don't know what incognito looks like, or you dare to assume he doesn't know that it exists, both are unrecoverably stupid
@unvergebeneid
@unvergebeneid 8 ай бұрын
Well I don't know if I like this new syntax but I know I've always hated the weirdly redundant TypeVar syntax.
@BenjaminWheeler0510
@BenjaminWheeler0510 8 ай бұрын
why didn't they just use the industry standard of ? oh well
@Redstoner237Channel
@Redstoner237Channel 8 ай бұрын
@@BenjaminWheeler0510 wasnt industry standard when python first introduced typing. Now they have to keep with it for backwards compatibility
@anthonywritescode
@anthonywritescode 8 ай бұрын
that's simply not true -- c# generics were 2005, java generics were 2004, c++98 had templates (and some compilers back until 1991 had some form of templates) all with angle brackets long before python typing was even an idea (mypy first released in 2009, python 3.0 syntax was 2008, the typing module was 2015)
@rexase
@rexase 8 ай бұрын
this is great, very Rust-like I think they should use for generics
@rosmelylawliet3919
@rosmelylawliet3919 8 ай бұрын
i would have loved that, as `[...]` is already used for indexes/keys, whereas `` is invalid. i would need to check the pep to see the reason why they went this way, there's likely a reason
@anthonywritescode
@anthonywritescode 8 ай бұрын
my understanding is there's already usage of `[...]` in python so when generics were originally added it was "natural" with the syntax -- whereas angle brackets are only used for comparisons and would have needed a bigger lift to adopt them as a syntax construct maybe if they went back today and redid everything without regards for backwards compatibility it'd be with angle brackets -- but it'd be too much of a lift to just do it now
@amirongoogle4795
@amirongoogle4795 8 ай бұрын
if it is possible, please make a video about variants; thank you.
@mrswats
@mrswats 8 ай бұрын
Good stuff, as per usual!
@AceofSpades5757
@AceofSpades5757 8 ай бұрын
So cool! Hype 🎉
@BrunoBeltran
@BrunoBeltran 8 ай бұрын
Maybe I've been brainwashed by C++ but I find the new syntax very readable and natural.
@bradb4143
@bradb4143 8 ай бұрын
Looks a lot like Go generics syntax, thanks for the video!
@hamzadlm6625
@hamzadlm6625 2 ай бұрын
Thank you for sharing
@workflowinmind
@workflowinmind 8 ай бұрын
5:08 That's the most logical syntax to me, what would you have preferred?
@qexat
@qexat 8 ай бұрын
for someone who uses typevars a lot this syntax kinda grew in me tbh…
@iliya-malecki
@iliya-malecki 8 ай бұрын
why is it def f[T: (str, bytes)](x:T):... and not def f[T: str | bytes](x:T):...? i tried the version that is more intuitive to me and it works. Is it not guaranteed to work? whats the deal with the whole thing?
@anthonywritescode
@anthonywritescode 8 ай бұрын
"it works" -- nothing is checking these in python itself -- it would be up to the typechecker to validate those it's a tuple because that's how it is specced -- yours is different saying that T must be a subclass of a union of int and str -- which is impossible because there exists no class which is a subclass of both int and str (so the typechecker should produce an error)
@iliya-malecki
@iliya-malecki 8 ай бұрын
@@anthonywritescode No no when i say "it works" i mean pyright in strict mode doesnt complain. Also, "a subclass of both int and str" is an intersection type (which i know doesnt exist yet), not a union, so doesnt it mean that T is a subclass of a Union[int, str] if it is a subclass of either int or str?
@anthonywritescode
@anthonywritescode 8 ай бұрын
youtube deleted my response because it was too long -- I misspoke about this and got it backwards but they still mean different things (the union can be satisfied by a union whereas the tuple has distinct bounds) an example: ``` from collections.abc import Sequence def f[T: int | str](a: Sequence[T], b: Sequence[T]) -> list[T]: return [*a, *b] def g[T: (int, str)](a: Sequence[T], b: Sequence[T]) -> list[T]: return [*a, *b] f([1], ['2']) f([1], [2]) f(['1'], ['2']) # g([1], ['2']) not allowed: must be a uniform list g([1], [2]) g(['1'], ['2']) ```
@iliya-malecki
@iliya-malecki 8 ай бұрын
@@anthonywritescode wow, this is massive! This essentially solves the "cat in the list of animals" problem that plagues, among others, typescript. Another huge victory! Can we celebrate or should we wait for intersection types first? Also, I would love a video on intermediate/advanced typing tricks, do you plan on making one? Please do, love your content :)
@anthonywritescode
@anthonywritescode 8 ай бұрын
tbf this already exists but the syntax changes how it works -- you can do the same thing with TypeVar("T", int, str) vs TypeVar("T", bound=int | str) I do occasionally do some tricks like this but they're not always so universally applicable -- maybe I'll cover this one though
@NicolasChanCSY
@NicolasChanCSY 8 ай бұрын
This change does not look very intuitive... In addition, now the function signatures become so long when we add type hints, how should we cope with the 80-ish characters per line rule of PEP8? (Yes, I think 80-ish is still better even when we have much wider screens these days)
@anthonywritescode
@anthonywritescode 8 ай бұрын
it's familiar if you've worked with generics in other languages -- though they tend to use angle brackets instead
@skela3152
@skela3152 8 ай бұрын
You can have multiline function signatures with each argument on it's own line which should eliminate the problem unless you have very, very long function, argument, and type hint names.
@yorailevi6747
@yorailevi6747 8 ай бұрын
At this point, almost all languages are functionally the same with different syntax. everything but maybe rust which can be emulated with robust linter in cpp.
@ismailbello513
@ismailbello513 6 ай бұрын
Hi Anthony, nice video as always! would be nice to hear your thoughts on typing.Annotated!
@anthonywritescode
@anthonywritescode 6 ай бұрын
tbh I haven't really used it, I steer far away from runtime annotations
@fuadnafiz98
@fuadnafiz98 8 ай бұрын
How do you remember all the syntaxs without any code completion or autosuggesions? 🙄
@timbrap4693
@timbrap4693 8 ай бұрын
Some people are just built different
@juniorceccon
@juniorceccon 8 ай бұрын
That kind of skill is built over time. Eventually, you get used to it.
@IonizedComa
@IonizedComa 8 ай бұрын
its easier to do it with python because it's an easily read language, i'm able to do it with C as well. But Java and C++ I have problems remembering most things. Also depends on which language you use often
@BenjaminWheeler0510
@BenjaminWheeler0510 8 ай бұрын
I like types, yes I do. I like types, how about you?
@blanky_nap
@blanky_nap 8 ай бұрын
after watching this video i have only one question: python wtf are you doing?
@float32
@float32 8 ай бұрын
I’m thinking of moving to swift. If I wanted all this complexity, I would use a compiled language and get 10x the performance, so might as well.
@yomajo
@yomajo 8 ай бұрын
For most of my usecases I use built in types or my own classes for typehints, and have no idea what a Generic is... This seems like a woke trend in python. A shame.
@lonterel4704
@lonterel4704 8 ай бұрын
​@@float32but swift is not good outside of macos. What do you use Python for?
@ericng8807
@ericng8807 8 ай бұрын
3:00 AI grifters fuming right now
@calebparks8318
@calebparks8318 8 ай бұрын
Hurray! Good riddance to "from ___future___ import annotations."
@MrYevelnad
@MrYevelnad 8 ай бұрын
IDK why python implemented generics when its a dynamically typed language. I'm kinda hyped when I first knew this and it is much simpler to implement in 3.12. I play with it a bit and came to a conclusion that it is pretty much useless in python.
@anthonywritescode
@anthonywritescode 8 ай бұрын
it's not for the runtime, it's for type checkers
@mailoisback
@mailoisback 8 ай бұрын
No performance gain, just useless syntax stuff that is littering Python.
@anthonywritescode
@anthonywritescode 8 ай бұрын
technically this performs better than TypeVar(...) calls since it's all lazily evaluated -- and 3.12 did improve performance quite a bit for basically everything not defending the syntax in any way -- just correcting your baseless complaints
@user-qi5kb5th7y
@user-qi5kb5th7y 2 ай бұрын
yada yada, try supporting a project without type hints, where everything is a plain dict then we'll talk about 'littering'
new 3.12 f-strings syntax! (intermediate) anthony explains #562
5:52
anthonywritescode
Рет қаралды 6 М.
python is removing the GIL! (PEP 703) (advanced) anthony explains #550
24:04
Gym belt !! 😂😂  @kauermtt
00:10
Tibo InShape
Рет қаралды 17 МЛН
Каха заблудился в горах
00:57
К-Media
Рет қаралды 7 МЛН
Python 3.12 Generic Types Explained
18:27
ArjanCodes
Рет қаралды 59 М.
5 More Useful F-String Tricks In Python
9:38
Indently
Рет қаралды 45 М.
Covariance and Contravariance
13:31
Christopher Okhravi
Рет қаралды 12 М.
python Generics (intermediate) anthony explains #430
13:43
anthonywritescode
Рет қаралды 15 М.
py-spy saved our python 3.11 rollout (intermediate) anthony explains #568
13:48
Python 3.12 is HERE!
12:37
mCoding
Рет қаралды 157 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 285 М.
What's new in Python 3.13?
5:08
Carberra
Рет қаралды 77 М.
WHY IS THE STACK SO FAST?
13:46
Core Dumped
Рет қаралды 143 М.
Как бесплатно замутить iphone 15 pro max
0:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН
Запрещенный Гаджет для Авто с aliexpress 2
0:50
Тимур Сидельников
Рет қаралды 710 М.
Как распознать поддельный iPhone
0:44
PEREKUPILO
Рет қаралды 2,3 МЛН
НОВЫЕ ФЕЙК iPHONE 🤯 #iphone
0:37
ALSER kz
Рет қаралды 317 М.
Здесь упор в процессор
18:02
Рома, Просто Рома
Рет қаралды 429 М.
Looks very comfortable. #leddisplay #ledscreen #ledwall #eagerled
0:19
LED Screen Factory-EagerLED
Рет қаралды 5 МЛН