GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add NewType() to create simple unique types with zero runtime overhead · Issue #189 · python/typing

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Add NewType() to create simple unique types with zero runtime overhead#189
Copy link
Assignees
gvanrossum
Milestone
3.5.2

Description

@gvanrossum
gvanrossum
opened on Mar 21, 2016
Issue body actions

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.

Activity

  1. gvanrossum
    added this to the 3.5.2 milestone on Mar 21, 2016
  2. gvanrossum
    added
    enhancement
    on Mar 21, 2016
  3. gvanrossum
    modified the milestones: 3.5.2, 3.5.2 stretch on Apr 5, 2016
  4. ilevkivskyi commented on May 27, 2016

    @ilevkivskyi
    ilevkivskyi
    on May 27, 2016
    Member
    More actions

    I think this is a useful feature.

    I have added PR #226 with a simple minded implementation of NewType, some tests, and a short discussion in the PEP.

    Also I added NewType, Type and ContextManager to the list of things provided by typing.py.

    I do not discuss whether NewType('EURtoUSD', Callable[[float], float]) etc. are allowed. If necessary, we could clarify this later.

  5. gvanrossum commented on May 27, 2016

    @gvanrossum
    gvanrossum
    on May 27, 2016
    MemberAuthor
    More actions

    I would like to limit the arg to a class. Also note that that class must have a constructor that is compatible with that assumed by the type checker -- because whenever you write UserId(42) the runtime will actually see int(42) -- which is fine, and ditto if you use e.g. strings, since str('x') is fine, but if you have your own custom class that you want to diversify this way, you have to make sure that your constructor accepts instances of itself -- even though the customary way of constructing instances might be something totally different. This won't work:

    class PacketId:
        def __init__(self, major: int, minor: int) -> None:
            self._major = major
            self._minor = minor
        # Etc.
    
    TcpPacketId = NewType('TcpPacketId', PacketId)
    
    a = TcpPacketId(127, 0)
    x = PacketId(100, 100)
    b = TcpPacketId(x)  # ???
    

    UPDATE: As pointed out below this is backwards. In order not to confuse future readers I'm not changing my words but adding this disclaimer.

  6. ilevkivskyi commented on May 27, 2016

    @ilevkivskyi
    ilevkivskyi
    on May 27, 2016
    Member
    More actions

    I would like to limit the arg to a class.

    OK

    This won't work: ...

    Actually, at runtime, UserId(42) will be just 42 not int(42). So that actually b = TcpPacketId(x)
    will work, but a = TcpPacketId(127, 0) will not work. This is intentional (as discussed in mypy issue),
    TcpPackedId works as a cast function, that should be fed by PacketId. Do I need to clarify this in the PEP?

  7. gvanrossum commented on May 27, 2016

    @gvanrossum
    gvanrossum
    on May 27, 2016
    MemberAuthor
    More actions

    I guess you need to clarify this -- using int or str as examples doesn't make this clear at all, since they have the same signature. (And sorry for having such a frazzled memory that I didn't remember this -- but maybe it's been helpful teasing out the ambiguities in the description. :-)

    FWIW I think we should take this to python-dev for a formal review period.

  8. ilevkivskyi commented on May 27, 2016

    @ilevkivskyi
    ilevkivskyi
    on May 27, 2016
    Member
    More actions

    I guess you need to clarify this -- using int or str as examples doesn't make this clear at all, since they have the same signature.

    OK, I will do this and then will post a link to the PR to python-dev.

  9. gvanrossum commented on May 27, 2016

    @gvanrossum
    gvanrossum
    on May 27, 2016
    MemberAuthor
    More actions

    To summarize, the essence here is that to the type checker, UserId is a subclass of int with a constructor that takes an int, while at runtime it is the identity function that returns its argument (hopefully an int :-) unchanged. (ADDED:) From this it follows that e.g. adding 1 to a UserId instance is legal and produces an int, not a UserId.

  10. ilevkivskyi commented on May 27, 2016

    @ilevkivskyi
    ilevkivskyi
    on May 27, 2016
    Member
    More actions

    From this it follows that e.g. adding 1 to a UserId instance is legal and produces an int, not a UserId.

    I totally agree with this, there is already this example in the text

  11. gvanrossum commented on May 27, 2016

    @gvanrossum
    gvanrossum
    on May 27, 2016
    MemberAuthor
    More actions

    I just wrote that down to summarize the proposal in the fewest words possible (and because I had never quite realized before that this is how the proposal works out -- it's a very sweet proposal that can be summarized so briefly).

  12. gvanrossum
    modified the milestones: 3.5.2, 3.5.2 stretch on Jun 6, 2016
  13. gvanrossum
    self-assigned this
    on Jun 6, 2016
  14. gvanrossum commented on Jun 6, 2016

    @gvanrossum
    gvanrossum
    on Jun 6, 2016
    MemberAuthor
    More actions

    The PEP text landed (PR #226). I'd really like to get the typing.py changes in too!

  15. gvanrossum commented on Jun 6, 2016

    @gvanrossum
    gvanrossum
    on Jun 6, 2016
    MemberAuthor
    More actions

    NM, the typing.py changes landed too: 807d3a9.

  16. gvanrossum
    closed this as completedon Jun 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @gvanrossum
    gvanrossum

Labels

No labels
No labels

Projects

No projects

    Milestone

    • 3.5.2

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.