I have a unique and constant object as a special value, which I would like to use for type-hints in different occasions.
Lets assume I have a situation like this:
NO_RESULT = object()
def foo() -> Hashable | ???:
try:
result : Hashable = res()
return result
except:
return NO_RESULT
Here I would like to annotate the return value foo() -> Literal[NO_RESULT] | Hashable
. However, this is not proper and I will get warnings as this is not a literal value.
I know I could use the following code and annotate it with NO_RESULT_TYPE
NO_RESULT_TYPE = NewType("NO_RESULT_TYPE", object)
NO_RESULT = NO_RESULT_TYPE(object())
However, as this refers to instances of a type, and not a unique object, which is a semantic difference I do not want to have.
How do I have to modify NO_RESULT
so that I can use it in type-hints as well as a variable?