← All posts

Release: Static type checker in Python

The Python analyzer now supports type checking with mypy.

Header image
By Srijan on 
Share on Twitter Share on LinkedIn Share on Facebook

Static type checking has taken the Python world by storm lately, with several engineering teams implementing it at scale and driving massive value. But if you're writing type-hint annotated code in Python today, you'd have to run a static type checker manually, as part of your CI or your local machine.

Well, no more! We’re excited to announce that static type checking for Python is now available on DeepSource, powered by mypy. And how do you use it? By just adding a single line in your .deepsource.toml.

version = 1

[[ analyzers ]]
name = 'python'
enabled = true
  
  [analyzers.meta]
  type_checker = "mypy"

If you happen to have a mypy.ini in your repository for your typing configuration, DeepSource will honor that. Read all about it in the docs.

If you’ve enabled type checking, DeepSource will raise two types of issues: issues with type-annotated code, and any unnecessary suppressions of type checks.

Issues with type-hint annotated code

def add_middleware(
    self,
    middleware_class: type,
    **kwargs: typing.Any
) -> str:
    self.error_middleware.app = middleware_class(
        self.error_middleware.app, **kwargs
    )

This code snippet will be flagged like this:

Type checker type annotation issue (TYP-001)

Unnecessary suppression of type checks

def __init__(
    self,
    *args: typing.Union[
        "FormData",
        typing.Mapping[str, typing.Union[str, UploadFile]],
        typing.List[typing.Tuple[str, typing.Union[str, UploadFile]]],
    ],
    **kwargs: typing.Union[str, UploadFile],
) -> None:
    super().__init__(*args, **kwargs)  # type: ignore

This code snippet will be flagged like this:

Type checker unnecessary suppression (TYP-002)

Silencing type check issues

To silence issues raised by the type checker on a particular line, add # type: ignore or # skipcq: TYP-001 at the end of the line. To read more on silencing issues, follow this article.

Keep reading...

Ready for static type checking in Python?

DeepSource is the easiest way to run mypy continuously.