Does pyright treat intermediate object types differently to objects created in a return statement? #9527
-
Just ran into this on torch==2.5.1 (notably, not on torch==2.3.1).
This was an unexpected gotchya for me. Is this behaviour intended? Is there a good reason that pyright doesn't type objects that are created in a return statement the same way it types objects assigned to a variable? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yes, this is expected behavior. The expression within a return statement is evaluated using bidirectional type inference. The return type provides the "expected type" as context. When the same expression is assigned to a variable whose type is not declared, regular inference rules apply, since there is no context. If you want |
Beta Was this translation helpful? Give feedback.
Yes, this is expected behavior. The expression within a return statement is evaluated using bidirectional type inference. The return type provides the "expected type" as context. When the same expression is assigned to a variable whose type is not declared, regular inference rules apply, since there is no context.
If you want
get_param_a_1
to work the same way asget_param_a_2
, you can provide an explicit type declaration for theparam_a
variable, as you've done inget_param_a_3
.