-
Shouldn't Pylance ignore type errors if it is inside a try block ? I know it won't always be desired, but there are some really obvious examples where the error is clearly expected and treated, so why complain it at all ? Here is a dummy example similar to what I run into very often
I know This forces me to use the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Type consistency errors within a try block are not (and should not be) ignored by a type checker. As you pointed out, you can use a |
Beta Was this translation helpful? Give feedback.
Type consistency errors within a try block are not (and should not be) ignored by a type checker.
As you pointed out, you can use a
# type: ignore
if you want. Even better is to refactor your code so you don't usetry/except
for non-exceptional code flow. Using it in this manner is a bit of an abuse of the language feature. In your example above, if you know thatx
might beNone
in input, you should check for that condition and handle it appropriately. If your intent is thatx
should never containNone
on input, you can change its type fromOptional[str]
tostr
and let the static type checker detect any places where a caller is potentially passing None.