Sub topic 7
Any, unannotated code, and gradual typing
This chapter assumes you can annotate functions and variables and run mypy or Pyright from the command line. It covers how checkers treat code that has no annotations, and what Any does when it appears.
Gradual typing
Python's type system is gradual. You can annotate some code and leave the rest alone, and the checker still works. Where it has no type information, it uses Any. This is what makes adoption possible in an existing codebase, but it also leaves holes in the checking.
What Any means
Any is compatible with every type in both directions. You can assign an Any value to an int variable. You can pass an int where Any is expected. You can also access any attribute on an Any value, and the checker will not complain.
from typing import Any
def risky(value: Any) -> int:
return value.no_such_attribute # accepted: the checker stops checking here
def safe(value: object) -> int:
return value.no_such_attribute # flagged: "object" has no such attributeThe two functions differ only in the parameter type. object means "anything, but I know nothing about it", so you must narrow it before use. Any means "stop checking this value".
| Annotation | Accepts any argument | Allows any attribute or operation |
|---|---|---|
| `Any` | Yes | Yes |
| `object` | Yes | No, only what `object` itself has |
Unannotated code
A function with no annotations has parameters and a return value of type Any. The two checkers treat the body differently.
- mypy skips the body of a fully unannotated function by default. Errors inside it go unreported. The option
check_untyped_defsturns checking on. - Pyright checks the body anyway. It infers parameter types from default values, and it infers the return type from the body.
def total(prices, tax=0.2):
return sum(prices) * (1 + tax)
def label(price: float) -> str:
return "Cost: " + price # flagged by both: str + floattotal is unannotated, so mypy treats its result as Any. label is annotated, so both tools check its body and report the error.
Where Any comes from
Any often enters a program without you writing it:
- Unannotated parameters and return values.
- Imports from libraries that have no type information.
json.loads, which returnsAny.- The
Anytype argument in a barelistordict, which meanslist[Any].
Because Any is contagious, one Any can disable checking for everything computed from it. A value assigned from an Any expression to an annotated variable regains its declared type. For example, count: int = json.loads(text) makes count an int from then on, but nothing verifies it at runtime.
Making Any visible
Use reveal_type to see where Any appears. The strict modes in both tools report unannotated definitions and implicit Any values. The strict mode chapter covers them.
Exercise
A function def double(x): return x * 2 is called as double("a").nothing_here. Why might mypy accept this with default settings, and what does object change if you annotate x: object?
Show answer
The function is unannotated, so mypy treats its return value as Any and allows any attribute access on it. If x is annotated as object, the function now counts as annotated and mypy checks its body. It reports that * is not supported for object, so you would need to narrow x first, for example with isinstance.