Skip to content Skip to sidebar Skip to footer

The Type Of A Variable That Can Be Implicitly Checked For "zeroness" Or "emptiness"

I have a function that receives a variable x and checks for it's 'zeroness' or 'emptiness'. Using PEP-484 type hinting syntax, what type hint would I use? And how would I check if

Solution 1:

So first, since the comments seem to be a little confused about this, I figure I should probably state my assumptions first. I'm assuming that you're trying to check whether something is 0 or empty, or is boolable statically -- that is, you annotate your code using PEP 484 types and run a typechecker like mypy to determine whether a variable is zero or not.

If that is not what you're trying to do, and you're trying to check if something is zero or empty or boolable at runtime (when actually running your code), you can just use isinstance checks -- e.g. stuff like assert x == 0 or assert len(x) == 0 or assert hasattr(x, '__bool__') or hasattr(x, '__len__').


If you are trying statically check if something is zero or empty, there is unfortunately no way of doing this with PEP 484 types. Basically, PEP 484 doesn't let you associate "logical checks" with types -- you can't create a constraint mandating that some function accepts only positive ints, for example.

This is possible in more sophisticated type systems (see dependent type systems, refinement type systems, etc...) but implementing such type systems is very complex so it's extremely unlikely that PEP 484 will be modified to support features like this in the near future.

That said, there are some plans for at least mypy to add support for simple dependent types for literals. For example, while we might not be able to easily tell whether some arbitrary variable is zero or not, we can tell that the literal 0 is a zero. (This feature is particularly useful when trying to type the open(...) function, for example -- it returns a different type depending on the value of the second argument).

There's some related discussion here -- that said, I wouldn't hold your breath waiting for this to be implemented. It's a relatively complex feature.

For now, the only real alternatives are to fall back to using runtime checks or to just restructure your code so you don't need to check if something is zero or empty.


However, if you want to check if some type is "boolable" by checking for the presence of the __bool__ or __len__ methods, you can do so statically by using protocols:

from typing import Union
from typing_extensions import Protocol

classHasBool(Protocol):def__bool__(self) -> bool: ...

classHasLen(Protocol):def__len__(self) -> int: ...

Boolable = Union[HasBool, HasLen]

defaccepts_boolable(x: Boolable) -> None: pass

accepts_boolable(3)
accepts_boolable("asdf")

classNotBoolable: pass

accepts_boolable(NotBoolable())  # Mypy reports an error

You'll need to install the typing_extensions package using pip first. Protocols aren't yet a part of the standard library, but are expected to be standardized in the near future. I'm also not sure if other type checkers beyond mypy support protocols yet.

More documentation about protocols.

Post a Comment for "The Type Of A Variable That Can Be Implicitly Checked For "zeroness" Or "emptiness""