Skip to content

Strategies for Optional attributes that become non-Optional during runtime? #838

Answered by JelleZijlstra
jeremyn asked this question in Q&A
Discussion options

You must be logged in to vote

I don't think the type system provides any direct way to do what you want here, and it's hard to imagine a clean way to add something like this. Most type checkers should support something like assert obj.key is not None, but that gets repetitive.

Personally I would try to rewrite this code so that the key attribute is known when the object is initialized.

Replies: 5 comments 8 replies

Comment options

You must be logged in to vote
1 reply
@jeremyn
Comment options

Answer selected by jeremyn
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@jeremyn
Comment options

@sobolevn
Comment options

Comment options

You must be logged in to vote
4 replies
@jeremyn
Comment options

@JelleZijlstra
Comment options

@sobolevn
Comment options

@jeremyn
Comment options

Comment options

You must be logged in to vote
1 reply
@erictraut
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants