Skip to content

Ignore typing errors in individual blocks #6948

Open
@exhuma

Description

@exhuma

It is already possible to silence errors using # type: ignore on individual lines, and - since #626) also for a whole file.

Additionally it would be nice to ignore errors on individual blocks. Especially on a class/function level when abstracting away an untyped library. For example:

class MyAbstraction:

    def __init__(self, url: str) -> None:
        self.hostname = url.host  # <-- I want this to be caught by the type-checker

    def some_call(self) -> int:
        # type: ignore
        result = api.untyped_call()
        return result

    def another_large_call(self) -> int:
        # type: ignore
        result_1 = api.untyped_call_1()
        result_2 = api.untyped_call_2()
        result = result_1 + result_2
        return result

When running mypy in "strict" mode the code will be littered with messages like Call to an untyped function ... in a typed context. Currently the only options are:

  • Add # type: ignore on each line causing errors by the external library
    • Disadvantage: Cumbersome (many type-comments needed)
    • Advantage: Only the problematic calls are silenced
  • Add # type: ignore to the file
    • Disadvantage: Typing errors unrelated to the external library become invisible
    • Advantage: Only one line to add

The second option is best used if a module contains only calls to the external library. For new projects this is doable, but older/larger code-bases often need some refactoring for this. And this can be error-prone and seems risky to do only for type-hinting.

Having the option to disable errors on either a class-level or block-level would add a lot of flexibility.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions