Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,9 +789,9 @@ def false_only(t: Type) -> ProperType:
if not ret_type.can_be_false:
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.

return UninhabitedType(line=t.line)
elif isinstance(t, LiteralType) and t.is_enum_literal():
elif isinstance(t, LiteralType) and t.is_enum_literal() and state.strict_optional:
return UninhabitedType(line=t.line)

new_t = copy_type(t)
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,31 @@ if 'x' in d: # E: "None" has no attribute "__iter__" (not iterable)
reveal_type(d) # N: Revealed type is "None"
[builtins fixtures/dict.pyi]

[case testNoWrongUnreachableWarningWithNoStrictOptionalAndFinalInstance]
# flags: --no-strict-optional --warn-unreachable
from typing import final, Optional

@final
class C: ...

x: Optional[C]
if not x:
x = C()
[builtins fixtures/dict.pyi]

[case testNoWrongUnreachableWarningWithNoStrictOptionalAndEnumLiteral]
# flags: --no-strict-optional --warn-unreachable
from enum import Enum
from typing import Literal, Optional

class E(Enum):
a = 1

x: Optional[Literal[E.a]]
if not x:
x = E.a
[builtins fixtures/dict.pyi]

[case testInferFromEmptyListWhenUsingInWithStrictEquality]
# flags: --strict-equality
def f() -> None:
Expand Down