Skip to content

[conflict] Allow assignment of types with Nones without explicit annotation #2225

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

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 29 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2698,22 +2698,46 @@ def infer_operator_assignment_method(type: Type, operator: str) -> Tuple[bool, s
def is_valid_inferred_type(typ: Type) -> bool:
"""Is an inferred type valid?

Examples of invalid types include the None type or a type with a None component.
Examples of invalid types include the None type or List[None].

When not doing strict Optional checking, all types containing None are
invalid. When doing strict Optional checking, only types that are
incompletely defined (i.e. contain UninhabitedType) or can be turned into
PartialTypes are invalid.
"""
if is_same_type(typ, NoneTyp()):
# With strict Optional checking, we *may* eventually infer NoneTyp, but
# we only do that if we can't infer a specific Optional type. This
# resolution happens in leave_partial_types when we pop a partial types
# scope.
return False
return is_valid_inferred_type_component(typ)


def is_valid_inferred_type_component(typ: Type) -> bool:
"""Is this part of a type a valid inferred type?

In strict Optional mode this excludes bare None types, as otherwise every
type containing None would be invalid.
"""
if not experiments.STRICT_OPTIONAL:
if is_same_type(typ, NoneTyp()):
return False
if is_same_type(typ, UninhabitedType()):
return False
elif isinstance(typ, Instance):
for arg in typ.args:
if not is_valid_inferred_type(arg):
return False
fullname = typ.type.fullname()
if ((fullname == 'builtins.list' or
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that these are special cased as a convenience, as collections with all None types are rarely useful and thus we require them to be annotated, but for other generic types all None args may be totally reasonable. This seems pretty reasonable to me, but it's worth adding a comment here about why we special case just these types.

Also, can you add tests for the special casing?

fullname == 'builtins.set' or
fullname == 'builtins.dict') and
all(isinstance(t, (NoneTyp, UninhabitedType)) for t in typ.args)):
return False
else:
for arg in typ.args:
if not is_valid_inferred_type_component(arg):
return False
elif isinstance(typ, TupleType):
for item in typ.items:
if not is_valid_inferred_type(item):
if not is_valid_inferred_type_component(item):
return False
return True
7 changes: 7 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,10 @@ x + 1
[out]
main:2: note: In module imported here:
tmp/a.py:3: error: Unsupported left operand type for + (some union)

[case testOptionalNonPartialTypeWithNone]
from typing import Generator
def f() -> Generator[str, None, None]: pass
x = f()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reveal the type of x.

l = [f()]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reveal the type of l.

[builtins fixtures/list.pyi]