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 — typing.Never and typing.assert_never · Issue #90633 · python/cpython

typing.Never and typing.assert_never #90633

New issue
Copy link
New issue
Copy link
Closed
Closed
typing.Never and typing.assert_never#90633
Copy link
Assignees
JelleZijlstra
Labels
3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@JelleZijlstra
JelleZijlstra
opened on Jan 22, 2022
Issue body actions
BPO 46475
Nosy @gvanrossum, @davidfstr, @JelleZijlstra, @sobolevn, @Fidget-Spinner, @AlexWaygood
PRs
  • bpo-46475: Add typing.Never and typing.assert_never #30842
  • 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 = 'https://github.com/JelleZijlstra'
    closed_at = <Date 2022-02-08.19:07:21.141>
    created_at = <Date 2022-01-22.22:48:21.675>
    labels = ['type-feature', 'library', '3.11']
    title = 'typing.Never and typing.assert_never'
    updated_at = <Date 2022-02-08.19:07:21.140>
    user = 'https://github.com/JelleZijlstra'

    bugs.python.org fields:

    activity = <Date 2022-02-08.19:07:21.140>
    actor = 'JelleZijlstra'
    assignee = 'JelleZijlstra'
    closed = True
    closed_date = <Date 2022-02-08.19:07:21.141>
    closer = 'JelleZijlstra'
    components = ['Library (Lib)']
    creation = <Date 2022-01-22.22:48:21.675>
    creator = 'JelleZijlstra'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46475
    keywords = ['patch']
    message_count = 5.0
    messages = ['411297', '411301', '411309', '411347', '412860']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'David Foster', 'JelleZijlstra', 'sobolevn', 'kj', 'AlexWaygood']
    pr_nums = ['30842']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46475'
    versions = ['Python 3.11']

    Activity

    Next
    1. JelleZijlstra commented on Jan 22, 2022

      @JelleZijlstra
      JelleZijlstra
      on Jan 22, 2022
      MemberAuthor
      More actions

      At the moment, https://docs.python.org/3.10/library/typing.html#typing.NoReturn simply says:

      Special type indicating that a function never returns.
      

      In practice, type checkers accept NoReturn as a bottom type in other positions too. We should document this behavior.

    2. JelleZijlstra
      assigned
      JelleZijlstra
      on Jan 22, 2022
    3. JelleZijlstra
      added
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      3.9 (EOL)end of lifeend of life
      3.10 (EOL)end of lifeend of life
      3.11only security fixesonly security fixes
      on Jan 22, 2022
    4. sobolevn commented on Jan 22, 2022

      @sobolevn
      sobolevn
      on Jan 22, 2022
      Member
      More actions

      I agree.

      Plus, I cannot refrain from saying that it is generally quite hard to teach what "NoReturn" is.

      I start by explaining what bottom type and void is and then just say that in Python it is called NoReturn. I remember that there was a discussion about Never alias for it.

      Jelle, will you please push it? :)

    5. AlexWaygood commented on Jan 22, 2022

      @AlexWaygood
      AlexWaygood
      on Jan 22, 2022
      Member
      More actions

      I also agree that documenting this is a great idea. It's already been adopted by large swathes of the typing community for use in this context. The value of having NoReturn act as a "bottom type" is evident. Time to make it official, in my opinion.

    6. JelleZijlstra commented on Jan 23, 2022

      @JelleZijlstra
      JelleZijlstra
      on Jan 23, 2022
      MemberAuthor
      More actions

      Based on feedback here and on typing-sig I'm now leaning towards adding typing.Never along with typing.assert_never. I'll submit a single patch for these as the documentation will be closely linked.

    7. JelleZijlstra
      removed
      3.9 (EOL)end of lifeend of life
      on Jan 23, 2022
    8. 7 remaining items

    9. gvanrossum commented on Feb 8, 2022

      @gvanrossum
      gvanrossum
      on Feb 8, 2022
      Member
      More actions

      New changeset 243436f by Jelle Zijlstra in branch 'main':
      bpo-46475: Add typing.Never and typing.assert_never (GH-30842)
      243436f

    10. JelleZijlstra
      closed this as completedon Feb 8, 2022
    11. JelleZijlstra
      closed this as completedon Feb 8, 2022
    12. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    13. ptmcg commented on Apr 19, 2022

      @ptmcg
      ptmcg
      on Apr 19, 2022
      More actions

      What is the purpose of the required argument to assert_never()? Regardless of what I pass to this function, the raised exception is "AssertionError: Expected code to be unreachable"

    14. JelleZijlstra commented on Apr 19, 2022

      @JelleZijlstra
      JelleZijlstra
      on Apr 19, 2022
      MemberAuthor
      More actions

      @ptmcg it always throws at runtime; the point is that static typecheckers will verify that the code really is unreachable. See https://docs.python.org/3.11/library/typing.html#typing.assert_never and https://typing.readthedocs.io/en/latest/source/unreachable.html

    15. gvanrossum commented on Apr 19, 2022

      @gvanrossum
      gvanrossum
      on Apr 19, 2022
      Member
      More actions

      IIUC the question is why does it have an argument and what effect does it have? (It seems to have none, then why is it required?)

    16. ptmcg commented on Apr 19, 2022

      @ptmcg
      ptmcg
      on Apr 19, 2022
      More actions

      Thank you @gvanrossum , that is what I was asking.

    17. JelleZijlstra commented on Apr 19, 2022

      @JelleZijlstra
      JelleZijlstra
      on Apr 19, 2022
      MemberAuthor
      More actions

      I see. Usually you'd use this function in a place where some variable gets narrowed down to Never. Making it an explicit argument makes it possible to implement the function without any special casing in type checkers, because they already know how to treat Never in an argument position.

      We could add a similar function with no arguments that asserts a block is unreachable, but unlike assert_never() it would need to be special cased in type checkers.

      We might want to revisit the fact that the error message is always the same. We did that on the basis of security concerns about str() on untrusted input, but I don't find those concerns that convincing, and it's useful to have some representation of the object in the error message.

    18. gvanrossum commented on Apr 19, 2022

      @gvanrossum
      gvanrossum
      on Apr 19, 2022
      Member
      More actions

      Security concerns? That seems excessive. Millions of things in the stdlib print the repr() of the thing that unexpectedly failed some check.

      Reacted by Alex Waygood
    19. JelleZijlstra commented on Apr 20, 2022

      @JelleZijlstra
      JelleZijlstra
      on Apr 20, 2022
      MemberAuthor
      More actions

      Sending a PR shortly to add the asserted value to assert_never()'s error message, and also to link to https://typing.readthedocs.io/en/latest/source/unreachable.html while I'm at it.

    20. JelleZijlstra
      reopened this on Apr 20, 2022
    21. JelleZijlstra
      added a commit that references this issue on Apr 20, 2022

      pythongh-90633: Improve error and docs for typing.assert_never

      ce3da53
    22. JelleZijlstra
      closed this as completedin 93d2801on Apr 25, 2022
    23. serhiy-storchaka
      added a commit that references this issue on Aug 9, 2026

      pythongh-90633: Improve error and docs for typing.assert_never (pytho…

      93d2801
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    • @JelleZijlstra
      JelleZijlstra

    Labels

    3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      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.