Skip to content

Commit

Permalink
Allow assignment of types with Nones without explicit annotation
Browse files Browse the repository at this point in the history
Currently, mypy will not infer types containing None unless they are
valid PartialTypes (i.e. are bare None, list, set, or dict). This means
that all assignments of such types must be explicitly annotated.
It doesn't make sense to require this with strict Optional, as None is a
perfectly valid type.  This PR makes types containing None valid
inference targets.

Fixes #2195.
  • Loading branch information
ddfisher committed Oct 7, 2016
1 parent ea23ac0 commit 8643103
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
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
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()
l = [f()]
[builtins fixtures/list.pyi]

0 comments on commit 8643103

Please sign in to comment.