8000 Stricter None handling with --no-strict-optional (#11717) · python/mypy@e8cf960 · GitHub
[go: up one dir, main page]

Skip to content

Commit e8cf960

Browse files
authored
Stricter None handling with --no-strict-optional (#11717)
Closes #11705
1 parent e0a89b7 commit e8cf960

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

mypy/checker.py

Lines changed: 6 additions & 7 deletions
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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,10 @@ class Status(Enum):
704704
typ = get_proper_type(typ)
705705

706706
if isinstance(typ, UnionType):
707-
items = [try_expanding_sum_type_to_union(item, target_fullname) for item in typ.items]
707+
items = [
708+
try_expanding_sum_type_to_union(item, target_fullname)
709+
for item in typ.relevant_items()
710+
]
708711
return make_simplified_union(items, contract_literals=False)
709712
elif isinstance(typ, Instance) and typ.type.fullname == target_fullname:
710713
if typ.type.is_enum:

test-data/unit/check-inference.test

Lines changed: 30 additions & 0 deletions
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)
0