Skip to content

prevent false unreachable warnings for @final instances that occur when strict optional checking is disabled - #20045

Merged
ilevkivskyi merged 2 commits into
python:masterfrom
tyralla:fix/unreachable_nostrictoptional_final
Oct 12, 2025
Merged

prevent false unreachable warnings for @final instances that occur when strict optional checking is disabled#20045
ilevkivskyi merged 2 commits into
python:masterfrom
tyralla:fix/unreachable_nostrictoptional_final

Conversation

@tyralla

Copy link
Copy Markdown
Collaborator

Fixes#19849

See #11717 for some background information on --no-strict-optional.

@github-actions

This comment has been minimized.

@sterliakovsterliakov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Primer change looks correct to me, result is defined earlier at L470 to an incompatible type. This branch was apparently unreachable before this change.

@tyralla

Copy link
Copy Markdown
CollaboratorAuthor

Yes, I think you are right. Mypy thought the branch was unreachable because EQUAL_NULL_PATTERN.match(constraint) returns Match | None, which it interprets as Match because of --no-strict-optional.

@ilevkivskyi: Not a super critical bug, but at least a super simple fix. Could you take a look at it?

Comment threadmypy/typeops.py
return UninhabitedType(line=t.line)
elif isinstance(t, Instance):
if t.type.is_final or t.type.is_enum:
if (t.type.is_final or t.type.is_enum) and state.strict_optional:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this should apply to the branch just below as well. I mean what if in your test you will have x: Optional[Literal[SomeEnum.Foo]]? Or those are not simplified when strict optional is False?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Thanks, you're right. I did as suggested and added the corresponding test case testNoWrongUnreachableWarningWithNoStrictOptionalAndEnumLiteral.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

pwndbg (https://github.com/pwndbg/pwndbg)
+ pwndbg/aglib/onegadget.py: note: In function "check_constraint":+ pwndbg/aglib/onegadget.py:495: error: Argument 1 to "find" has incompatible type "CheckSatResult"; expected "int | Value | None" [arg-type]+ pwndbg/aglib/onegadget.py:507: error: Argument 1 to "find" has incompatible type "CheckSatResult"; expected "int | Value | None" [arg-type]

@ilevkivskyi
ilevkivskyi merged commit 18bfc01 into python:masterOct 12, 2025
20 checks passed
Sign up for freeto 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.

False positive: Statement is [unreachable] in loop with iteration-carried flag

3 participants

@tyralla@ilevkivskyi@sterliakov