Skip to content

Commit

Permalink
Use ruff instead of flake8
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <[email protected]>
  • Loading branch information
gaborbernat committed Jun 16, 2023
1 parent 2fe8ee2 commit b7b8d4e
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 222 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ concurrency:
jobs:
test:
name: test ${{ matrix.tox_env }}
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:

check:
name: check ${{ matrix.tox_env }}
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ coverage: ## Run the test suite, with Python code coverage

.PHONY: format
format: ## Autoformat all files
$(PYTHON) -m isort $(python_files)
$(PYTHON) -m black $(python_files)
$(PYTHON) -m ruff --fix $(python_files)

.PHONY: lint
lint: ## Lint all files
$(PYTHON) -m isort --check $(python_files)
$(PYTHON) -m flake8 $(python_files)
$(PYTHON) -m black --check --diff $(python_files)
$(PYTHON) -m ruff check $(python_files)
$(PYTHON) -m mypy src/pytest_memray --ignore-missing-imports

.PHONY: docs
Expand Down
15 changes: 9 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Sphinx configuration file for pytest-memray documentation."""
"""Sphinx configuration file for pytest-memray documentation.""" # noqa: INP001
from __future__ import annotations

import sys
from pathlib import Path
from subprocess import check_output
from typing import TYPE_CHECKING

from sphinx.application import Sphinx
from sphinxcontrib.programoutput import Command

if TYPE_CHECKING:
from sphinx.application import Sphinx

extensions = [
"sphinx.ext.extlinks",
"sphinx.ext.githubpages",
Expand All @@ -33,11 +36,11 @@
prev = Command.get_output
here = Path(__file__).parent
linkcheck_allowed_redirects = {
"https://github.com/bloomberg/pytest-memray/issues/.*": "https://github.com/bloomberg/pytest-memray/pull/.*"
"https://github.com/bloomberg/pytest-memray/issues/.*": "https://github.com/bloomberg/pytest-memray/pull/.*",
}


def _get_output(self):
def _get_output(self: Command) -> tuple[int, str]:
code, out = prev(self)
out = out.replace(str(Path(sys.executable).parents[1]), "/v")
out = out.replace(str(here), "/w")
Expand All @@ -47,11 +50,11 @@ def _get_output(self):
Command.get_output = _get_output


def setup(app: Sphinx) -> None:
def setup(app: Sphinx) -> None: # noqa: ARG001
here = Path(__file__).parent
root, exe = here.parent, Path(sys.executable)
towncrier = exe.with_name(f"towncrier{exe.suffix}")
cmd = [str(towncrier), "build", "--draft", "--version", "NEXT"]
new = check_output(cmd, cwd=root, text=True)
new = check_output(cmd, cwd=root, text=True) # noqa: S603
to = root / "docs" / "_draft.rst"
to.write_text("" if "No significant changes" in new else new)
10 changes: 5 additions & 5 deletions docs/demo/test_ok.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from __future__ import annotations
from __future__ import annotations # noqa: INP001

import pytest


def test_track():
def test_track() -> None:
from heapq import heappush

h = []
for value in range(1):
heappush(h, value)
assert [1] * 5_000
assert [1] * 5_000 # noqa: S101


@pytest.mark.limit_memory("100 KB")
def test_memory_exceed():
def test_memory_exceed() -> None:
found = [[i] * 1_000 for i in range(15)]
assert found
assert found # noqa: S101
105 changes: 62 additions & 43 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
[build-system]
build-backend = "hatchling.build"
requires = ["hatchling>=1.12.2", "hatch-vcs>=0.3"]
requires = [
"hatch-vcs>=0.3",
"hatchling>=1.18",
]

[project]
name = "pytest-memray"
description = "A simple plugin to use with pytest"
readme.file = "README.md"
readme.content-type = "text/markdown"
readme.file = "README.md"
license = "apache-2.0"
urls."Bug Tracker" = "https://github.com/bloomberg/pytest-memray/issues"
urls.Documentation = "https://pytest-memray.readthedocs.io"
urls."Source Code" = "https://github.com/bloomberg/pytest-memray"
authors = [
maintainers = [
{ name = "Pablo Galindo Salgado", email = "[email protected]" },
]
maintainers = [
authors = [
{ name = "Pablo Galindo Salgado", email = "[email protected]" },
]
requires-python = ">=3.8"
dependencies = [
"pytest>=7.2",
"memray>=1.5",
]
optional-dependencies.docs = [
"furo>=2022.12.7",
"sphinx>=6.1.3",
"sphinx-argparse>=0.4",
"sphinx-inline-tabs>=2022.1.2b11",
"sphinxcontrib-programoutput>=0.17",
"towncrier>=22.12",
]
optional-dependencies.lint = [
"black==22.12",
"flake8==6",
"isort==5.11.4",
"mypy==0.991",
]
optional-dependencies.test = [
"covdefaults>=2.2.2",
"pytest>=7.2",
"coverage>=7.0.5",
"flaky>=3.7",
"pytest-xdist>=3.1",
]
dynamic = ["version"]
classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Software Development :: Debuggers",
]

dynamic = [
"version",
]
dependencies = [
"memray>=1.8",
"pytest>=7.3.2",
]
optional-dependencies.docs = [
"furo>=2023.5.20",
"sphinx>=7.0.1",
"sphinx-argparse>=0.4",
"sphinx-inline-tabs>=2023.4.21",
"sphinxcontrib-programoutput>=0.17",
"towncrier>=23.6",
]
optional-dependencies.lint = [
"black==23.3",
"mypy==1.3",
"ruff==0.0.272",
]
optional-dependencies.test = [
"covdefaults>=2.3",
"coverage>=7.2.7",
"flaky>=3.7",
"pytest>=7.3.2",
"pytest-xdist>=3.3.1",
]
urls."Bug Tracker" = "https://github.com/bloomberg/pytest-memray/issues"
urls.Documentation = "https://pytest-memray.readthedocs.io"
urls."Source Code" = "https://github.com/bloomberg/pytest-memray"
[project.entry-points.pytest11]
memray = "pytest_memray.plugin"

Expand Down Expand Up @@ -91,15 +95,6 @@ python_version = "3.8"
show_error_codes = true
strict = true

[tool.isort]
force_single_line = true
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
line_length = 88
known_first_party = ["pytest_memray"]

[tool.towncrier]
name = "pytest-memray"
filename = "docs/news.rst"
Expand All @@ -114,3 +109,27 @@ type = [
{ name = "Improved Documentation", directory = "doc", showcontent = true },
{ name = "Miscellaneous", directory = "misc", showcontent = true },
]

[tool.ruff]
select = ["ALL"]
line-length = 88
target-version = "py38"
isort = {known-first-party = ["pytest_memray"], required-imports = ["from __future__ import annotations"]}
ignore = [
"ANN401", # dynamically typed expression
"D", # documentation not always applied
"ANN101", # self type annotation
"D203", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible
"D212", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
"S104", # Possible binding to all interface
]
[tool.ruff.per-file-ignores]
"tests/**/*.py" = [
"S101", # asserts allowed in tests...
"FBT", # don"t care about booleans as positional arguments in tests
"INP001", # no implicit namespace
"D", # don"t care about documentation in tests
"S603", # `subprocess` call: check for execution of untrusted input
"PLR2004", # Magic value used in comparison, consider replacing with a constant variable
]
"src/pytest_memray/_version.py" = ["I002"] # Missing required import: `from __future__ import annotations
2 changes: 1 addition & 1 deletion src/pytest_memray/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from ._version import __version__ as __version__
from ._version import __version__

__all__ = [
"__version__",
Expand Down
23 changes: 14 additions & 9 deletions src/pytest_memray/marks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Tuple
from typing import cast
from typing import TYPE_CHECKING, Tuple, cast

from memray import AllocationRecord
from pytest import Config
from .utils import parse_memory_string, sizeof_fmt, value_or_ini

from .utils import parse_memory_string
from .utils import sizeof_fmt
from .utils import value_or_ini
if TYPE_CHECKING:
import pytest
from memray import AllocationRecord

PytestSection = Tuple[str, str]

Expand Down Expand Up @@ -62,7 +60,10 @@ def long_repr(self) -> str:


def limit_memory(
limit: str, *, _allocations: list[AllocationRecord], _config: Config
limit: str,
*,
_allocations: list[AllocationRecord],
_config: pytest.Config,
) -> _MemoryInfo | None:
"""Limit memory used by the test."""
max_memory = parse_memory_string(limit)
Expand All @@ -72,7 +73,11 @@ def limit_memory(
num_stacks: int = cast(int, value_or_ini(_config, "stacks"))
native_stacks: bool = cast(bool, value_or_ini(_config, "native"))
return _MemoryInfo(
max_memory, total_allocated_memory, _allocations, num_stacks, native_stacks
max_memory,
total_allocated_memory,
_allocations,
num_stacks,
native_stacks,
)


Expand Down
Loading

0 comments on commit b7b8d4e

Please sign in to comment.