mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-10 05:14:18 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -14,6 +14,7 @@ The semantics are:
|
|||
- ident evaluates to True of False according to a provided matcher function.
|
||||
- or/and/not evaluate according to the usual boolean semantics.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
import dataclasses
|
||||
|
|
@ -29,24 +30,24 @@ from typing import Sequence
|
|||
|
||||
|
||||
__all__ = [
|
||||
"Expression",
|
||||
"ParseError",
|
||||
'Expression',
|
||||
'ParseError',
|
||||
]
|
||||
|
||||
|
||||
class TokenType(enum.Enum):
|
||||
LPAREN = "left parenthesis"
|
||||
RPAREN = "right parenthesis"
|
||||
OR = "or"
|
||||
AND = "and"
|
||||
NOT = "not"
|
||||
IDENT = "identifier"
|
||||
EOF = "end of input"
|
||||
LPAREN = 'left parenthesis'
|
||||
RPAREN = 'right parenthesis'
|
||||
OR = 'or'
|
||||
AND = 'and'
|
||||
NOT = 'not'
|
||||
IDENT = 'identifier'
|
||||
EOF = 'end of input'
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Token:
|
||||
__slots__ = ("type", "value", "pos")
|
||||
__slots__ = ('type', 'value', 'pos')
|
||||
type: TokenType
|
||||
value: str
|
||||
pos: int
|
||||
|
|
@ -64,11 +65,11 @@ class ParseError(Exception):
|
|||
self.message = message
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"at column {self.column}: {self.message}"
|
||||
return f'at column {self.column}: {self.message}'
|
||||
|
||||
|
||||
class Scanner:
|
||||
__slots__ = ("tokens", "current")
|
||||
__slots__ = ('tokens', 'current')
|
||||
|
||||
def __init__(self, input: str) -> None:
|
||||
self.tokens = self.lex(input)
|
||||
|
|
@ -77,23 +78,23 @@ class Scanner:
|
|||
def lex(self, input: str) -> Iterator[Token]:
|
||||
pos = 0
|
||||
while pos < len(input):
|
||||
if input[pos] in (" ", "\t"):
|
||||
if input[pos] in (' ', '\t'):
|
||||
pos += 1
|
||||
elif input[pos] == "(":
|
||||
yield Token(TokenType.LPAREN, "(", pos)
|
||||
elif input[pos] == '(':
|
||||
yield Token(TokenType.LPAREN, '(', pos)
|
||||
pos += 1
|
||||
elif input[pos] == ")":
|
||||
yield Token(TokenType.RPAREN, ")", pos)
|
||||
elif input[pos] == ')':
|
||||
yield Token(TokenType.RPAREN, ')', pos)
|
||||
pos += 1
|
||||
else:
|
||||
match = re.match(r"(:?\w|:|\+|-|\.|\[|\]|\\|/)+", input[pos:])
|
||||
match = re.match(r'(:?\w|:|\+|-|\.|\[|\]|\\|/)+', input[pos:])
|
||||
if match:
|
||||
value = match.group(0)
|
||||
if value == "or":
|
||||
if value == 'or':
|
||||
yield Token(TokenType.OR, value, pos)
|
||||
elif value == "and":
|
||||
elif value == 'and':
|
||||
yield Token(TokenType.AND, value, pos)
|
||||
elif value == "not":
|
||||
elif value == 'not':
|
||||
yield Token(TokenType.NOT, value, pos)
|
||||
else:
|
||||
yield Token(TokenType.IDENT, value, pos)
|
||||
|
|
@ -103,9 +104,9 @@ class Scanner:
|
|||
pos + 1,
|
||||
f'unexpected character "{input[pos]}"',
|
||||
)
|
||||
yield Token(TokenType.EOF, "", pos)
|
||||
yield Token(TokenType.EOF, '', pos)
|
||||
|
||||
def accept(self, type: TokenType, *, reject: bool = False) -> Optional[Token]:
|
||||
def accept(self, type: TokenType, *, reject: bool = False) -> Token | None:
|
||||
if self.current.type is type:
|
||||
token = self.current
|
||||
if token.type is not TokenType.EOF:
|
||||
|
|
@ -118,8 +119,8 @@ class Scanner:
|
|||
def reject(self, expected: Sequence[TokenType]) -> NoReturn:
|
||||
raise ParseError(
|
||||
self.current.pos + 1,
|
||||
"expected {}; got {}".format(
|
||||
" OR ".join(type.value for type in expected),
|
||||
'expected {}; got {}'.format(
|
||||
' OR '.join(type.value for type in expected),
|
||||
self.current.type.value,
|
||||
),
|
||||
)
|
||||
|
|
@ -128,7 +129,7 @@ class Scanner:
|
|||
# True, False and None are legal match expression identifiers,
|
||||
# but illegal as Python identifiers. To fix this, this prefix
|
||||
# is added to identifiers in the conversion to Python AST.
|
||||
IDENT_PREFIX = "$"
|
||||
IDENT_PREFIX = '$'
|
||||
|
||||
|
||||
def expression(s: Scanner) -> ast.Expression:
|
||||
|
|
@ -176,7 +177,7 @@ class MatcherAdapter(Mapping[str, bool]):
|
|||
self.matcher = matcher
|
||||
|
||||
def __getitem__(self, key: str) -> bool:
|
||||
return self.matcher(key[len(IDENT_PREFIX) :])
|
||||
return self.matcher(key[len(IDENT_PREFIX):])
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
raise NotImplementedError()
|
||||
|
|
@ -191,13 +192,13 @@ class Expression:
|
|||
The expression can be evaluated against different matchers.
|
||||
"""
|
||||
|
||||
__slots__ = ("code",)
|
||||
__slots__ = ('code',)
|
||||
|
||||
def __init__(self, code: types.CodeType) -> None:
|
||||
self.code = code
|
||||
|
||||
@classmethod
|
||||
def compile(self, input: str) -> "Expression":
|
||||
def compile(self, input: str) -> Expression:
|
||||
"""Compile a match expression.
|
||||
|
||||
:param input: The input expression - one line.
|
||||
|
|
@ -205,8 +206,8 @@ class Expression:
|
|||
astexpr = expression(Scanner(input))
|
||||
code: types.CodeType = compile(
|
||||
astexpr,
|
||||
filename="<pytest match expression>",
|
||||
mode="eval",
|
||||
filename='<pytest match expression>',
|
||||
mode='eval',
|
||||
)
|
||||
return Expression(code)
|
||||
|
||||
|
|
@ -219,5 +220,5 @@ class Expression:
|
|||
|
||||
:returns: Whether the expression matches or not.
|
||||
"""
|
||||
ret: bool = eval(self.code, {"__builtins__": {}}, MatcherAdapter(matcher))
|
||||
ret: bool = eval(self.code, {'__builtins__': {}}, MatcherAdapter(matcher))
|
||||
return ret
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue