Skip to content

Add NewType() to create simple unique types with zero runtime overhead #189

@gvanrossum

Description

@gvanrossum

The description is best gleaned from the following mypy issue: python/mypy#1284 (comment) and following. We're going with option (A).

Really brief example:

from typing import NewType
UserId = NewType('UserId', int)

Now to the type checker UserId is a new type that's compatible with int, but converting an int to a UserId requires a special cast form, UserId(x). At runtime UserId instances are just ints (not a subclass!) and UserId() is a dummy function that just returns its argument.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy