Skip to content

Remove Undefined from stubs #696

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

Merged
merged 3 commits into from
May 31, 2015
Merged
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
582 changes: 402 additions & 180 deletions lib-typing/3.2/test_typing.py

Large diffs are not rendered by default.

678 changes: 404 additions & 274 deletions lib-typing/3.2/typing.py

Large diffs are not rendered by default.

38 changes: 5 additions & 33 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
BytesExpr, UnicodeExpr, FloatExpr, OpExpr, UnaryExpr, CastExpr, SuperExpr,
TypeApplication, DictExpr, SliceExpr, FuncExpr, TempNode, SymbolTableNode,
Context, ListComprehension, ConditionalExpr, GeneratorExpr,
Decorator, SetExpr, PassStmt, TypeVarExpr, UndefinedExpr, PrintStmt,
Decorator, SetExpr, PassStmt, TypeVarExpr, PrintStmt,
LITERAL_TYPE, BreakStmt, ContinueStmt, ComparisonExpr, StarExpr,
YieldFromExpr, YieldFromStmt, NamedTupleExpr, SetComprehension,
DictionaryComprehension, ComplexExpr, EllipsisNode, TypeAliasExpr,
Expand Down Expand Up @@ -1004,13 +1004,9 @@ def check_multi_assignment(self, lvalues: List[Node],
if not msg:
msg = messages.INCOMPATIBLE_TYPES_IN_ASSIGNMENT

# First handle case where rvalue is of form Undefined, ...
rvalue_type = get_undefined_tuple(rvalue, self.named_type('builtins.tuple'))
undefined_rvalue = True
if not rvalue_type:
# Infer the type of an ordinary rvalue expression.
rvalue_type = self.accept(rvalue) # TODO maybe elsewhere; redundant
undefined_rvalue = False
# Infer the type of an ordinary rvalue expression.
rvalue_type = self.accept(rvalue) # TODO maybe elsewhere; redundant
undefined_rvalue = False

if isinstance(rvalue_type, AnyType):
for lv in lvalues:
Expand Down Expand Up @@ -1243,13 +1239,7 @@ def narrow_type_from_binder(self, expr: Node, known_type: Type) -> Type:
def check_simple_assignment(self, lvalue_type: Type, rvalue: Node,
context: Node,
msg: str = messages.INCOMPATIBLE_TYPES_IN_ASSIGNMENT) -> Type:
"""Checks the assignment of rvalue to a lvalue of type lvalue_type."""
if refers_to_fullname(rvalue, 'typing.Undefined'):
# The rvalue is just 'Undefined'; this is always valid.
# Infer the type of 'Undefined' from the lvalue type.
self.store_type(rvalue, lvalue_type)
return None
elif self.is_stub and isinstance(rvalue, EllipsisNode):
if self.is_stub and isinstance(rvalue, EllipsisNode):
# '...' is always a valid initializer in a stub.
return AnyType()
else:
Expand Down Expand Up @@ -1797,9 +1787,6 @@ def visit_generator_expr(self, e: GeneratorExpr) -> Type:
def visit_dictionary_comprehension(self, e: DictionaryComprehension) -> Type:
return self.expr_checker.visit_dictionary_comprehension(e)

def visit_undefined_expr(self, e: UndefinedExpr) -> Type:
return self.expr_checker.visit_undefined_expr(e)

def visit_temp_node(self, e: TempNode) -> Type:
return e.type

Expand Down Expand Up @@ -1981,21 +1968,6 @@ def map_type_from_supertype(typ: Type, sub_info: TypeInfo,
return expand_type_by_instance(typ, inst_type)


def get_undefined_tuple(rvalue: Node, tuple_type: Instance) -> Type:
"""Get tuple type corresponding to a tuple of Undefined values.

The type is Tuple[Any, ...]. If rvalue is not of the right form, return
None.
"""
if isinstance(rvalue, TupleExpr):
for item in rvalue.items:
if not refers_to_fullname(item, 'typing.Undefined'):
break
else:
return TupleType([AnyType()] * len(rvalue.items), tuple_type)
return None


def find_isinstance_check(node: Node,
type_map: Dict[Node, Type]) -> Tuple[Node, Type, Type, int]:
"""Check if node is an isinstance(variable, type) check.
Expand Down
5 changes: 1 addition & 4 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
OpExpr, UnaryExpr, IndexExpr, CastExpr, TypeApplication, ListExpr,
TupleExpr, DictExpr, FuncExpr, SuperExpr, SliceExpr, Context,
ListComprehension, GeneratorExpr, SetExpr, MypyFile, Decorator,
UndefinedExpr, ConditionalExpr, ComparisonExpr, TempNode, SetComprehension,
ConditionalExpr, ComparisonExpr, TempNode, SetComprehension,
DictionaryComprehension, ComplexExpr, EllipsisNode, LITERAL_TYPE,
TypeAliasExpr
)
Expand Down Expand Up @@ -1197,9 +1197,6 @@ def check_for_comp(self, e: Union[GeneratorExpr, DictionaryComprehension]) -> No
self.accept(condition)
self.chk.binder.pop_frame()

def visit_undefined_expr(self, e: UndefinedExpr) -> Type:
return e.type

def visit_conditional_expr(self, e: ConditionalExpr) -> Type:
cond_type = self.accept(e.cond)
self.check_not_void(cond_type, e)
Expand Down
16 changes: 0 additions & 16 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,22 +1278,6 @@ def accept(self, visitor: NodeVisitor[T]) -> T:
return visitor.visit_conditional_expr(self)


class UndefinedExpr(Node):
"""Expression None # type: type, used as an initializer.

This is used to declare the type of a variable without initializing with
a proper value. For example:

x = None # type: List[int]
"""

def __init__(self, type: 'mypy.types.Type') -> None:
self.type = type

def accept(self, visitor: NodeVisitor[T]) -> T:
return visitor.visit_undefined_expr(self)


class TypeApplication(Node):
"""Type application expr[type, ...]"""

Expand Down
30 changes: 2 additions & 28 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
GlobalDecl, SuperExpr, DictExpr, CallExpr, RefExpr, OpExpr, UnaryExpr,
SliceExpr, CastExpr, TypeApplication, Context, SymbolTable,
SymbolTableNode, BOUND_TVAR, UNBOUND_TVAR, ListComprehension, GeneratorExpr,
FuncExpr, MDEF, FuncBase, Decorator, SetExpr, UndefinedExpr, TypeVarExpr,
FuncExpr, MDEF, FuncBase, Decorator, SetExpr, TypeVarExpr,
StrExpr, PrintStmt, ConditionalExpr, PromoteExpr,
ComparisonExpr, StarExpr, ARG_POS, ARG_NAMED, MroError, type_aliases,
YieldFromStmt, YieldFromExpr, NamedTupleExpr, NonlocalDecl,
Expand Down Expand Up @@ -821,7 +821,6 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
if s.type:
s.type = self.anal_type(s.type)
else:
s.type = self.infer_type_from_undefined(s.rvalue)
# For simple assignments, allow binding type aliases.
if (s.type is None and len(s.lvalues) == 1 and
isinstance(s.lvalues[0], NameExpr)):
Expand Down Expand Up @@ -988,13 +987,6 @@ def check_lvalue_validity(self, node: Node, ctx: Context) -> None:
if isinstance(node, (FuncDef, TypeInfo, TypeVarExpr)):
self.fail('Invalid assignment target', ctx)

def infer_type_from_undefined(self, rvalue: Node) -> Type:
if isinstance(rvalue, CallExpr):
if isinstance(rvalue.analyzed, UndefinedExpr):
undef = cast(UndefinedExpr, rvalue.analyzed)
return undef.type
return None

def store_declared_types(self, lvalue: Node, typ: Type) -> None:
if isinstance(typ, StarType) and not isinstance(lvalue, StarExpr):
self.fail('Star type only allowed for starred expressions', lvalue)
Expand Down Expand Up @@ -1476,7 +1468,7 @@ def visit_call_expr(self, expr: CallExpr) -> None:
"""Analyze a call expression.

Some call expressions are recognized as special forms, including
cast(...), Undefined(...) and Any(...).
cast(...) and Any(...).
"""
expr.callee.accept(self)
if refers_to_fullname(expr.callee, 'typing.cast'):
Expand All @@ -1501,18 +1493,6 @@ def visit_call_expr(self, expr: CallExpr) -> None:
expr.analyzed = CastExpr(expr.args[0], AnyType())
expr.analyzed.line = expr.line
expr.analyzed.accept(self)
elif refers_to_fullname(expr.callee, 'typing.Undefined'):
# Special form Undefined(...).
if not self.check_fixed_args(expr, 1, 'Undefined'):
return
try:
type = expr_to_unanalyzed_type(expr.args[0])
except TypeTranslationError:
self.fail('Argument to Undefined is not a type', expr)
return
expr.analyzed = UndefinedExpr(type)
expr.analyzed.line = expr.line
expr.analyzed.accept(self)
elif refers_to_fullname(expr.callee, 'typing._promote'):
# Special form _promote(...).
if not self.check_fixed_args(expr, 1, '_promote'):
Expand Down Expand Up @@ -1625,9 +1605,6 @@ def visit_cast_expr(self, expr: CastExpr) -> None:
expr.expr.accept(self)
expr.type = self.anal_type(expr.type)

def visit_undefined_expr(self, expr: UndefinedExpr) -> None:
expr.type = self.anal_type(expr.type)

def visit_type_application(self, expr: TypeApplication) -> None:
expr.expr.accept(self)
for i in range(len(expr.types)):
Expand Down Expand Up @@ -2006,9 +1983,6 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
self.analyze(s.type)
super().visit_assignment_stmt(s)

def visit_undefined_expr(self, e: UndefinedExpr) -> None:
self.analyze(e.type)

def visit_cast_expr(self, e: CastExpr) -> None:
self.analyze(e.type)
super().visit_cast_expr(e)
Expand Down
3 changes: 0 additions & 3 deletions mypy/strconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,6 @@ def visit_index_expr(self, o):
def visit_super_expr(self, o):
return self.dump([o.name], o)

def visit_undefined_expr(self, o):
return 'UndefinedExpr:{}({})'.format(o.line, o.type)

def visit_type_application(self, o):
return self.dump([o.expr, ('Types', o.types)], o)

Expand Down
12 changes: 6 additions & 6 deletions mypy/test/data/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def f():
f()
[out]
'x' <class 'collections.abc.Sized'> True
[1] typing.Sequence[~T] True
{1: 3} typing.Sequence[~T] False
<str_iterator object at 0x...> typing.Iterator[~T] True
'x' typing.Iterable[~T] True
{} typing.Mapping[~KT, ~VT] True
{1} typing.AbstractSet[~T] True
[1] typing.Sequence[+T_co] True
{1: 3} typing.Sequence[+T_co] False
<str_iterator object at 0x...> typing.Iterator[+T_co] True
'x' typing.Iterable[+T_co] True
{} typing.Mapping[+KT_co, +VT_co] True
{1} typing.AbstractSet[+T_co] True

[case testSized]
from typing import Sized
Expand Down
5 changes: 1 addition & 4 deletions mypy/test/testtypegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run_test(self, testcase):
nodes = map.keys()

# Ignore NameExpr nodes of variables with explicit (trivial) types
# to simplify output. Also ignore 'Undefined' nodes.
# to simplify output.
searcher = VariableDefinitionNodeSearcher()
for file in result.files.values():
file.accept(searcher)
Expand Down Expand Up @@ -86,9 +86,6 @@ def visit_assignment_stmt(self, s):
for lvalue in s.lvalues:
if isinstance(lvalue, NameExpr):
self.nodes.add(lvalue)
if (isinstance(s.rvalue, NameExpr)
and s.rvalue.fullname == 'typing.Undefined'):
self.nodes.add(s.rvalue)


def ignore_node(node):
Expand Down
5 changes: 1 addition & 4 deletions mypy/treetransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
ConditionalExpr, DictExpr, SetExpr, NameExpr, IntExpr, StrExpr, BytesExpr,
UnicodeExpr, FloatExpr, CallExpr, SuperExpr, MemberExpr, IndexExpr,
SliceExpr, OpExpr, UnaryExpr, FuncExpr, TypeApplication, PrintStmt,
SymbolTable, RefExpr, UndefinedExpr, TypeVarExpr, PromoteExpr,
SymbolTable, RefExpr, TypeVarExpr, PromoteExpr,
ComparisonExpr, TempNode, StarExpr, YieldFromStmt,
YieldFromExpr, NamedTupleExpr, NonlocalDecl, SetComprehension,
DictionaryComprehension, ComplexExpr, TypeAliasExpr
Expand Down Expand Up @@ -367,9 +367,6 @@ def visit_index_expr(self, node: IndexExpr) -> Node:
new.analyzed.set_line(node.analyzed.line)
return new

def visit_undefined_expr(self, node: UndefinedExpr) -> Node:
return UndefinedExpr(self.type(node.type))

def visit_type_application(self, node: TypeApplication) -> TypeApplication:
return TypeApplication(self.node(node.expr),
self.types(node.types))
Expand Down
3 changes: 0 additions & 3 deletions mypy/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ def visit_set_expr(self, o: 'mypy.nodes.SetExpr') -> T:
def visit_index_expr(self, o: 'mypy.nodes.IndexExpr') -> T:
pass

def visit_undefined_expr(self, o: 'mypy.nodes.UndefinedExpr') -> T:
pass

def visit_type_application(self, o: 'mypy.nodes.TypeApplication') -> T:
pass

Expand Down
16 changes: 7 additions & 9 deletions stubs/2.7/__future__.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from typing import Undefined

class _Feature: pass

absolute_import = Undefined(_Feature)
division = Undefined(_Feature)
generators = Undefined(_Feature)
nested_scopes = Undefined(_Feature)
print_function = Undefined(_Feature)
unicode_literals = Undefined(_Feature)
with_statement = Undefined(_Feature)
absolute_import = None # type: _Feature
division = None # type: _Feature
generators = None # type: _Feature
nested_scopes = None # type: _Feature
print_function = None # type: _Feature
unicode_literals = None # type: _Feature
with_statement = None # type: _Feature
18 changes: 9 additions & 9 deletions stubs/2.7/builtins.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Stubs for builtins (Python 2.7)

from typing import (
Undefined, TypeVar, Iterator, Iterable, overload,
TypeVar, Iterator, Iterable, overload,
Sequence, Mapping, Tuple, List, Any, Dict, Callable, Generic, Set,
AbstractSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsAbs,
SupportsRound, IO, BinaryIO, Union, AnyStr
Expand All @@ -23,7 +23,7 @@ property = object()

class object:
__doc__ = ''
__class__ = Undefined # type: type
__class__ = None # type: type

def __init__(self) -> None: pass
def __eq__(self, o: object) -> bool: pass
Expand All @@ -35,7 +35,7 @@ class object:
class type:
__name__ = ''
__module__ = ''
__dict__ = Undefined # type: Dict[unicode, Any]
__dict__ = None # type: Dict[unicode, Any]

def __init__(self, o: object) -> None: pass
# TODO: __new__ may have to be special and not a static method.
Expand Down Expand Up @@ -578,7 +578,7 @@ class list(Sequence[_T], Reversible[_T], Generic[_T]):
def append(self, object: _T) -> None: pass
def extend(self, iterable: Iterable[_T]) -> None: pass
def pop(self, index: int = -1) -> _T: pass
def index(self, object: _T, start: int = 0, stop: int = Undefined(int)) -> int: pass
def index(self, object: _T, start: int = 0, stop: int = None) -> int: pass
def count(self, object: _T) -> int: pass
def insert(self, index: int, object: _T) -> None: pass
def remove(self, object: _T) -> None: pass
Expand Down Expand Up @@ -709,16 +709,16 @@ class xrange(Sized, Iterable[int], Reversible[int]):
class module:
__name__ = ''
__file__ = ''
__dict__ = Undefined # type: Dict[unicode, Any]
__dict__ = None # type: Dict[unicode, Any]

True = Undefined # type: bool
False = Undefined # type: bool
True = None # type: bool
False = None # type: bool
__debug__ = False

long = int
bytes = str

NotImplemented = Undefined # type: Any
NotImplemented = None # type: Any

def abs(n: SupportsAbs[_T]) -> _T: pass
def all(i: Iterable) -> bool: pass
Expand Down Expand Up @@ -841,7 +841,7 @@ def __import__(name: unicode,
# Exceptions

class BaseException:
args = Undefined # type: Any
args = None # type: Any
def __init__(self, *args: Any) -> None: pass
def with_traceback(self, tb: Any) -> BaseException: pass

Expand Down
4 changes: 2 additions & 2 deletions stubs/2.7/difflib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
# TODO: Support unicode?

from typing import (
TypeVar, Callable, Iterable, List, NamedTuple, Sequence, Tuple, Generic, Undefined
TypeVar, Callable, Iterable, List, NamedTuple, Sequence, Tuple, Generic
)

_T = TypeVar('_T')

class SequenceMatcher(Generic[_T]):
def __init__(self, isjunk: Callable[[_T], bool] = None,
a: Sequence[_T] = Undefined, b: Sequence[_T] = Undefined,
a: Sequence[_T] = None, b: Sequence[_T] = None,
autojunk: bool = True) -> None: pass
def set_seqs(self, a: Sequence[_T], b: Sequence[_T]) -> None: pass
def set_seq1(self, a: Sequence[_T]) -> None: pass
Expand Down
2 changes: 1 addition & 1 deletion stubs/2.7/re.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# based on: http://docs.python.org/2.7/library/re.html

from typing import (
Undefined, List, Iterator, overload, Callable, Tuple, Sequence, Dict,
List, Iterator, overload, Callable, Tuple, Sequence, Dict,
Generic, AnyStr, Match, Pattern
)

Expand Down
Loading