Python Has A NewType That No One Is Using?

preview_player
Показать описание
In todays video we will be looking at another uncommon typing feature provided by the typing module in Python.

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Intro
00:18 What is type aliasing?
01:41 How to use NewType
04:07 Your thoughts?
Рекомендации по теме
Комментарии
Автор

I love it that the python devs have always philosophised that types don't matter but the community has collectively tried to jam types into it as far in as possible. I love it...

mayank
Автор

I guess there is a difference with "the editor gives a warning" vs. "does not permit execution"
The example @2:32 is just a warning, but will execute as expected with find_user(10)

ygstuff
Автор

So to use more technical language, "UserID = int" is a bivariant (both covariant and contravariant) derivation, while "UserID = NewType('UserID', int)" is only a covariant derivation.

Covariant means that wherever an int is expected, you can also use a UserID. This is why the print(UserID(10)) function works in both cases.

Contravariant means that wherever a UserID is expected, you can also use an int. This is why find_user(10) only works in the first case.

Covariance (without contra- / bivariance) is how normal class derivation that you're used to works, just like NewType does as well.

bttlemastr
Автор

Finally I can organise my types in python. This is one of the things I love about c++, and now I can finally somewhat replicate custom type strictness!

AlmondAxis
Автор

Some people never programmed with strongly typed languages so won’t recognise the benefits immediately. In big Python projects the problem is to know what type does the variable contain, and if you know it’s type like a str you still might not know what it represents. To be able to quickly reason about the code and do some mypy style of checks you can use type annotations. In terms of strictness level you can go from not having a type hint -> basic type hint like int -> NewType which differentiates between int and int -> class that can asset correct construction. NewType means that I didn’t pass some variable by accident but took care to construct it correctly (not enforced by language though) and you IDE or mypy will complain otherwise. For me it would be easier to read def get_user(uid: UuidStr) than def get_user(uid: str) where I don’t know what str means. Better yet you convert it to real uuid class but not always makes sense. If you only write def get_user(uid) it won’t take you far.

AI-xijk
Автор

I see that python has finally added nominal types to the language, I was hoping to see TS add them to the language so that we don't have to do any weird hacks to get them, this will be very useful for validation

markzuckerbread
Автор

So good for create types for data mining and analisys. Tks

esseeoaminho
Автор

This is new to me, but I found your video at the exact perfect time in a project I'm working on at work. I want to have some type aliases for simple types but not have them interchangeable with the simple types because my lazy team will just use str or int instead of the type names I intended. Perfect.

vorpal
Автор

The lack of Gundam references in the comments so utterly devastating.

MobiusCoin
Автор

I agree with the comments above. This is interesting but it seems something purely esthetic. What’s the practical use of all this? I would have understood if passing an int instead of userID would have triggered an error.

andreaardemagni
Автор

It seems like if dataclass is a lightweight class then NewType is a bare-bones class. It would be very easy to refactor UserId into a class e.g. maybe the init function has some extra logic like checking against a list of valid IDs. I can see myself using NewType to prototype some code where I'm not sure yet whether UserId needs to be a full class

DownThereForDancing
Автор

Maybe a better use case would be to use it with time. Without it you would write something like:
def int, endTimeInSeconds: int) -> int:
pass

And this just sucks if you just could write this instead:

def timePassed(start: seconds_t, end: seconds_t) -> seconds_t):
pass

chaxs_
Автор

what kind of IDE/Text Editor do you use?

oldpersonalaccount
Автор

What editor are you using? Doesn't look like vscode. Doesn't look like pycharm either

vikingthedude
Автор

I've been using typed python in production for quite some time now, and I truly don't find any reason to build a distinct type to wrap an already existing type no matter how complex it is, I don't think it really improves semantics in any way

I still assume there's a reason to implement this feature, but I couldn't find any practical example for it - I literally found the same user_id example on multiple sites including the docs, could you find a practical case where this is useful?

reg.x
Автор

which text editor are you using? it looks neat

suou
Автор

I guess this is about as close to opaque types that we'll get in Python for now.

vorpal
Автор

I think the final point that you can still compare a UsedId with an int pretty much breaks the whole concept

transientaardvark
Автор

Why not use "class UserId(int): pass"

Optimistas
Автор

I have always used NewType in my projects 😂 thought it was a common practice 😅😅

thomasricatte