Skip to content

typing.Any used

Detects usage of typing.Any in type annotations, which can lead to runtime errors by bypassing type checking.

When you use Any, you're essentially telling the type checker "this could be anything," which defeats the purpose of having types in the first place. This can cause bugs that would otherwise be caught at development time.

What gets flagged

We currently detect Any in these places:

Function parameters

from typing import Any

def process_data(data: Any) -> str:
    return str(data)
Diagnostic Output
error[typing-any-used]: Using `typing.Any` in type annotations can lead to runtime errors.
 --> snippet_01.py:3:24
  |
1 | from typing import Any
2 |
3 | def process_data(data: Any) -> str:
  |                        ^^^
4 |     return str(data)
  |
info: rule `typing-any-used` is enabled by default

Return types

from typing import Any

def get_user_data() -> Any:
    return {"name": "John", "age": 30}
Diagnostic Output
error[typing-any-used]: Using `typing.Any` in type annotations can lead to runtime errors.
 --> snippet_02.py:3:24
  |
1 | from typing import Any
2 |
3 | def get_user_data() -> Any:
  |                        ^^^
4 |     return {"name": "John", "age": 30}
  |
info: rule `typing-any-used` is enabled by default

Variable assignments

from typing import Any

user_info: Any = get_user()
Diagnostic Output
error[typing-any-used]: Using `typing.Any` in type annotations can lead to runtime errors.
 --> snippet_03.py:3:12
  |
1 | from typing import Any
2 |
3 | user_info: Any = get_user()
  |            ^^^
  |
info: rule `typing-any-used` is enabled by default

Why this matters

Using Any removes type safety. For example:

from typing import Any

def calculate_total(items: Any) -> int:
    return sum(item for item in items)
Diagnostic Output
error[typing-any-used]: Using `typing.Any` in type annotations can lead to runtime errors.
 --> snippet_04.py:3:28
  |
1 | from typing import Any
2 |
3 | def calculate_total(items: Any) -> int:
  |                            ^^^
4 |     return sum(item for item in items)
  |
info: rule `typing-any-used` is enabled by default

Instead, be specific about what you expect:

def calculate_total(items: list[int]) -> int:
    return sum(item for item in items)

No Diagnostic Emitted

Other Examples

The checker also finds Any nested in other types:

from typing import Any

def process(data: dict[str, Any]) -> None: pass
def get_items() -> list[Any]: pass
user_data: dict[str, Any] = {}
Diagnostic Output
error[typing-any-used]: Using `typing.Any` in type annotations can lead to runtime errors.
 --> snippet_06.py:3:29
  |
1 | from typing import Any
2 |
3 | def process(data: dict[str, Any]) -> None: pass
  |                             ^^^
4 | def get_items() -> list[Any]: pass
5 | user_data: dict[str, Any] = {}
  |
info: rule `typing-any-used` is enabled by default

error[typing-any-used]: Using `typing.Any` in type annotations can lead to runtime errors.
 --> snippet_06.py:4:25
  |
3 | def process(data: dict[str, Any]) -> None: pass
4 | def get_items() -> list[Any]: pass
  |                         ^^^
5 | user_data: dict[str, Any] = {}
  |
info: rule `typing-any-used` is enabled by default

error[typing-any-used]: Using `typing.Any` in type annotations can lead to runtime errors.
 --> snippet_06.py:5:22
  |
3 | def process(data: dict[str, Any]) -> None: pass
4 | def get_items() -> list[Any]: pass
5 | user_data: dict[str, Any] = {}
  |                      ^^^
  |
info: rule `typing-any-used` is enabled by default