# Non-typed signature [[Type Annotation|Type annotations]] are a good way to document the [[Type|types]] of [[function]] [[Parameter|parameters]]. They also help catch bugs, when used alongside a [[type]] checker, by ensuring that the [[Type|types]] of any provided [[Argument|arguments]] match [[Parameter|parameters]] expectations. ```python # Don't def foo(x, *args, **kwargs): ... # Do def foo(x: int, *args: str | float, **kwargs: str) -> list[float]: ... ``` ```python # Don't class Foo: def __init__(self, x): self.x = x # Do class Foo: x: int def __init__(self, x: int) -> None: self.x = x ``` ## Automation These [[Anti-Patterns of Python|anti-patterns]] can be detected by [[Ruff]] (recommended) or the [[ANN - flake8-annotations]] extension: List of rules that can be automated by [[Ruff]]:[^1] | Code | Detected pattern | Name | | ------ | ------------------------------------------------------------------------------------ | ---------------------------------------------------------- | | ANN001 | `def foo(x): …` | `missing-type-function-argument`[^2] | | ANN002 | `def foo(*args): …` | `missing-type-args`[^3] | | ANN003 | `def foo(**kwargs): …` | `missing-type-kwargs`[^4] | | ANN201 | `def add(a, b): return a + b` | `missing-return-type-undocumented-public-function`[^5]<br> | | ANN202 | `def _protected_add(a, b): return a + b` and `def __private_add(a, b): return a + b` | `missing-return-type-private-function`[^6]<br> | | ANN204 | `def __init__(self, x):` | `missing-return-type-special-method`[^7] | | ANN205 | `@staticmethod`<br>`def bar():` | `missing-return-type-static-method`[^8] | | ANN206 | `@classmethod`<br>`def bar(cls):` | `missing-return-type-class-method`[^9] | List of rules that can be automated by [[ANN - flake8-annotations|flake8-annotations]]: | Code | Detected pattern | Description | | ------ | ---------------------------------------- | ----------------------------------------------------- | | ANN001 | `def foo(x): …` | Missing type annotation for function argument | | ANN002 | `def foo(*args): …` | Missing type annotation for `*args` | | ANN003 | `def foo(**kwargs): …` | Missing type annotation for `**kwargs` | | ANN101 | `def foo(self): …` | Missing type annotation for `self` in method | | ANN102 | `def foo(cls): …` | Missing type annotation for `cls` in classmethod | | ANN201 | `def add(a, b): return a + b` | Missing return type annotation for public function | | ANN202 | `def _protected_add(a, b): return a + b` | Missing return type annotation for protected function | | ANN203 | `def __private_add(a, b): return a + b` | Missing return type annotation for private function | | ANN204 | `def __init__(self, x):` | Missing return type annotation for special method | | ANN205 | `@staticmethod`<br>`def bar():` | Missing return type annotation for staticmethod | | ANN206 | `@classmethod`<br>`def bar(cls):` | Missing return type annotation for classmethod | --- [^1]: [flake8-annotations (ANN) - docs.astral.sh](https://docs.astral.sh/ruff/rules/#flake8-annotations-ann) [^2]: [missing-type-function-argument (ANN001) - docs.astral.sh](https://docs.astral.sh/ruff/rules/missing-type-function-argument/) [^3]: [missing-type-args (ANN002) - docs.astral.sh](https://docs.astral.sh/ruff/rules/missing-type-args/) [^4]: [missing-type-kwargs (ANN003) - docs.astral.sh](https://docs.astral.sh/ruff/rules/missing-type-kwargs/) [^5]: [missing-return-type-undocumented-public-function (ANN201) - docs.astral.sh](https://docs.astral.sh/ruff/rules/missing-return-type-undocumented-public-function/) [^6]: [missing-return-type-private-function (ANN202) - docs.astral.sh](https://docs.astral.sh/ruff/rules/missing-return-type-private-function/) [^7]: [missing-return-type-special-method (ANN204) - docs.astral.sh](https://docs.astral.sh/ruff/rules/missing-return-type-special-method/) [^8]: [missin-return-type-static-method (ANN205) - docs.astral.sh](https://docs.astral.sh/ruff/rules/missing-return-type-static-method/) [^9]: [missing-return-type-class-method (ANN206) - docs.astral.sh](https://docs.astral.sh/ruff/rules/missing-return-type-class-method/)