Skip to content

Allow TYPE_CHECKING guards in match statements#1726

Merged
DetachHead merged 5 commits intoDetachHead:mainfrom
grahamcracker1234:gbp/type-checking-guards-in-match-statements
Feb 18, 2026
Merged

Allow TYPE_CHECKING guards in match statements#1726
DetachHead merged 5 commits intoDetachHead:mainfrom
grahamcracker1234:gbp/type-checking-guards-in-match-statements

Conversation

@grahamcracker1234
Copy link
Copy Markdown
Contributor

Comment thread packages/pyright-internal/src/analyzer/binder.ts
Comment thread packages/pyright-internal/src/tests/samples/unreachable1.py
Comment thread packages/pyright-internal/src/tests/samples/unreachable2.py
Comment thread packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts
@github-actions

This comment has been minimized.

Comment thread packages/pyright-internal/src/analyzer/binder.ts Outdated
Comment thread packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment on lines +20603 to +20606
const suiteInNotTypeChecking =
node.d.suite.d.statements.length > 0 && isNotTypeCheckingBlock(node.d.suite.d.statements[0]);
if (subjectTypeResult.isIncomplete || suiteInNotTypeChecking) continue;
checkForUnusedPattern(evaluatorInterface, node.d.pattern, subjectType, node.parent.d.expr);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason to continue here instead of keeping the original logic? i think this might be causing the unintended changes from the primer

Suggested change
const suiteInNotTypeChecking =
node.d.suite.d.statements.length > 0 && isNotTypeCheckingBlock(node.d.suite.d.statements[0]);
if (subjectTypeResult.isIncomplete || suiteInNotTypeChecking) continue;
checkForUnusedPattern(evaluatorInterface, node.d.pattern, subjectType, node.parent.d.expr);
const suiteInNotTypeChecking =
node.d.suite.d.statements.length > 0 && isNotTypeCheckingBlock(node.d.suite.d.statements[0]);
if (!subjectTypeResult.isIncomplete && !suiteInNotTypeChecking) {
checkForUnusedPattern(evaluatorInterface, node.d.pattern, subjectType, node.parent.d.expr);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the continue

Comment thread packages/pyright-internal/src/analyzer/binder.ts Outdated
@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@DetachHead DetachHead merged commit 83c1dc7 into DetachHead:main Feb 18, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TYPE_CHECKING guard not respected in match exhaustiveness / unreachable-code analysis

2 participants