In contrast, use object for cases where you want to indicate in a typesafe way that a value MUST literally work with any possible object in existence. As a temporary measure, you might want to give your JSON dicts a type of Dict, which is a bit better then nothing. For example, Python's type annotations currently do not support recursive types, which makes typing things like arbitrary JSON dicts difficult. As a way of giving a type to an expression that is difficult to type.(Or to put it another way, Any is a useful tool for helping migrate an untypechecked codebase to a typed codebase in stages). For example, if you have many dynamic and complex functions, and don't have time to fully statically type all of them, you could settle for just giving them a return type of Any to nominally bring them into the typechecked work. As a way of mixing together dynamic and statically typed code.You should generally try and use Any only for cases where. # Since we have no idea what hello() is, `bar` will also have a type of Any # OK, foo could be any type, and that type might have a 'hello' method Any is the least restrictive type - any possible method or operation is permitted on a value of type Any. In contrast, Any is an escape hatch meant to allow you to mix together dynamic and statically typed code. # Error, not all objects have a method 'hello' If you have a value of type object, the only methods you are permitted to call are ones that are a part of every single object. That means that object is in a certain sense the most restrictive type you can give values. Object is the root of Python's metaclass hierarchy. Create T from a TypeVar: from typing import TypeVar, ListĪny and object are superficially similar, but in fact are entirely opposite in meaning. Which in your case would be T since you are retrieving a value from the list. To properly participate in type-checked code, you need to mark your input as List (a genericly typed container) for a typechecker to then be able to care about the return value. 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. You already painted your function into a an un-typed corner by accepting list, which comes down to being the same thing as List. 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). Unlike Any, object is an ordinary static type (it is similar to Object in Java), and only operations valid for all types are accepted for object values. The type object is another type that can have an instance of arbitrary type as a value. 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.Īnd from the mypy documentation section Any vs. However, unlike Any, the reverse is not true: object is not a subtype of every other type. Similar to Any, every type is a subtype of object. Notice that no typechecking is performed when assigning a value of type Any to a more precise type.Ĭontrast the behavior of Any with the behavior of object. 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. As a special case, Any and object are subclasses of each other. Special type indicating an unconstrained type. The Any type docstring states that object is a subclass of Any and vice-versa: > import typing 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.
0 Comments
Leave a Reply. |