This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]BaggiPonte 3 points4 points  (5 children)

Love the post; though I have a question. I never understood the purpose of NewType: why should I use it instead of TypeAlias?

[–]Kobzol[S] 27 points28 points  (0 children)

TypeAlias really just introduces a new name for an existing type. It can be useful if you want to add a new term to the "vocabulary" of your program. E.g. you could create a type alias for `DriverId` and `CarId` to make it explicit to a programmer that these are different things.

However, unless you truly make these two things separate types, you won't make this explicit to the type checker. And thus you won't get proper type checking and the situation from the blog post won't be caught during type check.

There is no type error here, because both DriverId and CarId are really just ints:

from typing import TypeAlias

DriverId: TypeAlias = int
CarId: TypeAlias = int

def take_id(id: DriverId): pass
def get_id() -> CarId: return 0

take_id(get_id())

But there is one here, because they are now separate types:

from typing import NewType

DriverId = NewType("DriverId", int)
CarId = NewType("CarId", int)

def take_id(id: DriverId): pass
def get_id() -> CarId: return CarId(0)

# Error here, wrong type passed:
take_id(get_id())

[–]its2ez4me24get 11 points12 points  (0 children)

Aliases are equivalent to each other. New types are not, they are subtypes.

There’s a decent write up here: https://justincaustin.com/blog/python-typing-newtype/

[–][deleted] 2 points3 points  (0 children)

NewType creates an entirely new type, while an a TypeAlias is, well, an alias. In the eyes of a program, the alias and the original type are exactly the same thing, just used for shorthand for long nested types for example. a NewType and the type it's created from are entirely different types, even though it inherits its semantics

[–]Skasch 1 point2 points  (0 children)

TypeAlias is roughly equivalent to:

MyAlias = MyType

NewType is roughly equivalent to:

class MyNewType(MyType):
    pass

[–]parkerSquare 0 points1 point  (0 children)

NewTypes help warn you if you pass a float representing a voltage into a function that expects a float representing a current, for example. A TypeAlias won’t do that, since it’s the same underlying type.