Skip to content

Pylance should ignore treated errors #2452

Answered by erictraut
Jota-Ve asked this question in Ideas
Discussion options

You must be logged in to vote

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 use try/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 that x might be None in input, you should check for that condition and handle it appropriately. If your intent is that x should never contain None on input, you can change its type from Optional[str] to str and let the static type checker detect any places where a caller is potentially passing None.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Jota-Ve
Comment options

@erictraut
Comment options

Answer selected by Jota-Ve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants