Skip to content

How to write negative typing tests? #8803

Answered by erictraut
dimaqq asked this question in Q&A
Discussion options

You must be logged in to vote

The common way to write negative typing tests is to use a combination of a # type: ignore or # pyright: ignore along with the reportUnnecessaryTypeIgnoreComment check in pyright. The former will suppress the typing error, and the latter will trigger a diagnostic if the # pyright: ignore is no longer serving a purpose.

The assert_type function is also useful for typing tests.

Code sample in pyright playground

# pyright: reportUnnecessaryTypeIgnoreComment=true

from typing import assert_type


def func(x: int) -> int:
    return x


v1 = func(3)
assert_type(v1, int)

func("bad param") # pyright: ignore[reportArgumentType]

func(1) # pyright: ignore[reportArgumentType]

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dimaqq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants