Is there a way to ignore mypy checks on a single function?
Asked Answered
S

1

30

You can ignore mypy checks on a individual lines as answered here. Is there a way to ignore mypy for a full function?

Singe answered 16/2, 2021 at 10:16 Comment(2)
What exactly do you want mypy to ignore? Is the function annotated, but mypy should not use these annotations? Should the usage or the implementation be ignored, or both? Should the function be checked, but the result ignored?Ghent
Good clarifying question. I found this answer while looking for a solution to the former (I want mypy to be quiet about usage of a particular imported function).Saboteur
S
47

mypy checks can be ignored for a full function by adding @typing.no_type_check decorator on top of the function.

import typing
@typing.no_type_check
def some_function():
    ...
Singe answered 16/2, 2021 at 10:16 Comment(3)
This doesn't just turn off type checking, but additionally removes any annotations from the function's definition.Sarina
@typing.no_type_check might be a very big hammer, but it served wonderfully for sidestepping a bug in mypy that caused it to crash on a certain function.Lieberman
Strangely this doesn't work in some cases, adding it did silence some errors but not others (eg: class Foo(bar_module_PrivateClass) defined inside a function)Frontlet

© 2022 - 2024 — McMap. All rights reserved.