Skip to content

Commit

Permalink
Use ruff for both linting and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Mar 3, 2024
1 parent 361083e commit 65a1055
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 68 deletions.
23 changes: 3 additions & 20 deletions .pre-commit-config.yaml
Expand Up @@ -7,28 +7,11 @@ repos:
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 24.1.1
hooks:
- id: black
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
args: [--skip-errors, --skip-string-normalization]
- repo: https://github.com/timothycrosley/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.14
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.0
hooks:
- id: ruff
- repo: https://github.com/yunojuno/pre-commit-xenon
rev: v0.1
hooks:
- id: xenon
args: [--max-average=A, --max-modules=C, --max-absolute=C]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
Expand Down
54 changes: 27 additions & 27 deletions docs/conf.py
@@ -1,31 +1,31 @@
"""Config file for Sphinx documentation"""

# General information about the project.
exclude_patterns = ["_build"]
master_doc = "index"
needs_sphinx = "5.0"
project = "requests-ratelimiter"
source_suffix = [".rst", ".md"]
templates_path = ["_templates"]
exclude_patterns = ['_build']
master_doc = 'index'
needs_sphinx = '5.0'
project = 'requests-ratelimiter'
source_suffix = ['.rst', '.md']
templates_path = ['_templates']

# Sphinx extensions
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.intersphinx",
"sphinx.ext.napoleon",
"sphinx_autodoc_typehints",
"sphinx_copybutton",
"myst_parser",
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
'sphinx.ext.intersphinx',
'sphinx.ext.napoleon',
'sphinx_autodoc_typehints',
'sphinx_copybutton',
'myst_parser',
]
myst_enable_extensions = ["colon_fence"]
myst_enable_extensions = ['colon_fence']

# Enable automatic links to other projects' Sphinx docs
intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
"requests": ("https://requests.readthedocs.io/en/latest/", None),
"urllib3": ("https://urllib3.readthedocs.io/en/stable/", None),
"pyrate_limiter": ("https://pyratelimiter.readthedocs.io/en/latest/", None),
'python': ('https://docs.python.org/3', None),
'requests': ('https://requests.readthedocs.io/en/latest/', None),
'urllib3': ('https://urllib3.readthedocs.io/en/stable/', None),
'pyrate_limiter': ('https://pyratelimiter.readthedocs.io/en/latest/', None),
}

# napoleon settings
Expand All @@ -34,22 +34,22 @@
numpydoc_show_class_members = False

# copybutton settings: Strip prompt text when copying code blocks
copybutton_prompt_text = r">>> |\.\.\. |\$ "
copybutton_prompt_text = r'>>> |\.\.\. |\$ '
copybutton_prompt_is_regexp = True

# Disable autodoc's built-in type hints, and use sphinx_autodoc_typehints extension instead
autodoc_typehints = "none"
autodoc_typehints = 'none'

# HTML general settings
html_show_sphinx = False
html_static_path = ["_static"]
pygments_style = "friendly"
pygments_dark_style = "material"
html_static_path = ['_static']
pygments_style = 'friendly'
pygments_dark_style = 'material'

# HTML theme settings
html_theme = "furo"
html_theme = 'furo'
html_theme_options = {
"light_logo": "requests-ratelimiter.png",
"dark_logo": "requests-ratelimiter-white.png",
"sidebar_hide_name": True,
'light_logo': 'requests-ratelimiter.png',
'dark_logo': 'requests-ratelimiter-white.png',
'sidebar_hide_name': True,
}
32 changes: 19 additions & 13 deletions pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "requests-ratelimiter"
version = "0.6.0"
version = "0.6.1"
description = "Rate-limiting for the requests library"
authors = ["Jordan Cook"]
license = "MIT"
Expand Down Expand Up @@ -48,10 +48,6 @@ requests-cache = ">=1.1"
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.black]
line-length = 100
skip-string-normalization = true

[tool.coverage.run]
branch = true
source = ['requests_ratelimiter']
Expand All @@ -62,16 +58,26 @@ directory = 'test-reports'
[tool.coverage.xml]
output = 'test-reports/coverage.xml'

[tool.isort]
profile = 'black'
line_length = 100
skip_gitignore = true
known_first_party = ['tests']

[tool.mypy]
ignore_missing_imports = true

[tool.ruff]
fix = true
unsafe-fixes = true
line-length = 100
output-format = 'grouped'
line-length = 110
select = ['B', 'C4','C90', 'E', 'F']
target-version = 'py38'

[tool.ruff.format]
quote-style = 'single'

[tool.ruff.lint]
select = ['B', 'C4', 'C90', 'E', 'F']
ignore = ['B023']

[tool.ruff.lint.isort]
known-first-party = ['test']

# Wrap lines to 100 chars, but don't error on unwrappable lines until 120 chars
[tool.ruff.lint.pycodestyle]
max-line-length = 120
6 changes: 3 additions & 3 deletions requests_ratelimiter/requests_ratelimiter.py
Expand Up @@ -56,8 +56,8 @@ def __init__(
]
if rates and not limiter:
logger.debug(
"Creating Limiter with rates:\n%s",
"\n".join([f"{r.limit}/{r.interval}s" for r in rates]),
'Creating Limiter with rates:\n%s',
'\n'.join([f'{r.limit}/{r.interval}s' for r in rates]),
)

# If using a persistent backend, we don't want to use monotonic time (the default)
Expand Down Expand Up @@ -121,7 +121,7 @@ def _fill_bucket(self, request: PreparedRequest):
If the server also has an hourly limit, we don't have enough information to know if we've
exceeded that limit or how long to delay, so we'll keep delaying in 1-minute intervals.
"""
logger.info(f"Rate limit exceeded for {request.url}; filling limiter bucket")
logger.info(f'Rate limit exceeded for {request.url}; filling limiter bucket')
bucket = self.limiter.bucket_group[self._bucket_name(request)]

# Determine how many requests we've made within the smallest defined time interval
Expand Down
10 changes: 5 additions & 5 deletions test/test_requests_ratelimiter.py
Expand Up @@ -23,7 +23,7 @@
from requests_ratelimiter import LimiterAdapter, LimiterMixin, LimiterSession
from requests_ratelimiter.requests_ratelimiter import _convert_rate

patch_sleep = patch("pyrate_limiter.limit_context_decorator.sleep", side_effect=sleep)
patch_sleep = patch('pyrate_limiter.limit_context_decorator.sleep', side_effect=sleep)
rate = RequestRate(5, Duration.SECOND)


Expand All @@ -41,7 +41,7 @@ def test_limiter_session(mock_sleep):


@patch_sleep
@patch.object(HTTPAdapter, "send")
@patch.object(HTTPAdapter, 'send')
def test_limiter_adapter(mock_send, mock_sleep):
# To allow mounting a mock:// URL, we need to patch HTTPAdapter.send()
# so it doesn't validate the protocol
Expand All @@ -52,7 +52,7 @@ def test_limiter_adapter(mock_send, mock_sleep):

session = Session()
adapter = LimiterAdapter(per_second=5)
session.mount("http+mock://", adapter)
session.mount('http+mock://', adapter)

for _ in range(5):
session.get(MOCKED_URL)
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_limit_status_disabled(mock_sleep):


@pytest.mark.parametrize(
"limit, interval, expected_limit, expected_interval",
'limit, interval, expected_limit, expected_interval',
[
(5, 1, 5, 1),
(0.5, 1, 1, 2),
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_custom_bucket(mock_sleep, tmp_path):

session_a = get_mock_session(
per_second=5,
bucket_name="a",
bucket_name='a',
bucket_class=SQLiteBucket,
bucket_kwargs={
'path': ratelimit_path,
Expand Down

0 comments on commit 65a1055

Please sign in to comment.