I have a container class that can contain whatever value type is inserted into it.
Now I have the following situation:
def foo(val: str)
print(str)
container.add('string')
value = container.get_first()
foo(value) # Static type checker complains here
I know with certainty that the value coming out of container
will be a string, but as far as the type checker is concerned, this value is unknown, and it has problems with me passing it to foo()
, which takes a string.
I can't typehint container
because it's imported from a third party library. How do I tell the type checker that it's OK to assume a string?
question from:
https://stackoverflow.com/questions/66050626/how-do-i-type-hint-values-extracted-from-a-type-agnostic-container 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…