Skip to content

assert_type with callable seems to require a param name #9906

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

You must be logged in to vote

assert_type tests for type equivalency, not type assignability. The type of tuple_of_nums is (n: int) -> tuple[int, ...]. The type described by the annotation Callable[[int], tuple[int, ...]] is a subtype of this type, but it's not equivalent because it does not accept a keyword argument n.

If you want to test for type assignability, then assert_type isn't the right tool to use. You can instead simply include an assignment, and a type checker will verify assignability.

x: Callable[[int], tuple[int,...]] = tuple_of_nums

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Vulwsztyn
Comment options

You must be logged in to vote
0 replies
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