Skip to content

Commit 1393e3f

Browse files
Fix None assignments with fine-grained cache (#11574)
Problem: When using the fine-grained cache, we raise an assertion in some situations when an attribute is defined as `None`. Solution: Remove truthiness check to ensure that `None` is an allowed type. Fixes: #8682 Fixes: #11456
1 parent e20042f commit 1393e3f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

mypy/server/deps.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,11 @@ def get_non_partial_lvalue_type(self, lvalue: RefExpr) -> Type:
481481
return UninhabitedType()
482482
lvalue_type = get_proper_type(self.type_map[lvalue])
483483
if isinstance(lvalue_type, PartialType):
484-
if isinstance(lvalue.node, Var) and lvalue.node.type:
485-
lvalue_type = get_proper_type(lvalue.node.type)
484+
if isinstance(lvalue.node, Var):
485+
if lvalue.node.type:
486+
lvalue_type = get_proper_type(lvalue.node.type)
487+
else:
488+
lvalue_type = UninhabitedType()
486489
else:
487490
# Probably a secondary, non-definition assignment that doesn't
488491
# result in a non-partial type. We won't be able to infer any

test-data/unit/fine-grained.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9733,3 +9733,28 @@ class C:
97339733
[out]
97349734
==
97359735
main:5: error: Unsupported left operand type for + ("str")
9736+
[case testNoneAttribute]
9737+
from typing import Generic, TypeVar
9738+
9739+
T = TypeVar('T', int, str)
9740+
9741+
class ExampleClass(Generic[T]):
9742+
def __init__(
9743+
self
9744+
) -> None:
9745+
self.example_attribute = None
9746+
[out]
9747+
==
9748+
[case testStrictNoneAttribute]
9749+
# flags: --strict-optional
9750+
from typing import Generic, TypeVar
9751+
9752+
T = TypeVar('T', int, str)
9753+
9754+
class ExampleClass(Generic[T]):
9755+
def __init__(
9756+
self
9757+
) -> None:
9758+
self.example_attribute = None
9759+
[out]
9760+
==

0 commit comments

Comments
 (0)