[Question] Is it proper to use union with Any
to work against type-narrowing?
#2900
Unanswered
Azureblade3808
asked this question in
General
Replies: 2 comments 3 replies
-
Converting to a discussion topic since this is a question rather than a bug report or enhancement request. |
Beta Was this translation helpful? Give feedback.
1 reply
-
In fact, I was trying to avoid explicitly annotating the type of
I just want to make sure that such usage of |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've been playing with some black magics to make my coding work easier, and got some code like -
Writing
__v
with any non-int value, or writingv
with any value, would be rejected by type-checker. And type ofv
was inferred asint
as expected.And then I experimented with some tuple property -
In this case, type of
v
was inferred astuple[()]
instead oftuple[int, ...]
due to type-narrowing, which was expectable but beated my purpose. As a solution, I changed the signature ofcreate_property
to -Since then, everything worked perfectly, except for that type of
v
was inferred astuple[()] | tuple[int, ...]
instead of justtuple[int, ...]
, which was somehow acceptable.Here is the question -
Am I doing right to use union with
Any
this way?Beta Was this translation helpful? Give feedback.
All reactions