Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

python - How do I type hint values extracted from a type agnostic container?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...