Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improving effectiveness of static typing #1613

Open
0xalpharush opened this issue Jan 19, 2023 · 0 comments
Open

improving effectiveness of static typing #1613

0xalpharush opened this issue Jan 19, 2023 · 0 comments
Labels

Comments

@0xalpharush
Copy link
Contributor

As we increase type coverage it would be great to add something like the following to functions that should handle all types, effectively using mypy to alert when pattern matching isn't exhaustive. This will help catch issues earlier on when large changes are made e.g. to support a new solidity feature.

from typing import NoReturn

def assert_never(x: NoReturn) -> NoReturn:
    assert False, "Unhandled type: {}".format(type(x).__name__)

IntOrStr = int | str

def handle(x: IntOrStr) -> None:
    if isinstance(x, str):
        print(f"Handled {x}")
    else:
        assert_never(x)  # Error: "int" is incompatible with "NoReturn"

microsoft/pyright#2569

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant