Skip to content

Commit 3807148

Browse files
committed
Uninhabited should have all attributes
1 parent 5081c59 commit 3807148

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

mypy/checkmember.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
TypeVarLikeType,
7171
TypeVarTupleType,
7272
TypeVarType,
73+
UninhabitedType,
7374
UnionType,
7475
get_proper_type,
7576
)
@@ -269,6 +270,10 @@ def _analyze_member_access(
269270
if not mx.suppress_errors:
270271
mx.msg.deleted_as_rvalue(typ, mx.context)
271272
return AnyType(TypeOfAny.from_error)
273+
elif isinstance(typ, UninhabitedType):
274+
attr_type = UninhabitedType()
275+
attr_type.ambiguous = typ.ambiguous
276+
return attr_type
272277
return report_missing_attribute(mx.original_type, typ, name, mx)
273278

274279

test-data/unit/check-unreachable-code.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,3 +1587,19 @@ x = 0 # not unreachable
15871587

15881588
f2: Callable[[], NoReturn] = lambda: foo()
15891589
x = 0 # not unreachable
1590+
1591+
[case testAttributeNoReturn]
1592+
# flags: --warn-unreachable
1593+
from typing import Optional, NoReturn, TypeVar
1594+
1595+
def foo() -> NoReturn:
1596+
raise
1597+
1598+
T = TypeVar("T")
1599+
def bar(x: Optional[list[T]] = None) -> T:
1600+
...
1601+
1602+
reveal_type(bar().attr) # N: Revealed type is "Never"
1603+
1 # not unreachable
1604+
reveal_type(foo().attr) # N: Revealed type is "Never"
1605+
1 # E: Statement is unreachable

0 commit comments

Comments
 (0)