Skip to content

String representation for types created with typing.NewType(…) are opaque and unappealing #79144

Closed

Description

@fish2000
mannequin
BPO 34963
Nosy @serhiy-storchaka, @ilevkivskyi, @csabella, @fish2000, @uriyyo
PRs
  • bpo-34963: Create callable types in typing.NewType #9808
  • bpo-34963: Make the repr of the typing.NewType() result more meaningful. #9951
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-12-27.11:29:52.903>
    created_at = <Date 2018-10-12.03:03:15.563>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'String representation for types created with typing.NewType(\xe2\x80\xa6) are opaque and unappealing'
    updated_at = <Date 2021-12-27.11:29:52.903>
    user = 'https://github.com/fish2000'

    bugs.python.org fields:

    activity = <Date 2021-12-27.11:29:52.903>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-12-27.11:29:52.903>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2018-10-12.03:03:15.563>
    creator = 'fish2000'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34963
    keywords = ['patch']
    message_count = 11.0
    messages = ['327558', '327595', '327596', '327610', '327611', '327968', '328224', '328699', '328707', '343333', '398065']
    nosy_count = 5.0
    nosy_names = ['serhiy.storchaka', 'levkivskyi', 'cheryl.sabella', 'fish2000', 'uriyyo']
    pr_nums = ['9808', '9951']
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34963'
    versions = ['Python 3.7', 'Python 3.8']

    Activity

    1. fish2000 commented on Oct 12, 2018

      fish2000mannequin
      MannequinAuthor

      When creating a new type with typing.NewType(…) the result is a function defined on-the-fly with no reasonable __repr__ definition – the string representation of the type looks something like e.g. “<function NewType..new_type at 0x108ae6950>” which isn’t very useful or aesthetically appealing.

    2. added
      extension-modules
      3.7 (EOL)
      type-bug
      stdlib
      3.8 (EOL)
      and removed
      extension-modules
      on Oct 12, 2018
    3. serhiy-storchaka commented on Oct 12, 2018

      @serhiy-storchaka
      Member

      For repr you need to set __qualname__, not just __name__. It may be worth to set also __module__ for pickleability and better help.

    4. serhiy-storchaka commented on Oct 12, 2018

      @serhiy-storchaka
      Member

      You can determine the module of the caller by using sys._getframe().

    5. fish2000 commented on Oct 12, 2018

      fish2000mannequin
      MannequinAuthor

      The proposed solution in the PR replaces the identity-function return value of NewType(…) with a callable class instance that adds an explicit __repr__ function – which that function cobbles together the string representations of the supplied type and the new types’ name into a sensible and stylistically consistent “repr” output.

      In this capacity the __name__ vs. __qualname__ consideration would appear to be an implementation detail – in this case, calculating a __qualname__ value, á la PEP-3155, doesn’t seem to be possible nor advantageous, in this specific case.

      On the other hand, adding __module__ seems like it’d be a win – although I am wary about introducing a dependency on a function that starts with an underscore. How reliable and/or portable is sys._getframe() ?

    6. serhiy-storchaka commented on Oct 12, 2018

      @serhiy-storchaka
      Member

      There are disadvantages of using a callable class. First, creating a class is 10-100 times slower than creating a function. Second, calling it is slower than calling a function.

      sys._getframe() is already used in the typing module (and also in collections and enum).

    7. serhiy-storchaka commented on Oct 18, 2018

      @serhiy-storchaka
      Member

      PR 9951 is a simpler way. It just sets __qualname__ (and __module__ for completeness).

      >>> import typing
      >>> UserId = typing.NewType('UserId', int)
      >>> UserId
      <function UserId at 0x7f1386ac77e0>
    8. ilevkivskyi commented on Oct 21, 2018

      @ilevkivskyi
      Member

      OK, so now we have two "competing" PRs. I think I like Serhiy's PR a bit more, since it is simpler. I would propose to look at benchmarks and then decide. Are there any other factors I am missing for choosing one or other approach?

    9. serhiy-storchaka commented on Oct 28, 2018

      @serhiy-storchaka
      Member

      PR 9951 is simpler, but it makes invoking NewType() slower, because retrieving the module from the caller frame is slow in comparison with other operations.

      $ ./python -m timeit -s "import typing" -- "UserId = typing.NewType('UserId', int)"

      PR 9808:
      1000000 loops, best of 5: 316 nsec per loop

      PR 9951:
      500000 loops, best of 5: 634 nsec per loop

      PR 9951 without setting __module__ and stripping components before dot in __name__:
      1000000 loops, best of 5: 316 nsec per loop

      In contrary, calling the resulting type is slower with PR 9808:

      $ ./python -m timeit -s "import typing" -s "UserId = typing.NewType('UserId', int)" -- "UserId(5)"

      PR 9808:
      2000000 loops, best of 5: 105 nsec per loop

      PR 9951:
      5000000 loops, best of 5: 74.1 nsec per loop

      What other operations should be supported? Should the NewType() result be pickleable? Currently it is not, in contrary to other typing types. If it should be pickleable, should it be pickled by name (resulting in restoring the same instance on unpickling, but failing if it is not accessible by name) or by its arguments (resulting in creating a new instance when unpickled)? Should it support comparison and what values should be treated as equal?

      If make NewType a class, I would make the repr looking like a call:

      >>> import typing
      >>> UserId = typing.NewType('UserId', int)
      >>> UserId
      NewType('UserId', <class 'int'>)
      >>> UserNames = typing.NewType('UserNames', typing.List[int])
      >>> UserNames
      NewType('UserNames', typing.List[int])
    10. ilevkivskyi commented on Oct 28, 2018

      @ilevkivskyi
      Member

      Serhiy, thanks for benchmarks and good suggestions!

      If make NewType a class, I would make the repr looking like a call

      This is a nice idea, it indeed looks better. We can probably also use _type_repr() helper for the argument (for consistency).

      Should the NewType() result be pickleable?

      This is not required by PEP-484, but I could imagine this may be useful.

      If it should be pickleable, should it be pickled by name (resulting in restoring the same instance on unpickling, but failing if it is not accessible by name) or by its arguments (resulting in creating a new instance when unpickled)?

      Logically, they should behave like class objects, so if we are going to make them pickleable, they should be pickled by full name.

      Should it support comparison and what values should be treated as equal?

      Again, since they should behave like class objects, I think we don't need any dedicated __eq__, they should be just compared by identity.

      From performance point of view, both PRs look good. Maybe we can even combine both, so that we have both fine-tuned repr and pickleability. I understand this means we will get slower benchmarks for both creation and instantiation, but I think it is still OK, since it is still much faster than creating a new class.

    11. csabella commented on May 23, 2019

      @csabella
      Contributor

      Serhiy,

      It looks like Ivan approved your PR and was ready to merge it, but left it up to you for the final decision about incorporating changes from the competing PR. This is just a friendly ping in case you want to get it in for 3.8. Thanks!

    12. uriyyo commented on Jul 23, 2021

      @uriyyo
      Member

      NewType was converted into callable class at scope of https://bugs.python.org/issue44353

      Currently repr of NewType is:

      >>> UserId = NewType("UserId", int)
      __main__.UserId
      
    13. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)3.8 (EOL)stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions