I'm starting to get into type hints (aka annotations) in python 3.6, and I can't figure some of the dynamic aspects of this feature.
I wrote the following piece of code, and I want to add annotation and not sure how, even after looking through the docs on type hinting.
This is the function:
def validate_expression(expression: ?):
try:
assert expression
except AssertionError as e:
...
expression
needs to be anything that an assert works on (assuming any expression for which bool(expression)
is valid).
What should I write instead of the question mark?
UPDATE:
I know that most python expressions can be cast as a Boolean, but the context in which I write this code is one where it is reasonable to expect an expression to not be a assertable.
The relevant example in my case is pandas.DataFrame
. Running
bool(pandas.DataFrame())
raises an error, and I have good reason to expect that someone might try to pass a dataframe to the validation function.
UPDATE 2: Following Chepner's comments and answer, I understand now that:
typing.Any
or by not adding annotation at all.bool(pandas.DataFrame()) # --> ValueError
, annotations won't help since this is a runtime error.typing.Iterable
), and as far as I'm concerned it is not worth bending over backwards to address such an edge case (although it would be interesting to hear of relevant example and a bend-y solution!)Any value whatsoever can be used in a boolean context. An instance of object
is considered to be a truthy value unless a descendent class provides an alternate definition; anything that is considered false (like an empty list, an empty str, an empty dict, False
itself, etc) does so because it has been specially defined to be so.
As such, the only type hint you could use is typing.Any
:
from typing import Any
def validate_expression(expression: Any):
try:
assert expression
except AssertionError as e:
...
which, really, is barely worth stating explicitly.