Code Monkey home page Code Monkey logo

Comments (3)

johnthagen avatar johnthagen commented on June 15, 2024

I ran into an issue testing mypy. assert_never() is quite young (only a month or so old) so there could be some bugs:

from sealed-typing-pep.

johnthagen avatar johnthagen commented on June 15, 2024

Here is an example that works in the Python 3.10 and current mypy that can be used to play around with exhaustiveness checking.

from enum import Enum, auto

from typing_extensions import NoReturn


# TODO: Remove the need for this when mypy > 0.942 is released.
#     At that point, simply import this from `typing_extensions`
def assert_never(__arg: NoReturn) -> NoReturn:
    raise AssertionError("Expected code to be unreachable")


class Color(Enum):
    Red = auto()
    Green = auto()
    Blue = auto()


def c(color: Color) -> int:
    match color:
        case Color.Red:
            pass
        case Color.Blue:
            pass
        case _ as u:
            assert_never(u)


def int_or_str(arg: int | str) -> None:
    match arg:
        case int():
            print("It's an int")
        case str():
            print("It's a str")
        case _ as unreachable:
            assert_never(unreachable)

from sealed-typing-pep.

johnthagen avatar johnthagen commented on June 15, 2024

While exhaustiveness checking does not work currently on the proposed base-class method without @sealed, it does work with the existing Union method:

from dataclasses import dataclass
from enum import Enum, auto

# TODO: Remove the need for this when mypy > 0.942 is released.
#     At that point, simply import this from `typing_extensions`
def assert_never(__arg: NoReturn) -> NoReturn:
    raise AssertionError("Expected code to be unreachable")


class UsState(Enum):
    Alabama = auto()
    Alaska = auto()
    # --snip--


@dataclass
class Nickle:
    ...


@dataclass
class Dime:
    ...


@dataclass
class Quarter:
    us_state: UsState


Coin = Nickle | Dime | Quarter


def value_in_cents(coin: Coin) -> int:
    match coin:
        case Nickle():
            return 5
        # try removing one of the `case`s
        case Dime():
            return 10
        case Quarter(state):
            print(f"State quarter from {state}")
            return 25
        case _ as u:
            assert_never(u)

from sealed-typing-pep.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.