Skip to content

Uninhabited should have all attributes #19300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
TypeVarLikeType,
TypeVarTupleType,
TypeVarType,
UninhabitedType,
UnionType,
get_proper_type,
)
Expand Down Expand Up @@ -269,6 +270,10 @@ def _analyze_member_access(
if not mx.suppress_errors:
mx.msg.deleted_as_rvalue(typ, mx.context)
return AnyType(TypeOfAny.from_error)
elif isinstance(typ, UninhabitedType):
attr_type = UninhabitedType()
attr_type.ambiguous = typ.ambiguous
return attr_type
return report_missing_attribute(mx.original_type, typ, name, mx)


Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -1587,3 +1587,19 @@ x = 0 # not unreachable

f2: Callable[[], NoReturn] = lambda: foo()
x = 0 # not unreachable

[case testAttributeNoReturn]
# flags: --warn-unreachable
from typing import Optional, NoReturn, TypeVar

def foo() -> NoReturn:
raise

T = TypeVar("T")
def bar(x: Optional[list[T]] = None) -> T:
...

reveal_type(bar().attr) # N: Revealed type is "Never"
1 # not unreachable
reveal_type(foo().attr) # N: Revealed type is "Never"
1 # E: Statement is unreachable