Skip to content
Open
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
17 changes: 16 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8028,7 +8028,22 @@ def get_type_range_of_type(self, typ: Type) -> TypeRange | None:

if isinstance(typ, UnionType):
type_ranges = [self.get_type_range_of_type(item) for item in typ.items]
item = make_simplified_union([t.item for t in type_ranges if t is not None])
if any(t is None for t in type_ranges):
return None
# Filter out None and UninhabitedType entries (type checkers need explicit None check)
valid_ranges = [
t
for t in type_ranges
if t is not None and not isinstance(get_proper_type(t.item), UninhabitedType)
]
if not valid_ranges:
return TypeRange(UninhabitedType(), is_upper_bound=False)
# If the only meaningful type we can extract is "object", we've lost
# type precision (e.g. from a widened _ClassInfo alias). Return None
# to avoid narrowing to a useless type.
item = make_simplified_union([t.item for t in valid_ranges])
if isinstance(item, Instance) and item.type.fullname == "builtins.object":
return None
return TypeRange(item, is_upper_bound=True)
if isinstance(typ, FunctionLike) and typ.is_type_obj():
# If a type is generic, `isinstance` can only narrow its variables to Any.
Expand Down
Loading