Skip to content

Commit

Permalink
do
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftsayan committed Mar 10, 2024
1 parent 95159b4 commit 95d03c9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ repos:
"--ignore=E203,W503,E501,E704",
"--per-file-ignores=__init__.py:F401",
]
- repo: local
hooks:
- id: cyan
name: cyan
entry: poetry run cyan
language: python
files: \.(py)$
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Cyan
8 changes: 4 additions & 4 deletions cyan/errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import ast


class CyanError(Exception):
def __init__(self, message):
print(f"CyanError: {message}", file=sys.stderr)
super().__init__(message)
def __init__(self, node: ast.AST, message: str):
self.node = node
self.message = message
12 changes: 9 additions & 3 deletions cyan/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ class AssertVisitor(ast.NodeVisitor):
def visit_Assert(self, node: ast.Assert):
if isinstance(node.test, ast.BoolOp) and isinstance(node.test.op, ast.And):
raise CyanError(
f"Assert with 'and' found at line {node.lineno}: Use multiple asserts instead."
node=node,
message="Assert with 'and' found. Use multiple asserts instead.",
)
if node.msg is None:
raise CyanError(
f"Assert without a message found at line {node.lineno}: Always include a message."
node=node,
message="Assert without message found.",
)


def check_file(filename: str):
with open(filename, "r") as file:
tree = ast.parse(file.read(), filename=filename)
visitor = AssertVisitor()
visitor.visit(tree)
try:
visitor.visit(tree)
except CyanError as e:
print(f"{filename}:{e.node.lineno}:{e.node.col_offset}: {e.message}")
sys.exit(1)


def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] = None):
Expand Down
Empty file added pg.py
Empty file.

0 comments on commit 95d03c9

Please sign in to comment.