Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18bfc01

Browse files
authored
prevent false unreachable warnings for @Final instances that occur when strict optional checking is disabled (#20045)
Fixes #19849 See #11717 for some background information on `--no-strict-optional`.
1 parent 04a586c commit 18bfc01

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

mypy/typeops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,9 @@ def false_only(t: Type) -> ProperType:
789789
if not ret_type.can_be_false:
790790
return UninhabitedType(line=t.line)
791791
elif isinstance(t, Instance):
792-
if t.type.is_final or t.type.is_enum:
792+
if (t.type.is_final or t.type.is_enum) and state.strict_optional:
793793
return UninhabitedType(line=t.line)
794-
elif isinstance(t, LiteralType) and t.is_enum_literal():
794+
elif isinstance(t, LiteralType) and t.is_enum_literal() and state.strict_optional:
795795
return UninhabitedType(line=t.line)
796796

797797
new_t = copy_type(t)

test-data/unit/check-inference.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,31 @@ if 'x' in d: # E: "None" has no attribute "__iter__" (not iterable)
19641964
reveal_type(d) # N: Revealed type is "None"
19651965
[builtins fixtures/dict.pyi]
19661966

1967+
[case testNoWrongUnreachableWarningWithNoStrictOptionalAndFinalInstance]
1968+
# flags: --no-strict-optional --warn-unreachable
1969+
from typing import final, Optional
1970+
1971+
@final
1972+
class C: ...
1973+
1974+
x: Optional[C]
1975+
if not x:
1976+
x = C()
1977+
[builtins fixtures/dict.pyi]
1978+
1979+
[case testNoWrongUnreachableWarningWithNoStrictOptionalAndEnumLiteral]
1980+
# flags: --no-strict-optional --warn-unreachable
1981+
from enum import Enum
1982+
from typing import Literal, Optional
1983+
1984+
class E(Enum):
1985+
a = 1
1986+
1987+
x: Optional[Literal[E.a]]
1988+
if not x:
1989+
x = E.a
1990+
[builtins fixtures/dict.pyi]
1991+
19671992
[case testInferFromEmptyListWhenUsingInWithStrictEquality]
19681993
# flags: --strict-equality
19691994
def f() -> None:

0 commit comments

Comments
 (0)