No video

typing the untype-able with mypy plugins (advanced) anthony explains

  Рет қаралды 6,738

anthonywritescode

anthonywritescode

Күн бұрын

today I show an approach to make mypy understand a very dynamic pattern with a plugin!
playlist: • anthony explains
==========
twitch: / anthonywritescode
dicsord: / discord
twitter: / codewithanthony
github: github.com/aso...
stream github: github.com/ant...
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!

Пікірлер: 23
@alessio622
@alessio622 2 ай бұрын
Very very interesting topic. I’m using this to implement a plugin for factoryboy type annotations (types-factoryboy). It’d be great to have more videos like this explaining how to write mypy plugins, as there is very little “digested” information available, only “just read what others did” kind of documentation.
@mrswats
@mrswats 2 ай бұрын
It's so funny that a Typing video is full of typos: P
@itachi2011100
@itachi2011100 2 ай бұрын
wouldn't like it any other way
@sgeisenh
@sgeisenh 2 ай бұрын
I'd be curious to hear your thoughts on how you expect/hope the type system evolves to _hopefully_ support concepts like proxy objects without the need for a plugin. Type annotations are definitely a big improvement when interacting with an existing codebase, but it still feels like a different language. The model I think of is TypeScript where there's reasonable support for type-level computations. But JS exposes far fewer implementation details, so it seems simpler to implement a type system that captures common forms of "dynamism" in JS. TS definitely has significant deficiencies (e.g. bivariant method arguments) but it seems like there may still be a lot of ideas that python could draw to be able to fully annotate a larger variety of constructs.
@anthonywritescode
@anthonywritescode 2 ай бұрын
python has significantly more magic than js ever would have to deal with (descriptors, meta classes, etc.) such that I don't think the type system will (or even should!) represent every thing you can do. the only reason that I did make the proxy "work" is because it already existed. if the code were written with safety in mind such a pattern would not have been used
@sgeisenh
@sgeisenh 2 ай бұрын
@@anthonywritescode that makes sense. Type annotations do seem to encourage a safer subset of the language. Thanks for the reply!
@ChristianBrugger
@ChristianBrugger 2 ай бұрын
Thanks for that comment and adding that context. Good to know these tools are there, but better to design it, such that this is not needed 😂
@jrheard
@jrheard 2 ай бұрын
Love this, thanks Anthony!
@bswck
@bswck 2 ай бұрын
there are various approaches for creating sentinels. the one with enum, but also `MISSING = type("_MISSING", (), {})()` or creating a custom class with the class keyword and then reusing a single instance of it is what I tend to use if I don't want `object()`. or even importing MISSING from dataclasses could work, but well. I'm not sure it's a good idea. everything depends whether you need concrete unions or a killer type "object" in a variable (or, more often, a parameter).
@anthonywritescode
@anthonywritescode 2 ай бұрын
the enum approach is the only one I know that works well with mypy and narrowing
@antoniov845
@antoniov845 14 күн бұрын
Class LazySingleton
@TheAulto
@TheAulto 2 ай бұрын
he is back!
@kidhq
@kidhq 2 ай бұрын
For the singleton field `Lazy._inst`, my first idea would've just been to store `None` and type as `Optional[R]`. Is there a benefit to using the specific `empty` object with the _EmptyType enum instead? Related timestamp: 1:14 for creating empty object, 14:10 for updating with enum type
@anthonywritescode
@anthonywritescode 2 ай бұрын
None, Ellipsis, etc. would have worked for most cases, though Django's implementation uses an explicit empty singleton
@kilianvounckx9904
@kilianvounckx9904 2 ай бұрын
If, for some reason, your expensive function returns None, you wouldn't be able to store it if you also used is as the singleton field. You would check against None and the function would run always because it would refer to both the actual lazily computed object as the singleton. Using an empty object and the `is` operator takes care of this, because `is` compares by reference/address. This address is guaranteed to be the one of the singleton
@hassaanalansary
@hassaanalansary 2 ай бұрын
Great as always
@MrAlanCristhian
@MrAlanCristhian 2 ай бұрын
I didn't know that mypy plugins was a thing
@EinsteinNewtonify
@EinsteinNewtonify Ай бұрын
So there is no python built in way to handle that?
@TheRealisticNihilist
@TheRealisticNihilist Ай бұрын
Why is this guy using nano wtf?
@anthonywritescode
@anthonywritescode Ай бұрын
it's better than that :) check the header (nano is an alias because I haven't fixed my habits)
@thecodealchemist7095
@thecodealchemist7095 2 ай бұрын
KZfaq algorithm likez me 😂
Doing This Instead Of Studying.. 😳
00:12
Jojo Sim
Рет қаралды 35 МЛН
managed to catch #tiktok
00:16
Анастасия Тарасова
Рет қаралды 40 МЛН
Parenting hacks and gadgets against mosquitoes 🦟👶
00:21
Let's GLOW!
Рет қаралды 13 МЛН
女孩妒忌小丑女? #小丑#shorts
00:34
好人小丑
Рет қаралды 54 МЛН
Pydantic is OP, here's why
18:10
Carberra
Рет қаралды 22 М.
py-spy saved our python 3.11 rollout (intermediate) anthony explains #568
13:48
Python's Type Annotations DON'T Do What You THINK They Do
8:10
Python itertools - The key to mastering iteration
20:03
mCoding
Рет қаралды 32 М.
prefer tuples to lists! (intermediate) anthony explains #564
6:39
anthonywritescode
Рет қаралды 9 М.
Python: A Quick Guide To Type Annotations (ft. Mypy)
11:25
Indently
Рет қаралды 30 М.
Note-taking Apps for Command-line People
34:49
Mischa van den Burg
Рет қаралды 37 М.
The Worst Programming Language Ever - Mark Rendle - NDC Oslo 2021
1:00:41
NDC Conferences
Рет қаралды 1,3 МЛН
Doing This Instead Of Studying.. 😳
00:12
Jojo Sim
Рет қаралды 35 МЛН