Skip to content

Commit

Permalink
chore: slightly more compact (#5)
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Sep 7, 2022
1 parent 95cffeb commit a9f47f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 15 additions & 10 deletions src/flake8_errmsg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
import dataclasses
import sys
import traceback
from collections.abc import Iterator
from pathlib import Path
from typing import Iterator, NamedTuple
from typing import NamedTuple

__all__ = ("__version__", "run_on_file", "main", "ErrMsgASTPlugin")

__version__ = "0.2.3"
__version__ = "0.2.4"


class Flake8ASTErrorInfo(NamedTuple):
Expand All @@ -34,19 +35,23 @@ def __init__(self) -> None:
def visit_Raise(self, node: ast.Raise) -> None:
match node.exc:
case ast.Call(args=[ast.Constant(value=str()), *_]):
msg = "EM101 exception must not use a string literal, assign to variable first"
self.errors.append(
Flake8ASTErrorInfo(node.lineno, node.col_offset, msg, type(self))
)
self.errors.append(EM101(node))
case ast.Call(args=[ast.JoinedStr(), *_]):
msg = "EM102 exception must not use a f-string literal, assign to variable first"
self.errors.append(
Flake8ASTErrorInfo(node.lineno, node.col_offset, msg, type(self))
)
self.errors.append(EM102(node))
case _:
pass


def EM101(node: ast.AST) -> Flake8ASTErrorInfo:
msg = "EM101 Exception must not use a string literal, assign to variable first"
return Flake8ASTErrorInfo(node.lineno, node.col_offset, msg, Visitor)


def EM102(node: ast.AST) -> Flake8ASTErrorInfo:
msg = "EM102 Exception must not use an f-string literal, assign to variable first"
return Flake8ASTErrorInfo(node.lineno, node.col_offset, msg, Visitor)


@dataclasses.dataclass
class ErrMsgASTPlugin:
tree: ast.AST
Expand Down
4 changes: 2 additions & 2 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def test_err1():

assert (
results[0].msg
== "EM101 exception must not use a string literal, assign to variable first"
== "EM101 Exception must not use a string literal, assign to variable first"
)
assert (
results[1].msg
== "EM102 exception must not use a f-string literal, assign to variable first"
== "EM102 Exception must not use an f-string literal, assign to variable first"
)

0 comments on commit a9f47f6

Please sign in to comment.