Python Type Hint Generator – Add Hints from Defaults
NOUVEAUPython type hint generator that adds annotations inferred from default values and simple return statements, with typing imports and a before/after view. Free, in-browser.
Fonctionne à 100 % dans votre navigateur — vos données ne sont jamais envoyées ni stockées sur nos serveurs.
Cet outil n'est pas encore entièrement traduit : une partie du texte est encore en anglais. L'outil lui-même fonctionne de la même façon dans toutes les langues.
from typing import Any
def paginate(items, page: int = 1, per_page: float = 20.0, label: str = "Page", is_last: bool | None = None) -> list[Any] | list[str | int]:
if not items:
return []
return [label, page]
def is_valid(code: str = "") -> bool:
return len(code) == 6
class Cart:
def __init__(self, items: list[Any] = [], discount: float = 0.0) -> None:
self.items = items
| Function | Target | Type | Because |
|---|---|---|---|
| paginate | page | int | default 1 |
| paginate | per_page | float | default 20.0 |
| paginate | label | str | default "Page" |
| paginate | is_last | bool | None | default None + name prefix is_ |
| paginate | return | list[Any] | list[str | int] | return [] / [label, page] |
| is_valid | code | str | default "" |
| is_valid | return | bool | return len(code) == 6 |
| Cart.__init__ | items | list[Any] | default [] |
| Cart.__init__ | discount | float | default 0.0 |
| Cart.__init__ | return | None | __init__ always returns None |
Not inferred (1) — annotate these yourself:
- paginate · items: no default to infer from
- Cart.__init__(items=[]): mutable default argument — the same object is shared between calls; consider None and create it inside the function.
Inference is heuristic: it only reads literal defaults, a few name prefixes and simple return expressions. Run mypy or pyright on the result.
Exemples rapides
Des entrées typiques et les résultats produits par cet outil.
Présentation
This Python type hint generator adds annotations to functions that have none. It looks at evidence that is actually in the code: literal default values (numbers, strings, bytes, booleans, lists, dicts, sets, tuples, lambdas), a short list of boolean name prefixes such as is_ and has_, and the expressions in simple return statements. Existing annotations are never changed.
You choose modern syntax (list[int], X | None, Python 3.10+) or the typing module (List[int], Optional[X]) for older code, and the tool adds the matching from typing import line and, optionally, from __future__ import annotations. A before/after view and a table explain where each hint came from.
Inference is a heuristic, not a type checker. Anything it cannot prove is listed under "Not inferred" instead of guessed, and you should still run mypy or pyright. Everything runs in your browser.
Pourquoi utiliser cet outil ?
- Annotate legacy functions in seconds
- Every hint comes with the reason it was added
- Modern or typing-module syntax with imports
- Never guesses silently — unknowns are listed
- Warns about mutable default arguments
Fonctionnalités
Hints from defaults
int, float, complex, str, bytes, bool and nested list/dict/set/tuple literals become precise types.
Return types
Simple returns (literals, comparisons, len(), str(), parameters) are combined into one return type, None included.
Two syntaxes
list[int] and X | None for 3.10+, or List[int] and Optional[X] with the typing module.
Import lines
Adds from typing / collections.abc imports after the module docstring and __future__ imports.
Before / after
Switch views and read a table of every added hint and its reason.
Honest gaps
Parameters and returns with no usable evidence are listed, or annotated as Any if you opt in.
Mode d'emploi
Paste functions
Put one or more Python functions (with their bodies) into the Python functions box.
Pick the syntax
Choose Modern or typing module, and the import and name-prefix options.
Review the table
Check the Added hints table and the Not inferred list.
Copy the code
Press Copy annotated code and run your type checker on it.