Is there any difference between using typing.Any
as opposed to object
in typing? For example:
def get_item(L: list, i: int) -> typing.Any:
return L[i]
Compared to:
def get_item(L: list, i: int) -> object:
return L[i]
Yes, there is a difference. Although in Python 3, all objects are instances of object
, including object
itself, only Any
documents that the return value should be disregarded by the typechecker.
The Any
type docstring states that object is a subclass of Any
and vice-versa:
>>> import typing
>>> print(typing.Any.__doc__)
Special type indicating an unconstrained type.
- Any object is an instance of Any.
- Any class is a subclass of Any.
- As a special case, Any and object are subclasses of each other.
However, a proper typechecker (one that goes beyond isinstance()
checks, and which inspects how the object is actually used in the function) can readily object to object
where Any
is always accepted.
From the Any
type documentation:
Notice that no typechecking is performed when assigning a value of type
Any
to a more precise type.
and
Contrast the behavior of
Any
with the behavior ofobject
. Similar toAny
, every type is a subtype ofobject
. However, unlikeAny
, the reverse is not true: object is not a subtype of every other type.That means when the type of a value is
object
, a type checker will reject almost all operations on it, and assigning it to a variable (or using it as a return value) of a more specialized type is a type error.
and from the mypy documentation section Any vs. object:
The type
object
is another type that can have an instance of arbitrary type as a value. UnlikeAny
,object
is an ordinary static type (it is similar toObject
in Java), and only operations valid for all types are accepted for object values.
object
can be cast to a more specific type, while Any
really means anything goes and a type checker disengages from any use of the object (even if you later assign such an object to a name that is typechecked).
You already painted your function into a an un-typed corner by accepting list
, which comes down to being the same thing as List[Any]
. The typechecker disengaged there and the return value no longer matters, but since your function accepts a list containing Any
objects, the proper return value would be Any
here.
To properly participate in type-checked code, you need to mark your input as List[T]
(a genericly typed container) for a typechecker to then be able to care about the return value. Which in your case would be T
since you are retrieving a value from the list. Create T
from a TypeVar
:
from typing import TypeVar, List
T = TypeVar('T')
def get_item(L: List[T], i: int) -> T:
return L[i]