Skip to content

Commit

Permalink
Merge pull request #256 from thatch/issue249
Browse files Browse the repository at this point in the history
Better validate `except` without a space after it.
  • Loading branch information
thatch authored Mar 5, 2020
2 parents 196a4f6 + d9f0666 commit b31cf0c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
13 changes: 9 additions & 4 deletions libcst/_nodes/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,15 @@ def _validate(self) -> None:
raise CSTValidationError(
"Must use a Name node for AsName name inside ExceptHandler."
)
if self.type is not None and self.whitespace_after_except.empty:
raise CSTValidationError(
"Must have at least one space after except when ExceptHandler has a type."
)
type_ = self.type
if type_ is not None and self.whitespace_after_except.empty:
# Space is only required when the first char in `type` could start
# an identifier. In the most common cases, we want to allow
# grouping or tuple parens.
if isinstance(type_, Name) and not type_.lpar:
raise CSTValidationError(
"Must have at least one space after except when ExceptHandler has a type."
)

def _visit_and_replace_children(self, visitor: CSTVisitorT) -> "ExceptHandler":
return ExceptHandler(
Expand Down
44 changes: 44 additions & 0 deletions libcst/_nodes/tests/test_try.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,50 @@ class TryTest(CSTNodeTest):
+ " pass\n",
"parser": None,
},
# No space when using grouping parens
{
"node": cst.Try(
cst.SimpleStatementSuite((cst.Pass(),)),
handlers=(
cst.ExceptHandler(
cst.SimpleStatementSuite((cst.Pass(),)),
whitespace_after_except=cst.SimpleWhitespace(""),
type=cst.Name(
"Exception",
lpar=(cst.LeftParen(),),
rpar=(cst.RightParen(),),
),
),
),
),
"code": "try: pass\nexcept(Exception): pass\n",
"parser": parse_statement,
},
# No space when using tuple
{
"node": cst.Try(
cst.SimpleStatementSuite((cst.Pass(),)),
handlers=(
cst.ExceptHandler(
cst.SimpleStatementSuite((cst.Pass(),)),
whitespace_after_except=cst.SimpleWhitespace(""),
type=cst.Tuple(
[
cst.Element(
cst.Name("IOError"),
comma=cst.Comma(
whitespace_after=cst.SimpleWhitespace(" ")
),
),
cst.Element(cst.Name("ImportError")),
]
),
),
),
),
"code": "try: pass\nexcept(IOError, ImportError): pass\n",
"parser": parse_statement,
},
)
)
def test_valid(self, **kwargs: Any) -> None:
Expand Down
4 changes: 3 additions & 1 deletion libcst/_parser/conversions/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@
),
version="<=3.5",
)
def convert_argslist(config: ParserConfig, children: Sequence[Any]) -> Any:
def convert_argslist( # noqa: C901
config: ParserConfig, children: Sequence[Any]
) -> Any:
posonly_params: List[Param] = []
posonly_ind: Union[ParamSlash, MaybeSentinel] = MaybeSentinel.DEFAULT
params: List[Param] = []
Expand Down

0 comments on commit b31cf0c

Please sign in to comment.