10

I would like to annotate a variable such as it can be of any value except None, something like:

x: typing.Required(typing.Any) = 42

The typing module has the Union function, but there is no counterpart that would allow to exclude.

Kentzo
  • 3,881
  • 29
  • 54
  • `Any` disables typechecking, it does not validate each object, therefore `Any, except None` is not possible. You may want to try `object` instead, but IDK how to exclude NoneType from it, or if it is even possible. (Union conserves only the most encompassing types, so this route seems closed) – Reblochon Masque Nov 10 '17 at 05:12
  • The documentation only mentions "Every type is compatible with Any / Any is compatible with every type". I'm not aware of implementation details or how they were adjusted with new versions of Python. – Kentzo Nov 10 '17 at 06:28

0 Answers0