Тёмный

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

anthonywritescode
Подписаться 41 тыс.
Просмотров 7 тыс.
50% 1

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!

Опубликовано:

 

27 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 29   
@jackevansevo
@jackevansevo 3 дня назад
Great job explaining what's going on.
@alessio622
@alessio622 3 месяца назад
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.
@rtshadfhab7125
@rtshadfhab7125 9 дней назад
I love your videos. You have a calm presence, easy to understand and very informative. I really appreciate your content. I’ve been programming with python since 2 became three. Before that it was Java and c. I was wondering if you do any C or object pascal videos?
@anthonywritescode
@anthonywritescode 8 дней назад
a tiny bit of c but imo people should write in a safer language at the end of the day
@mrswats
@mrswats 3 месяца назад
It's so funny that a Typing video is full of typos: P
@itachi2011100
@itachi2011100 3 месяца назад
wouldn't like it any other way
@jrheard
@jrheard 3 месяца назад
Love this, thanks Anthony!
@TheAulto
@TheAulto 3 месяца назад
he is back!
@bswck
@bswck 3 месяца назад
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 3 месяца назад
the enum approach is the only one I know that works well with mypy and narrowing
@sgeisenh
@sgeisenh 3 месяца назад
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 3 месяца назад
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 3 месяца назад
@@anthonywritescode that makes sense. Type annotations do seem to encourage a safer subset of the language. Thanks for the reply!
@ChristianBrugger
@ChristianBrugger 3 месяца назад
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 😂
@dibri
@dibri 6 дней назад
i mean, a ton of python code tends to impl covariant methods. maybe mypy needs to support bivariance too and any other typing supports to preserve idiomatic python expressions
@dibri
@dibri 6 дней назад
@@sgeisenh not when the anys show up every where.
@antoniov845
@antoniov845 Месяц назад
Class LazySingleton
@kidhq
@kidhq 3 месяца назад
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 3 месяца назад
None, Ellipsis, etc. would have worked for most cases, though Django's implementation uses an explicit empty singleton
@kilianvounckx9904
@kilianvounckx9904 3 месяца назад
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
@TheRealisticNihilist
@TheRealisticNihilist 2 месяца назад
Why is this guy using nano wtf?
@anthonywritescode
@anthonywritescode 2 месяца назад
it's better than that :) check the header (nano is an alias because I haven't fixed my habits)
@thecodealchemist7095
@thecodealchemist7095 3 месяца назад
RU-vid algorithm likez me 😂
@EinsteinNewtonify
@EinsteinNewtonify 2 месяца назад
So there is no python built in way to handle that?
@MrAlanCristhian
@MrAlanCristhian 3 месяца назад
I didn't know that mypy plugins was a thing
@hassaanalansary
@hassaanalansary 3 месяца назад
Great as always
@Zhaxxy
@Zhaxxy 2 дня назад
5:46 true programmer, 10:42am is early in the morning
Далее
Help Me Celebrate! 😍🙏
00:35
Просмотров 21 млн
Китайка и Максим Крипер😂😆
00:21
Python: A Quick Guide To Type Annotations (ft. Mypy)
11:25
Being Competent With Coding Is More Fun
11:13
Просмотров 79 тыс.
A Jr Dev For Life?? | Prime Reacts
21:33
Просмотров 305 тыс.
Python 3.12 Generic Types Explained
18:27
Просмотров 61 тыс.
TypeScript Generics are EASY once you know this
22:21
Просмотров 137 тыс.
You Are WRONG About 0 Based Indexing
25:02
Просмотров 290 тыс.
Help Me Celebrate! 😍🙏
00:35
Просмотров 21 млн