30

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

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? Commented Feb 17, 2021 at 10:04
  • 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). Commented Dec 12, 2022 at 21:26

1 Answer 1

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():
    ...
3
  • 6
    This doesn't just turn off type checking, but additionally removes any annotations from the function's definition. Commented Feb 22, 2021 at 16:08
  • 1
    @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.
    – Ben Kovitz
    Commented Jul 27, 2022 at 0:37
  • 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) Commented Jul 28, 2023 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.