Skip to content

Commit c07ce3e

Browse files
committed
Use ruff instead of flake8 and isort
1 parent 6398302 commit c07ce3e

File tree

8 files changed

+58
-67
lines changed

8 files changed

+58
-67
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,4 @@ jobs:
2828
- name: Launch tests
2929
run: python -m pytest
3030
- name: Check coding style
31-
run: python -m flake8 --exclude tests/css-parsing-tests
32-
- name: Check imports order
33-
run: python -m isort . --check --diff
31+
run: python -m ruff check

docs/contribute.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@ You can launch tests using the following command::
4545

4646
venv/bin/python -m pytest
4747

48-
tinycss2 also uses isort_ to check imports and flake8_ to check the coding
49-
style::
48+
tinycss2 also uses ruff_ to check the coding style::
5049

51-
venv/bin/python -m isort . --check --diff
52-
venv/bin/python -m flake8 --exclude tests/css-parsing-tests
50+
venv/bin/python -m ruff check
5351

5452
.. _pytest: https://docs.pytest.org/
55-
.. _isort: https://pycqa.github.io/isort/
56-
.. _flake8: https://flake8.pycqa.org/
53+
.. _ruff: https://docs.astral.sh/ruff/
5754

5855

5956
Documentation

pyproject.toml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Donation = 'https://opencollective.com/courtbouillon'
4040

4141
[project.optional-dependencies]
4242
doc = ['sphinx', 'sphinx_rtd_theme']
43-
test = ['pytest', 'isort', 'flake8']
43+
test = ['pytest', 'ruff']
4444

4545
[tool.flit.sdist]
4646
exclude = ['.*']
@@ -56,7 +56,9 @@ include = ['tests/*', 'tinycss2/*']
5656
exclude_lines = ['pragma: no cover', 'def __repr__', 'raise NotImplementedError']
5757
omit = ['.*']
5858

59-
[tool.isort]
60-
default_section = 'FIRSTPARTY'
61-
multi_line_output = 4
62-
extend_skip = ['tests/css-parsing-tests']
59+
[tool.ruff]
60+
extend-exclude = ['tests/css-parsing-tests']
61+
62+
[tool.ruff.lint]
63+
select = ['E', 'W', 'F', 'I', 'N', 'RUF']
64+
ignore = ['RUF001', 'RUF002', 'RUF003']

tests/test_tinycss2.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
from pathlib import Path
55

66
import pytest
7-
from tinycss2 import (
7+
from webencodings import Encoding, lookup
8+
9+
from tinycss2 import ( # isort:skip
810
parse_blocks_contents, parse_component_value_list, parse_declaration_list,
9-
parse_one_component_value, parse_one_declaration, parse_one_rule,
10-
parse_rule_list, parse_stylesheet, parse_stylesheet_bytes, serialize)
11-
from tinycss2.ast import (
12-
AtKeywordToken, AtRule, Comment, CurlyBracketsBlock, Declaration,
13-
DimensionToken, FunctionBlock, HashToken, IdentToken, LiteralToken,
14-
NumberToken, ParenthesesBlock, ParseError, PercentageToken, QualifiedRule,
15-
SquareBracketsBlock, StringToken, UnicodeRangeToken, URLToken,
16-
WhitespaceToken)
11+
parse_one_component_value, parse_one_declaration, parse_one_rule, parse_rule_list,
12+
parse_stylesheet, parse_stylesheet_bytes, serialize)
13+
from tinycss2.ast import ( # isort:skip
14+
AtKeywordToken, AtRule, Comment, CurlyBracketsBlock, Declaration, DimensionToken,
15+
FunctionBlock, HashToken, IdentToken, LiteralToken, NumberToken, ParenthesesBlock,
16+
ParseError, PercentageToken, QualifiedRule, SquareBracketsBlock, StringToken,
17+
UnicodeRangeToken, URLToken, WhitespaceToken)
1718
from tinycss2.color3 import RGBA, parse_color
1819
from tinycss2.nth import parse_nth
19-
from webencodings import Encoding, lookup
2020

2121

2222
def generic(func):
@@ -49,26 +49,25 @@ def numeric(t):
4949
LiteralToken: lambda t: t.value,
5050
IdentToken: lambda t: ['ident', t.value],
5151
AtKeywordToken: lambda t: ['at-keyword', t.value],
52-
HashToken: lambda t: ['hash', t.value,
53-
'id' if t.is_identifier else 'unrestricted'],
52+
HashToken: lambda t: [
53+
'hash', t.value, 'id' if t.is_identifier else 'unrestricted'],
5454
StringToken: lambda t: ['string', t.value],
5555
URLToken: lambda t: ['url', t.value],
56-
NumberToken: lambda t: ['number'] + numeric(t),
57-
PercentageToken: lambda t: ['percentage'] + numeric(t),
58-
DimensionToken: lambda t: ['dimension'] + numeric(t) + [t.unit],
56+
NumberToken: lambda t: ['number', *numeric(t)],
57+
PercentageToken: lambda t: ['percentage', *numeric(t)],
58+
DimensionToken: lambda t: ['dimension', *numeric(t), t.unit],
5959
UnicodeRangeToken: lambda t: ['unicode-range', t.start, t.end],
6060

61-
CurlyBracketsBlock: lambda t: ['{}'] + to_json(t.content),
62-
SquareBracketsBlock: lambda t: ['[]'] + to_json(t.content),
63-
ParenthesesBlock: lambda t: ['()'] + to_json(t.content),
64-
FunctionBlock: lambda t: ['function', t.name] + to_json(t.arguments),
65-
66-
Declaration: lambda d: ['declaration', d.name,
67-
to_json(d.value), d.important],
68-
AtRule: lambda r: ['at-rule', r.at_keyword, to_json(r.prelude),
69-
to_json(r.content)],
70-
QualifiedRule: lambda r: ['qualified rule', to_json(r.prelude),
71-
to_json(r.content)],
61+
CurlyBracketsBlock: lambda t: ['{}', *to_json(t.content)],
62+
SquareBracketsBlock: lambda t: ['[]', *to_json(t.content)],
63+
ParenthesesBlock: lambda t: ['()', *to_json(t.content)],
64+
FunctionBlock: lambda t: ['function', t.name, *to_json(t.arguments)],
65+
66+
Declaration: lambda d: ['declaration', d.name, to_json(d.value), d.important],
67+
AtRule: lambda r: [
68+
'at-rule', r.at_keyword, to_json(r.prelude), to_json(r.content)],
69+
QualifiedRule: lambda r: [
70+
'qualified rule', to_json(r.prelude), to_json(r.content)],
7271

7372
RGBA: lambda v: [round(c, 10) for c in v],
7473
}

tinycss2/nth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def parse_b(tokens, a):
8888
def parse_signless_b(tokens, a, b_sign):
8989
token = _next_significant(tokens)
9090
if (token.type == 'number' and token.is_integer and
91-
not token.representation[0] in '-+'):
91+
token.representation[0] not in '-+'):
9292
return parse_end(tokens, a, b_sign * token.int_value)
9393

9494

tinycss2/parser.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ def _parse_declaration(first_token, tokens, nested=True):
135135
if state == 'value' and token == '!':
136136
state = 'bang'
137137
bang_position = i
138-
elif state == 'bang' and token.type == 'ident' \
139-
and token.lower_value == 'important':
138+
elif (state == 'bang' and token.type == 'ident'
139+
and token.lower_value == 'important'):
140140
state = 'important'
141141
elif token.type not in ('whitespace', 'comment'):
142142
state = 'value'
@@ -161,8 +161,9 @@ def _parse_declaration(first_token, tokens, nested=True):
161161

162162
# TODO: Handle unicode-range
163163

164-
return Declaration(name.source_line, name.source_column, name.value,
165-
name.lower_value, value, state == 'important')
164+
return Declaration(
165+
name.source_line, name.source_column, name.value, name.lower_value,
166+
value, state == 'important')
166167

167168

168169
def _consume_blocks_content(first_token, tokens):
@@ -183,8 +184,7 @@ def _consume_blocks_content(first_token, tokens):
183184
return declaration
184185
else:
185186
tokens = chain(declaration_tokens, semicolon_token, tokens)
186-
return _consume_qualified_rule(
187-
first_token, tokens, stop_token=';', nested=True)
187+
return _consume_qualified_rule(first_token, tokens, stop_token=';', nested=True)
188188

189189

190190
def _consume_declaration_in_list(first_token, tokens):
@@ -478,8 +478,9 @@ def _consume_at_rule(at_keyword, tokens):
478478
elif token == ';':
479479
break
480480
prelude.append(token)
481-
return AtRule(at_keyword.source_line, at_keyword.source_column,
482-
at_keyword.value, at_keyword.lower_value, prelude, content)
481+
return AtRule(
482+
at_keyword.source_line, at_keyword.source_column, at_keyword.value,
483+
at_keyword.lower_value, prelude, content)
483484

484485

485486
def _rule_error(token, name):
@@ -523,5 +524,5 @@ def _consume_qualified_rule(first_token, tokens, nested=False,
523524
prelude.append(token)
524525
else:
525526
return _rule_error(prelude[-1], 'EOF')
526-
return QualifiedRule(first_token.source_line, first_token.source_column,
527-
prelude, block.content)
527+
return QualifiedRule(
528+
first_token.source_line, first_token.source_column, prelude, block.content)

tinycss2/serializer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ def _serialize_to(nodes, write):
121121

122122
BAD_PAIRS = set(
123123
[(a, b)
124-
for a in ('ident', 'at-keyword', 'hash', 'dimension', '#', '-',
125-
'number')
124+
for a in ('ident', 'at-keyword', 'hash', 'dimension', '#', '-', 'number')
126125
for b in ('ident', 'function', 'url', 'number', 'percentage',
127126
'dimension', 'unicode-range')] +
128127
[(a, b)

tinycss2/tokenizer.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
from webencodings import ascii_lower
55

6-
from .ast import (
6+
from .ast import ( # isort: skip
77
AtKeywordToken, Comment, CurlyBracketsBlock, DimensionToken, FunctionBlock,
8-
HashToken, IdentToken, LiteralToken, NumberToken, ParenthesesBlock,
9-
ParseError, PercentageToken, SquareBracketsBlock, StringToken,
10-
UnicodeRangeToken, URLToken, WhitespaceToken)
8+
HashToken, IdentToken, LiteralToken, NumberToken, ParenthesesBlock, ParseError,
9+
PercentageToken, SquareBracketsBlock, StringToken, UnicodeRangeToken, URLToken,
10+
WhitespaceToken)
1111
from .serializer import serialize_string_value, serialize_url
1212

1313
_NUMBER_RE = re.compile(r'[-+]?([0-9]*\.)?[0-9]+([eE][+-]?[0-9]+)?')
@@ -108,11 +108,9 @@ def parse_component_value_list(css, skip_comments=False):
108108
line, column, value, int_value, repr_, unit))
109109
elif css.startswith('%', pos):
110110
pos += 1
111-
tokens.append(PercentageToken(
112-
line, column, value, int_value, repr_))
111+
tokens.append(PercentageToken(line, column, value, int_value, repr_))
113112
else:
114-
tokens.append(NumberToken(
115-
line, column, value, int_value, repr_))
113+
tokens.append(NumberToken(line, column, value, int_value, repr_))
116114
elif c == '@':
117115
pos += 1
118116
if pos < length and _is_ident_start(css, pos):
@@ -175,12 +173,10 @@ def parse_component_value_list(css, skip_comments=False):
175173
pos = css.find('*/', pos + 2)
176174
if pos == -1:
177175
if not skip_comments:
178-
tokens.append(
179-
Comment(line, column, css[token_start_pos + 2:]))
176+
tokens.append(Comment(line, column, css[token_start_pos + 2:]))
180177
break
181178
if not skip_comments:
182-
tokens.append(
183-
Comment(line, column, css[token_start_pos + 2:pos]))
179+
tokens.append(Comment(line, column, css[token_start_pos + 2:pos]))
184180
pos += 2
185181
elif css.startswith('<!--', pos):
186182
tokens.append(LiteralToken(line, column, '<!--'))
@@ -219,8 +215,7 @@ def _is_ident_start(css, pos):
219215
pos += 1
220216
return (
221217
# Name-start code point or hyphen:
222-
(pos < len(css) and (
223-
_is_name_start(css, pos) or css[pos] == '-')) or
218+
(pos < len(css) and (_is_name_start(css, pos) or css[pos] == '-')) or
224219
# Valid escape:
225220
(css.startswith('\\', pos) and not css.startswith('\\\n', pos)))
226221
elif css[pos] == '\\':

0 commit comments

Comments
 (0)