Skip to content

Commit

Permalink
[ASDL] assert not None in pretty-printing methods rather than 'if'.
Browse files Browse the repository at this point in the history
'if' should only be used for MaybeType fields.

typed_demo.* now passes under strict mode.

Fix lint checks.
  • Loading branch information
Andy Chu committed Feb 14, 2019
1 parent e5c49c6 commit 61c9f56
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
14 changes: 6 additions & 8 deletions asdl/format.py
Expand Up @@ -14,9 +14,7 @@
- objects with name fields
- abbreviated, unnamed fields
"""

from typing import Tuple, List, IO
from asdl.runtime import PrettyLeaf, PrettyNode, PrettyArray, _PrettyBase

from cStringIO import StringIO

Expand Down Expand Up @@ -222,7 +220,7 @@ def PopColor(self):

INDENT = 2

def _PrintWrappedArray(array, # type: List[_PrettyBase]
def _PrintWrappedArray(array, # type: List[runtime._PrettyBase]
prefix_len, # type: int
f, # type: ColorOutput
indent, # type: int
Expand Down Expand Up @@ -257,7 +255,7 @@ def _PrintWrappedArray(array, # type: List[_PrettyBase]


def _PrintWholeArray(array, prefix_len, f, indent, max_col):
# type: (List[_PrettyBase], int, ColorOutput, int, int) -> bool
# type: (List[runtime._PrettyBase], int, ColorOutput, int, int) -> bool

# This is UNLIKE the abbreviated case above, where we do WRAPPING.
# Here, ALL children must fit on a single line, or else we separate
Expand Down Expand Up @@ -290,7 +288,7 @@ def _PrintWholeArray(array, prefix_len, f, indent, max_col):


def _PrintTreeObj(node, f, indent, max_col):
# type: (PrettyNode, ColorOutput, int, int) -> None
# type: (runtime.PrettyNode, ColorOutput, int, int) -> None
"""Print a CompoundObj in abbreviated or normal form."""
ind = ' ' * indent

Expand Down Expand Up @@ -358,7 +356,7 @@ def _PrintTreeObj(node, f, indent, max_col):


def PrintTree(node, f, indent=0, max_col=100):
# type: (_PrettyBase, ColorOutput, int, int) -> None
# type: (runtime._PrettyBase, ColorOutput, int, int) -> None
"""Second step of printing: turn homogeneous tree into a colored string.
Args:
Expand Down Expand Up @@ -391,7 +389,7 @@ def PrintTree(node, f, indent=0, max_col=100):


def _TrySingleLineObj(node, f, max_chars):
# type: (PrettyNode, ColorOutput, int) -> bool
# type: (runtime.PrettyNode, ColorOutput, int) -> bool
"""Print an object on a single line."""
f.write(node.left)
if node.abbrev:
Expand Down Expand Up @@ -420,7 +418,7 @@ def _TrySingleLineObj(node, f, max_chars):
return True


def _TrySingleLine(node, # type: _PrettyBase
def _TrySingleLine(node, # type: runtime._PrettyBase
f, # type: ColorOutput
max_chars, # type: int
):
Expand Down
6 changes: 3 additions & 3 deletions asdl/gen_python.py
Expand Up @@ -167,6 +167,7 @@ def _CodeSnippet(self, method_name, var_name, desc):
elif isinstance(desc, meta.SumType):
if desc.is_simple:
code_str = 'PrettyLeaf(%s.name, Color_TypeName)' % var_name
none_guard = True # otherwise MyPy complains about foo.name
else:
code_str = '%s.%s()' % (var_name, method_name)
none_guard = True
Expand Down Expand Up @@ -207,9 +208,8 @@ def _EmitCodeForField(self, method_name, field_name, desc, counter):
code_str, obj_none_guard = self._CodeSnippet(method_name, var_name, desc)

depth = self.current_depth
if obj_none_guard:
self.Emit(' if self.%s is not None:' % field_name)
depth += 1 # MUTATION
if obj_none_guard: # to satisfy MyPy type system
self.Emit(' assert self.%s is not None' % field_name)
self.Emit(' %s = %s' % (out_val_name, code_str), depth)

self.Emit(' L.append((%r, %s))' % (field_name, out_val_name), depth)
Expand Down
2 changes: 1 addition & 1 deletion asdl/pretty.py
Expand Up @@ -6,7 +6,7 @@
try:
import fastlex
except ImportError:
fastlex = None
fastlex = None # type: ignore

# Word characters, - and _, as well as path name characters . and /.
PLAIN_WORD_RE = r'[a-zA-Z0-9\-_./]+'
Expand Down
2 changes: 1 addition & 1 deletion asdl/pyann_driver.py
Expand Up @@ -6,7 +6,7 @@

from pyannotate_runtime import collect_types

from asdl import typed_arith_parse_test
#from asdl import typed_arith_parse_test
from asdl import format_test


Expand Down
4 changes: 2 additions & 2 deletions asdl/typed.sh
Expand Up @@ -34,7 +34,7 @@ typecheck() {
check-arith() {
local strict='--strict'
#strict=''
MYPYPATH=. PYTHONPATH=. typecheck $strict --no-strict-optional \
MYPYPATH=. PYTHONPATH=. typecheck $strict \
asdl/typed_arith_parse.py asdl/typed_arith_parse_test.py asdl/tdop.py
}

Expand All @@ -51,7 +51,7 @@ iter-arith() {

iter-demo() {
asdl/run.sh gen-typed-demo-asdl
typecheck --strict --no-strict-optional \
typecheck --strict \
_devbuild/gen/typed_demo_asdl.py asdl/typed_demo.py

PYTHONPATH=. asdl/typed_demo.py "$@"
Expand Down

0 comments on commit 61c9f56

Please sign in to comment.