Add support for NameConstant.

This commit is contained in:
whitequark 2015-06-06 15:12:40 +03:00
parent eb76f594a0
commit d08598fa0f
2 changed files with 17 additions and 3 deletions

View File

@ -155,6 +155,10 @@ class TValue(Type):
def __ne__(self, other):
return not (self == other)
def TNone():
"""The type of None."""
return TMono("NoneType")
def TBool():
"""A boolean type."""
return TMono("bool")
@ -193,6 +197,9 @@ class TypePrinter(object):
self.map[typ] = "'%s" % next(self.gen)
return self.map[typ]
elif isinstance(typ, TMono):
if typ.params == {}:
return typ.name
else:
return "%s(%s)" % (typ.name, ", ".join(
["%s=%s" % (k, self.name(typ.params[k])) for k in typ.params]))
elif isinstance(typ, TTuple):

View File

@ -215,6 +215,13 @@ class Inferencer(algorithm.Transformer):
return asttyped.NameT(type=self._find_name(node.id, node.loc),
id=node.id, ctx=node.ctx, loc=node.loc)
def visit_NameConstant(self, node):
if node.value is True or node.value is False:
typ = types.TBool()
elif node.value is None:
typ = types.TNone()
return asttyped.NameConstantT(type=typ, value=node.value, loc=node.loc)
def visit_Tuple(self, node):
node = self.generic_visit(node)
return asttyped.TupleT(type=types.TTuple([x.type for x in node.elts]),
@ -303,7 +310,7 @@ class Printer(algorithm.Visitor):
def generic_visit(self, node):
if hasattr(node, "type"):
self.rewriter.insert_after(node.loc,
":%s".format(self.type_printer.name(node.type)))
":{}".format(self.type_printer.name(node.type)))
super().generic_visit(node)