Comments (3)
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.
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.
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sealed-typing-pep.