Skip to content

Commit 33d1159

Browse files
committed
🎨 Use ruff to format code
1 parent 88a3f85 commit 33d1159

File tree

9 files changed

+58
-60
lines changed

9 files changed

+58
-60
lines changed

.pre-commit-config.yaml

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
exclude: ^templates/
3+
24
repos:
35
- repo: https://github.com/pre-commit/pre-commit-hooks
46
rev: v4.5.0
@@ -11,7 +13,6 @@ repos:
1113
- id: trailing-whitespace
1214
- id: mixed-line-ending
1315
- id: end-of-file-fixer
14-
exclude: ^templates/class\.txt$
1516
- id: detect-private-key
1617
- id: check-symlinks
1718
- id: check-ast
@@ -64,8 +65,9 @@ repos:
6465
- mdformat-toc
6566
- mdformat-deflist
6667
- mdformat-beautysh
67-
- mdformat-black
68+
- mdformat-ruff
6869
- mdformat-config
70+
- mdformat-web
6971
- repo: https://github.com/DavidAnson/markdownlint-cli2
7072
rev: v0.12.1
7173
hooks:
@@ -77,32 +79,15 @@ repos:
7779
hooks:
7880
- id: update-CITATION.cff
7981
- id: update-pyproject.toml
80-
- repo: https://github.com/psf/black
81-
rev: 24.3.0
82-
hooks:
83-
- id: black
84-
- repo: https://github.com/PyCQA/isort
85-
rev: 5.13.2
86-
hooks:
87-
- id: isort
88-
- repo: https://github.com/pycqa/pydocstyle
89-
rev: 6.3.0
82+
- repo: https://github.com/astral-sh/ruff-pre-commit
83+
rev: v0.2.1
9084
hooks:
91-
- id: pydocstyle
92-
additional_dependencies:
93-
- tomli
85+
- id: ruff
86+
- id: ruff-format
9487
- repo: https://github.com/kumaraditya303/mirrors-pyright
95-
rev: v1.1.354
88+
rev: v1.1.350
9689
hooks:
9790
- id: pyright
98-
- repo: https://github.com/PyCQA/bandit
99-
rev: 1.7.8
100-
hooks:
101-
- id: bandit
102-
args:
103-
- -cpyproject.toml
104-
additional_dependencies:
105-
- tomli
10691
- repo: https://github.com/nix-community/nixpkgs-fmt
10792
rev: v1.3.0
10893
hooks:

docs/conf.py

-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
https://www.sphinx-doc.org/en/master/usage/configuration.html
44
"""
55

6-
from repl_python_wakatime import __version__ as version # type: ignore
7-
from repl_python_wakatime._metainfo import ( # type: ignore
8-
author,
9-
copyright,
10-
project,
11-
)
126

137
# -- Path setup --------------------------------------------------------------
148

src/repl_python_wakatime/hooks/codestats.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ def send_xp(self) -> None:
130130

131131
headers = {
132132
"Content-Type": "application/json",
133-
"User-Agent": "code-stats-python/{0}".format(__version__),
133+
"User-Agent": f"code-stats-python/{__version__}",
134134
"X-API-Token": self.api_key,
135135
"Accept": "*/*",
136136
}
137137

138138
# after lock is released we can send the payload
139139
utc_now = datetime.now().astimezone().isoformat()
140140
pulse_json = json.dumps({
141-
"coded_at": "{0}".format(utc_now),
141+
"coded_at": f"{utc_now}",
142142
"xps": xp_list,
143143
}).encode("utf-8")
144144
req = Request(url=self.url, data=pulse_json, headers=headers)
@@ -150,7 +150,7 @@ def send_xp(self) -> None:
150150
except URLError as e:
151151
try:
152152
# HTTP error
153-
error = "{0} {1}".format(
153+
error = "{} {}".format(
154154
e.code, # type: ignore
155155
e.read().decode("utf-8"), # type: ignore
156156
)
@@ -161,10 +161,7 @@ def send_xp(self) -> None:
161161
# SSL certificate error (eg. a public wifi redirects traffic)
162162
error = e
163163
except HTTPException as e:
164-
error = "HTTPException on send data. Msg: {0}\nDoc?:{1}".format(
165-
e.message,
166-
e.__doc__, # type: ignore
167-
)
164+
error = f"HTTPException on send data. \nDoc: {e.__doc__}"
168165
if error:
169166
logger.error(error)
170167

src/repl_python_wakatime/hooks/wakatime.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
"""
66

77
import os
8+
from collections.abc import Callable
89
from subprocess import Popen # nosec: B404
9-
from typing import Any, Callable
10+
from typing import Any
1011

1112

1213
def wakatime_hook(
1314
project: str = "",
1415
language: str = "python",
1516
category: str = "coding",
1617
plugin: str = "repl-python-wakatime",
17-
filenames: list[str] = [".git"],
18+
filenames: list[str] | None = None,
1819
detect_func: Callable[[str], bool] = os.path.isdir,
1920
*args: Any,
2021
**kwargs: Any,
@@ -33,11 +34,13 @@ def wakatime_hook(
3334
:param plugin:
3435
:type plugin: str
3536
:param filenames:
36-
:type filenames: list[str]
37+
:type filenames: list[str] | None
3738
:param detect_func:
3839
:type detect_func: Callable[[str], bool]
3940
:rtype: None
4041
"""
42+
if filenames is None:
43+
filenames = [".git"]
4144
if project == "":
4245
from ..utils.project import get_project
4346

src/repl_python_wakatime/ipython.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
==========
33
"""
44

5-
from typing import Any, Callable
5+
from collections.abc import Callable
6+
from typing import Any
67

78
from IPython.terminal.interactiveshell import TerminalInteractiveShell
89
from IPython.terminal.prompts import ClassicPrompts, Prompts
@@ -16,7 +17,7 @@ def get_new_prompts_class(
1617
prompts_class: type,
1718
hook: Callable = wakatime_hook,
1819
args: tuple = (),
19-
kwargs: dict[str, Any] = {},
20+
kwargs: dict[str, Any] | None = None,
2021
) -> type:
2122
"""Get new prompts class.
2223
@@ -27,9 +28,11 @@ def get_new_prompts_class(
2728
:param args:
2829
:type args: tuple
2930
:param kwargs:
30-
:type kwargs: dict[str, Any]
31+
:type kwargs: dict[str, Any] | None
3132
:rtype: type
3233
"""
34+
if kwargs is None:
35+
kwargs = {}
3336
if isinstance(prompts_class, LazyConfigValue):
3437
prompts_class = ClassicPrompts
3538
shell = TerminalInteractiveShell()
@@ -77,7 +80,7 @@ def install_hook(
7780
c: Config,
7881
hook: Callable = wakatime_hook,
7982
args: tuple = (),
80-
kwargs: dict[str, Any] = {"plugin": "repl-ipython-wakatime"},
83+
kwargs: dict[str, Any] | None = None,
8184
) -> Config:
8285
"""Install hook.
8386
@@ -88,10 +91,15 @@ def install_hook(
8891
:param args:
8992
:type args: tuple
9093
:param kwargs:
91-
:type kwargs: dict[str, Any]
94+
:type kwargs: dict[str, Any] | None
9295
:rtype: Config
9396
"""
97+
if kwargs is None:
98+
kwargs = {"plugin": "repl-ipython-wakatime"}
9499
c.TerminalInteractiveShell.prompts_class = get_new_prompts_class( # type: ignore
95-
c.TerminalInteractiveShell.prompts_class, hook, args, kwargs # type: ignore
100+
c.TerminalInteractiveShell.prompts_class,
101+
hook,
102+
args,
103+
kwargs, # type: ignore
96104
)
97105
return c

src/repl_python_wakatime/ptpython.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
===========
33
"""
44

5-
from typing import Any, Callable
5+
from collections.abc import Callable
6+
from typing import Any
67

78
from prompt_toolkit.formatted_text import AnyFormattedText
89
from ptpython.prompt_style import PromptStyle
@@ -19,7 +20,7 @@ def __init__(
1920
prompt_style: PromptStyle,
2021
hook: Callable = wakatime_hook,
2122
args: tuple = (),
22-
kwargs: dict[str, Any] = {},
23+
kwargs: dict[str, Any] | None = None,
2324
) -> None:
2425
"""Init.
2526
@@ -30,9 +31,11 @@ def __init__(
3031
:param args:
3132
:type args: tuple
3233
:param kwargs:
33-
:type kwargs: dict[str, Any]
34+
:type kwargs: dict[str, Any] | None
3435
:rtype: None
3536
"""
37+
if kwargs is None:
38+
kwargs = {}
3639
super().__init__()
3740
self.prompt_style = prompt_style
3841
self.hook = hook
@@ -69,7 +72,7 @@ def install_hook(
6972
repl: PythonRepl,
7073
hook: Callable = wakatime_hook,
7174
args: tuple = (),
72-
kwargs: dict[str, Any] = {"plugin": "repl-ptpython-wakatime"},
75+
kwargs: dict[str, Any] | None = None,
7376
hook_prefix: str = "ps1_",
7477
) -> PythonRepl:
7578
"""Install hook.
@@ -81,11 +84,13 @@ def install_hook(
8184
:param args:
8285
:type args: tuple
8386
:param kwargs:
84-
:type kwargs: dict[str, Any]
87+
:type kwargs: dict[str, Any] | None
8588
:param hook_prefix:
8689
:type hook_prefix: str
8790
:rtype: PythonRepl
8891
"""
92+
if kwargs is None:
93+
kwargs = {"plugin": "repl-ptpython-wakatime"}
8994
ps = Ps(repl.all_prompt_styles[repl.prompt_style], hook, args, kwargs)
9095
length = len(hook_prefix)
9196
names = map(

src/repl_python_wakatime/python.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"""
44

55
import sys
6-
from typing import Any, Callable
6+
from collections.abc import Callable
7+
from typing import Any
78

89
from .hooks.wakatime import wakatime_hook
910

@@ -16,7 +17,7 @@ def __init__(
1617
ps1: object = None,
1718
hook: Callable = wakatime_hook,
1819
args: tuple = (),
19-
kwargs: dict[str, Any] = {},
20+
kwargs: dict[str, Any] | None = None,
2021
) -> None:
2122
"""Init.
2223
@@ -27,9 +28,11 @@ def __init__(
2728
:param args:
2829
:type args: tuple
2930
:param kwargs:
30-
:type kwargs: dict[str, Any]
31+
:type kwargs: dict[str, Any] | None
3132
:rtype: None
3233
"""
34+
if kwargs is None:
35+
kwargs = {}
3336
if ps1:
3437
self.ps1 = ps1
3538
else:
@@ -56,7 +59,7 @@ def __str__(self) -> str:
5659
def install_hook(
5760
hook: Callable = wakatime_hook,
5861
args: tuple = (),
59-
kwargs: dict[str, Any] = {"plugin": "repl-python-wakatime"},
62+
kwargs: dict[str, Any] | None = None,
6063
) -> object:
6164
"""Install hook.
6265
@@ -65,8 +68,10 @@ def install_hook(
6568
:param args:
6669
:type args: tuple
6770
:param kwargs:
68-
:type kwargs: dict[str, Any]
71+
:type kwargs: dict[str, Any] | None
6972
:rtype: object
7073
"""
74+
if kwargs is None:
75+
kwargs = {"plugin": "repl-python-wakatime"}
7176
sys.ps1 = Ps1(hook=hook, args=args, kwargs=kwargs)
7277
return sys.ps1

src/repl_python_wakatime/utils/project.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"""
44

55
import os
6-
from typing import Callable
6+
from collections.abc import Callable
77

88

99
def get_project(
10-
filenames: list[str] = [".git"],
10+
filenames: list[str] | None = None,
1111
detect_func: Callable[[str], bool] = os.path.isdir,
1212
) -> str:
1313
"""Get project. Its function is like ``git rev-parse --show-toplevel``.
@@ -18,10 +18,12 @@ def get_project(
1818
use current directory as ``project``.
1919
2020
:param filenames:
21-
:type filenames: list[str]
21+
:type filenames: list[str] | None
2222
:param detect_func:
2323
:type detect_func: Callable[[str], bool]
2424
"""
25+
if filenames is None:
26+
filenames = [".git"]
2527
cwd = os.getcwd()
2628
project = cwd
2729
oldproject = ""

tests/test_api.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import pytest
6-
76
from repl_python_wakatime.hooks.codestats import CodeStats
87
from repl_python_wakatime.utils.api import get_api_key
98

0 commit comments

Comments
 (0)