Skip to content

Commit 73de602

Browse files
sobolevnJukkaL
authored andcommitted
Stricter None handling with --no-strict-optional (#11717)
Closes #11705
1 parent 221a7f3 commit 73de602

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

mypy/checker.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -4550,17 +4550,16 @@ def has_no_custom_eq_checks(t: Type) -> bool:
45504550
self._check_for_truthy_type(original_vartype, node)
45514551
vartype = try_expanding_sum_type_to_union(original_vartype, "builtins.bool")
45524552

4553-
if_type = true_only(vartype) # type: Type
4554-
else_type = false_only(vartype) # type: Type
4555-
ref = node # type: Expression
4553+
if_type = true_only(vartype)
4554+
else_type = false_only(vartype)
45564555
if_map = (
4557-
{ref: if_type}
4558-
if not isinstance(get_proper_type(if_type), UninhabitedType)
4556+
{node: if_type}
4557+
if not isinstance(if_type, UninhabitedType)
45594558
else None
45604559
)
45614560
else_map = (
4562-
{ref: else_type}
4563-
if not isinstance(get_proper_type(else_type), UninhabitedType)
4561+
{node: else_type}
4562+
if not isinstance(else_type, UninhabitedType)
45644563
else None
45654564
)
45664565
return if_map, else_map

mypy/typeops.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,10 @@ class Status(Enum):
723723
typ = get_proper_type(typ)
724724

725725
if isinstance(typ, UnionType):
726-
items = [try_expanding_sum_type_to_union(item, target_fullname) for item in typ.items]
726+
items = [
727+
try_expanding_sum_type_to_union(item, target_fullname)
728+
for item in typ.relevant_items()
729+
]
727730
return make_simplified_union(items, contract_literals=False)
728731
elif isinstance(typ, Instance) and typ.type.fullname == target_fullname:
729732
if typ.type.is_enum:

test-data/unit/check-inference.test

+30
Original file line numberDiff line numberDiff line change
@@ -3210,3 +3210,33 @@ def test(seq: List[Union[Iterable, Any]]) -> None:
32103210
k = [k]
32113211
reveal_type(k) # N: Revealed type is "builtins.list[Any]"
32123212
[builtins fixtures/list.pyi]
3213+
3214+
[case testRegression11705_Strict]
3215+
# flags: --strict-optional
3216+
# See: https://github.com/python/mypy/issues/11705
3217+
from typing import Dict, Optional, NamedTuple
3218+
class C(NamedTuple):
3219+
x: int
3220+
3221+
t: Optional[C]
3222+
d: Dict[C, bytes]
3223+
x = t and d[t]
3224+
reveal_type(x) # N: Revealed type is "Union[None, builtins.bytes*]"
3225+
if x:
3226+
reveal_type(x) # N: Revealed type is "builtins.bytes*"
3227+
[builtins fixtures/dict.pyi]
3228+
3229+
[case testRegression11705_NoStrict]
3230+
# flags: --no-strict-optional
3231+
# See: https://github.com/python/mypy/issues/11705
3232+
from typing import Dict, Optional, NamedTuple
3233+
class C(NamedTuple):
3234+
x: int
3235+
3236+
t: Optional[C]
3237+
d: Dict[C, bytes]
3238+
x = t and d[t]
3239+
reveal_type(x) # N: Revealed type is "builtins.bytes*"
3240+
if x:
3241+
reveal_type(x) # N: Revealed type is "builtins.bytes*"
3242+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)