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

Move to ruff-formatter & update settings #778

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 7 additions & 9 deletions .github/workflows/validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ jobs:
uses: actions/cache@v4
with:
path: ${{ env.PRE_COMMIT_HOME }}
key: "precommit-${{ runner.os }}-${{ steps.poetry_setup.outputs.python-version }}-\
${{ hashFiles('./.pre-commit-config.yaml') }}"
key:
"precommit-${{ runner.os }}-${{ steps.poetry_setup.outputs.python-version }}-\
${{ hashFiles('./.pre-commit-config.yaml') }}"
# Restore keys allows us to perform a cache restore even if the full cache key wasn't matched.
# That way we still end up saving new cache, but we can still make use of the cache from previous
# version.
Expand All @@ -41,18 +42,15 @@ jobs:
- name: Run pre-commit hooks
run: SKIP=black,isort,ruff,pyright pre-commit run --all-files

- name: Run black formatter check
run: black --check --diff .
- name: Run ruff linter
run: ruff check --output-format=full --show-fixes --exit-non-zero-on-fix .

- name: Run isort import formatter check
run: isort --check .
- name: Run ruff formatter
run: ruff format --diff .

- name: Run pyright type checker
run: pyright .

- name: Run ruff linter
run: ruff check --show-source .

# Steps below are here to generate and upload an artifact from
# this workflow so that we can have the data about author and some
# other things accessible from the status_embed workflow.
Expand Down
19 changes: 6 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,10 @@ repos:
hooks:
- id: python-check-blanket-noqa # Enforce noqa annotations (noqa: F401,W203)
- id: python-use-type-annotations # Enforce type annotations instead of type comments
- repo: local
hooks:
- id: black
name: Black
description: Auto-format the code with black
entry: poetry run black
language: system
types: [python]
- repo: local
hooks:
- id: ruff
name: Ruff
name: Ruff Linter
description: Run ruff checks on the code
entry: poetry run ruff check --force-exclude
language: system
Expand All @@ -44,12 +36,13 @@ repos:
args: [--fix, --exit-non-zero-on-fix]
- repo: local
hooks:
- id: isort
name: ISort
description: Sort imports with isort
entry: poetry run isort
- id: ruff-ruff
name: Ruff Formatter
description: Ruf ruff auto-formatter
entry: poetry run ruff format
language: system
types: [python]
require_serial: true
- repo: local
hooks:
- id: pyright
Expand Down
98 changes: 1 addition & 97 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 32 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ classifiers = [
]
packages = [
{ include = "mcstatus" },
{ include = "protocol", from = "mcstatus" }
{ include = "protocol", from = "mcstatus" },
]

[tool.poetry.dependencies]
Expand All @@ -48,9 +48,7 @@ coverage = "^7.3.0"

[tool.poetry.group.lint.dependencies]
ruff = "0.3.4"
black = ">=23.7,<25.0"
coverage = "^7.3.0"
isort = "^5.12.0"
pyright = "^1.1.322"
typing-extensions = "^4.7.1"

Expand Down Expand Up @@ -97,6 +95,7 @@ style = "pep440"
target-version = "py38"
line-length = 127

[tool.ruff.lint]
select = [
"F", # Pyflakes
"W", # Pycodestyle (warnigns)
Expand All @@ -118,28 +117,48 @@ ignore = [
"ANN204", # Missing return type annotation for special method
"B904", # Exception raised within try-except should use raise ... from exc
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`

# Redundant rules with ruff-format:
"E111", # Indentation of a non-multiple of 4 spaces
"E114", # Comment with indentation of a non-multiple of 4 spaces
"E117", # Cheks for over-indented code
"D206", # Checks for docstrings indented with tabs
"D300", # Checks for docstring that use ''' instead of """
"Q000", # Checks of inline strings that use wrong quotes (' instead of ")
"Q001", # Multiline string that use wrong quotes (''' instead of """)
"Q002", # Checks for docstrings that use wrong quotes (''' instead of """)
"Q003", # Checks for avoidable escaped quotes ("\"" -> '"')
"COM812", # Missing trailing comma (in multi-line lists/tuples/...)
"COM819", # Prohibited trailing comma (in single-line lists/tuples/...)
"ISC001", # Single line implicit string concatenation ("hi" "hey" -> "hihey")
"ISC002", # Multi line implicit string concatenation
]

[tool.ruff.extend-per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"test_*.py" = [
"ANN", # flake8-annotations
]
"docs/examples/code/**" = [
"FA", # flake8-future-annotations
]

[tool.ruff.flake8-tidy-imports]
[tool.ruff.lint.flake8-tidy-imports]
ban-relative-imports = "all"

[tool.black]
line-length = 127
[tool.ruff.lint.isort]
order-by-type = false
case-sensitive = true
combine-as-imports = true

# Redundant rules with ruff-format
force-single-line = false # forces all imports to appear on their own line
force-wrap-aliases = false # Split imports with multiple members and at least one alias
lines-after-imports = -1 # The number of blank lines to place after imports
lines-between-types = 0 # Number of lines to place between "direct" and import from imports
split-on-trailing-comma = false # if last member of multiline import has a comma, don't fold it to single line

[tool.isort]
profile = "black"
line_length = 127
order_by_type = false
case_sensitive = true
skip = [".venv", ".git", ".cache", ".tox"]
[tool.ruff-formatter]
line-ending = "lf"

[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning"]
Expand Down
8 changes: 2 additions & 6 deletions tests/motd/test_motd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class TestMotdParse:
def test_correct_result(self, source):
assert Motd.parse(source) == Motd(
[
# fmt: off
"top", Formatting.RESET,
"1", Formatting.RESET,
WebColor.from_hex(hex="#b3eeff"), "2", Formatting.RESET,
Expand All @@ -35,10 +34,9 @@ def test_correct_result(self, source):
MinecraftColor.MINECOIN_GOLD, "20", Formatting.RESET,
Formatting.RESET, "21", Formatting.RESET,
TranslationTag("some.random.string"), Formatting.RESET,
# fmt: on
],
raw=source,
)
) # fmt: skip

@pytest.mark.parametrize("bedrock", (True, False))
def test_bedrock_parameter_nothing_changes(self, bedrock: bool):
Expand Down Expand Up @@ -167,7 +165,6 @@ def test_multiple_times_nested_extras(self):
}
)
assert motd.parsed == [
# fmt: off
Formatting.RESET, Formatting.RESET, Formatting.RESET,
"1",
Formatting.RESET, Formatting.RESET,
Expand All @@ -187,8 +184,7 @@ def test_multiple_times_nested_extras(self):
Formatting.RESET, Formatting.RESET,
"9",
Formatting.RESET,
# fmt: on
]
] # fmt: skip

def test_raw_attribute(self, source):
motd = Motd.parse(source)
Expand Down
38 changes: 22 additions & 16 deletions tests/status_response/test_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ class TestJavaStatusResponse(BaseStatusResponseTest):
("raw", RAW),
("forge_data", None),
]
OPTIONAL_FIELDS = [("favicon", "icon"), ("enforcesSecureChat", "enforces_secure_chat")], {
"players": {"max": 20, "online": 0},
"version": {"name": "1.8-pre1", "protocol": 44},
"description": "A Minecraft Server",
"enforcesSecureChat": True,
"favicon": "data:image/png;base64,foo",
}
OPTIONAL_FIELDS = (
[("favicon", "icon"), ("enforcesSecureChat", "enforces_secure_chat")],
{
"players": {"max": 20, "online": 0},
"version": {"name": "1.8-pre1", "protocol": 44},
"description": "A Minecraft Server",
"enforcesSecureChat": True,
"favicon": "data:image/png;base64,foo",
},
)

@pytest.fixture(scope="class")
def build(self) -> JavaStatusResponse:
Expand All @@ -52,15 +55,18 @@ class TestJavaStatusPlayers(BaseStatusResponseTest):
],
),
]
OPTIONAL_FIELDS = [("sample", "sample")], {
"max": 20,
"online": 0,
"sample": [
{"name": "foo", "id": "0b3717c4-f45d-47c8-b8e2-3d9ff6f93a89"},
{"name": "bar", "id": "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"},
{"name": "baz", "id": "40e8d003-8872-412d-b09a-4431a5afcbd4"},
],
}
OPTIONAL_FIELDS = (
[("sample", "sample")],
{
"max": 20,
"online": 0,
"sample": [
{"name": "foo", "id": "0b3717c4-f45d-47c8-b8e2-3d9ff6f93a89"},
{"name": "bar", "id": "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"},
{"name": "baz", "id": "40e8d003-8872-412d-b09a-4431a5afcbd4"},
],
},
)

@pytest.fixture(scope="class")
def build(self) -> JavaStatusPlayers:
Expand Down
Loading