Skip to content
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

fix: improve validation for ImportAlias and Try statements #340

Merged
merged 4 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 12 additions & 1 deletion libcst/_nodes/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,11 @@ def _validate(self) -> None:
if len(self.handlers) == 0 and self.orelse is not None:
raise CSTValidationError(
"A Try statement must have at least one ExceptHandler in order "
+ "to have an Else"
+ "to have an Else."
)
# Check bare excepts are always at the last position
if any(handler.type is None for handler in self.handlers[:-1]):
raise CSTValidationError("The bare except: handler must be the last one.")

def _visit_and_replace_children(self, visitor: CSTVisitorT) -> "Try":
return Try(
Expand Down Expand Up @@ -972,6 +975,14 @@ def _validate(self) -> None:
raise CSTValidationError(
"Must use a Name node for AsName name inside ImportAlias."
)
try:
self.evaluated_name
except Exception as e:
if str(e) == "Logic error!":
raise CSTValidationError(
"The imported name must be a valid qualified name."
)
raise e

def _visit_and_replace_children(self, visitor: CSTVisitorT) -> "ImportAlias":
return ImportAlias(
Expand Down
12 changes: 12 additions & 0 deletions libcst/_nodes/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ def test_valid(self, **kwargs: Any) -> None:
),
"expected_re": "at least one space",
},
{
"get_node": lambda: cst.Import(
names=[
cst.ImportAlias(
name=cst.Attribute(
value=cst.Float(value="0."), attr=cst.Name(value="A")
)
)
]
),
"expected_re": "imported name must be a valid qualified name",
jimmylai marked this conversation as resolved.
Show resolved Hide resolved
},
)
)
def test_invalid(self, **kwargs: Any) -> None:
Expand Down
29 changes: 29 additions & 0 deletions libcst/_nodes/tests/test_try.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,35 @@ def test_valid(self, **kwargs: Any) -> None:
),
"expected_re": "at least one ExceptHandler in order to have an Else",
},
{
"get_node": lambda: cst.Try(
body=cst.SimpleStatementSuite(body=[cst.Pass()]),
handlers=(
cst.ExceptHandler(
body=cst.SimpleStatementSuite(body=[cst.Pass()]),
),
cst.ExceptHandler(
body=cst.SimpleStatementSuite(body=[cst.Pass()]),
),
),
),
"expected_re": "The bare except: handler must be the last one.",
},
{
"get_node": lambda: cst.Try(
body=cst.SimpleStatementSuite(body=[cst.Pass()]),
handlers=(
cst.ExceptHandler(
body=cst.SimpleStatementSuite(body=[cst.Pass()]),
),
cst.ExceptHandler(
body=cst.SimpleStatementSuite(body=[cst.Pass()]),
type=cst.Name("Exception"),
),
),
),
"expected_re": "The bare except: handler must be the last one.",
},
)
)
def test_invalid(self, **kwargs: Any) -> None:
Expand Down