Skip to content

Conversation

tyralla
Copy link
Collaborator

@tyralla tyralla commented Oct 10, 2025

Fixes #19849

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

…en strict optional checking is disabled.

This comment has been minimized.

Copy link
Collaborator

@sterliakov sterliakov left a comment

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
Collaborator Author

tyralla commented Oct 11, 2025

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?

@tyralla tyralla requested a review from ilevkivskyi October 11, 2025 08:53
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
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
Collaborator Author

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.

Copy link
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]

@tyralla tyralla requested a review from ilevkivskyi October 11, 2025 19:40
@ilevkivskyi ilevkivskyi merged commit 18bfc01 into python:master Oct 12, 2025
20 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.

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

3 participants