From 33d1159cf5b5a7ea3b8fddb72f0a8905bfeb85c1 Mon Sep 17 00:00:00 2001 From: "Wu, Zhenyu" Date: Tue, 9 Apr 2024 01:43:38 +0800 Subject: [PATCH] :art: Use ruff to format code --- .pre-commit-config.yaml | 33 ++++++--------------- docs/conf.py | 6 ---- src/repl_python_wakatime/hooks/codestats.py | 11 +++---- src/repl_python_wakatime/hooks/wakatime.py | 9 ++++-- src/repl_python_wakatime/ipython.py | 20 +++++++++---- src/repl_python_wakatime/ptpython.py | 15 ++++++---- src/repl_python_wakatime/python.py | 15 ++++++---- src/repl_python_wakatime/utils/project.py | 8 +++-- tests/test_api.py | 1 - 9 files changed, 58 insertions(+), 60 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b54b208..ac4e71d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,4 +1,6 @@ --- +exclude: ^templates/ + repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.5.0 @@ -11,7 +13,6 @@ repos: - id: trailing-whitespace - id: mixed-line-ending - id: end-of-file-fixer - exclude: ^templates/class\.txt$ - id: detect-private-key - id: check-symlinks - id: check-ast @@ -64,8 +65,9 @@ repos: - mdformat-toc - mdformat-deflist - mdformat-beautysh - - mdformat-black + - mdformat-ruff - mdformat-config + - mdformat-web - repo: https://github.com/DavidAnson/markdownlint-cli2 rev: v0.12.1 hooks: @@ -77,32 +79,15 @@ repos: hooks: - id: update-CITATION.cff - id: update-pyproject.toml - - repo: https://github.com/psf/black - rev: 24.3.0 - hooks: - - id: black - - repo: https://github.com/PyCQA/isort - rev: 5.13.2 - hooks: - - id: isort - - repo: https://github.com/pycqa/pydocstyle - rev: 6.3.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.2.1 hooks: - - id: pydocstyle - additional_dependencies: - - tomli + - id: ruff + - id: ruff-format - repo: https://github.com/kumaraditya303/mirrors-pyright - rev: v1.1.354 + rev: v1.1.350 hooks: - id: pyright - - repo: https://github.com/PyCQA/bandit - rev: 1.7.8 - hooks: - - id: bandit - args: - - -cpyproject.toml - additional_dependencies: - - tomli - repo: https://github.com/nix-community/nixpkgs-fmt rev: v1.3.0 hooks: diff --git a/docs/conf.py b/docs/conf.py index b25b2eb..95f5865 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -3,12 +3,6 @@ https://www.sphinx-doc.org/en/master/usage/configuration.html """ -from repl_python_wakatime import __version__ as version # type: ignore -from repl_python_wakatime._metainfo import ( # type: ignore - author, - copyright, - project, -) # -- Path setup -------------------------------------------------------------- diff --git a/src/repl_python_wakatime/hooks/codestats.py b/src/repl_python_wakatime/hooks/codestats.py index 47fffe6..26d0654 100644 --- a/src/repl_python_wakatime/hooks/codestats.py +++ b/src/repl_python_wakatime/hooks/codestats.py @@ -130,7 +130,7 @@ def send_xp(self) -> None: headers = { "Content-Type": "application/json", - "User-Agent": "code-stats-python/{0}".format(__version__), + "User-Agent": f"code-stats-python/{__version__}", "X-API-Token": self.api_key, "Accept": "*/*", } @@ -138,7 +138,7 @@ def send_xp(self) -> None: # after lock is released we can send the payload utc_now = datetime.now().astimezone().isoformat() pulse_json = json.dumps({ - "coded_at": "{0}".format(utc_now), + "coded_at": f"{utc_now}", "xps": xp_list, }).encode("utf-8") req = Request(url=self.url, data=pulse_json, headers=headers) @@ -150,7 +150,7 @@ def send_xp(self) -> None: except URLError as e: try: # HTTP error - error = "{0} {1}".format( + error = "{} {}".format( e.code, # type: ignore e.read().decode("utf-8"), # type: ignore ) @@ -161,10 +161,7 @@ def send_xp(self) -> None: # SSL certificate error (eg. a public wifi redirects traffic) error = e except HTTPException as e: - error = "HTTPException on send data. Msg: {0}\nDoc?:{1}".format( - e.message, - e.__doc__, # type: ignore - ) + error = f"HTTPException on send data. \nDoc: {e.__doc__}" if error: logger.error(error) diff --git a/src/repl_python_wakatime/hooks/wakatime.py b/src/repl_python_wakatime/hooks/wakatime.py index ddf3a35..a138bfc 100644 --- a/src/repl_python_wakatime/hooks/wakatime.py +++ b/src/repl_python_wakatime/hooks/wakatime.py @@ -5,8 +5,9 @@ """ import os +from collections.abc import Callable from subprocess import Popen # nosec: B404 -from typing import Any, Callable +from typing import Any def wakatime_hook( @@ -14,7 +15,7 @@ def wakatime_hook( language: str = "python", category: str = "coding", plugin: str = "repl-python-wakatime", - filenames: list[str] = [".git"], + filenames: list[str] | None = None, detect_func: Callable[[str], bool] = os.path.isdir, *args: Any, **kwargs: Any, @@ -33,11 +34,13 @@ def wakatime_hook( :param plugin: :type plugin: str :param filenames: - :type filenames: list[str] + :type filenames: list[str] | None :param detect_func: :type detect_func: Callable[[str], bool] :rtype: None """ + if filenames is None: + filenames = [".git"] if project == "": from ..utils.project import get_project diff --git a/src/repl_python_wakatime/ipython.py b/src/repl_python_wakatime/ipython.py index 0defc85..d2b8f98 100644 --- a/src/repl_python_wakatime/ipython.py +++ b/src/repl_python_wakatime/ipython.py @@ -2,7 +2,8 @@ ========== """ -from typing import Any, Callable +from collections.abc import Callable +from typing import Any from IPython.terminal.interactiveshell import TerminalInteractiveShell from IPython.terminal.prompts import ClassicPrompts, Prompts @@ -16,7 +17,7 @@ def get_new_prompts_class( prompts_class: type, hook: Callable = wakatime_hook, args: tuple = (), - kwargs: dict[str, Any] = {}, + kwargs: dict[str, Any] | None = None, ) -> type: """Get new prompts class. @@ -27,9 +28,11 @@ def get_new_prompts_class( :param args: :type args: tuple :param kwargs: - :type kwargs: dict[str, Any] + :type kwargs: dict[str, Any] | None :rtype: type """ + if kwargs is None: + kwargs = {} if isinstance(prompts_class, LazyConfigValue): prompts_class = ClassicPrompts shell = TerminalInteractiveShell() @@ -77,7 +80,7 @@ def install_hook( c: Config, hook: Callable = wakatime_hook, args: tuple = (), - kwargs: dict[str, Any] = {"plugin": "repl-ipython-wakatime"}, + kwargs: dict[str, Any] | None = None, ) -> Config: """Install hook. @@ -88,10 +91,15 @@ def install_hook( :param args: :type args: tuple :param kwargs: - :type kwargs: dict[str, Any] + :type kwargs: dict[str, Any] | None :rtype: Config """ + if kwargs is None: + kwargs = {"plugin": "repl-ipython-wakatime"} c.TerminalInteractiveShell.prompts_class = get_new_prompts_class( # type: ignore - c.TerminalInteractiveShell.prompts_class, hook, args, kwargs # type: ignore + c.TerminalInteractiveShell.prompts_class, + hook, + args, + kwargs, # type: ignore ) return c diff --git a/src/repl_python_wakatime/ptpython.py b/src/repl_python_wakatime/ptpython.py index 7837233..7370bc9 100644 --- a/src/repl_python_wakatime/ptpython.py +++ b/src/repl_python_wakatime/ptpython.py @@ -2,7 +2,8 @@ =========== """ -from typing import Any, Callable +from collections.abc import Callable +from typing import Any from prompt_toolkit.formatted_text import AnyFormattedText from ptpython.prompt_style import PromptStyle @@ -19,7 +20,7 @@ def __init__( prompt_style: PromptStyle, hook: Callable = wakatime_hook, args: tuple = (), - kwargs: dict[str, Any] = {}, + kwargs: dict[str, Any] | None = None, ) -> None: """Init. @@ -30,9 +31,11 @@ def __init__( :param args: :type args: tuple :param kwargs: - :type kwargs: dict[str, Any] + :type kwargs: dict[str, Any] | None :rtype: None """ + if kwargs is None: + kwargs = {} super().__init__() self.prompt_style = prompt_style self.hook = hook @@ -69,7 +72,7 @@ def install_hook( repl: PythonRepl, hook: Callable = wakatime_hook, args: tuple = (), - kwargs: dict[str, Any] = {"plugin": "repl-ptpython-wakatime"}, + kwargs: dict[str, Any] | None = None, hook_prefix: str = "ps1_", ) -> PythonRepl: """Install hook. @@ -81,11 +84,13 @@ def install_hook( :param args: :type args: tuple :param kwargs: - :type kwargs: dict[str, Any] + :type kwargs: dict[str, Any] | None :param hook_prefix: :type hook_prefix: str :rtype: PythonRepl """ + if kwargs is None: + kwargs = {"plugin": "repl-ptpython-wakatime"} ps = Ps(repl.all_prompt_styles[repl.prompt_style], hook, args, kwargs) length = len(hook_prefix) names = map( diff --git a/src/repl_python_wakatime/python.py b/src/repl_python_wakatime/python.py index 1337eb9..cb5cbea 100644 --- a/src/repl_python_wakatime/python.py +++ b/src/repl_python_wakatime/python.py @@ -3,7 +3,8 @@ """ import sys -from typing import Any, Callable +from collections.abc import Callable +from typing import Any from .hooks.wakatime import wakatime_hook @@ -16,7 +17,7 @@ def __init__( ps1: object = None, hook: Callable = wakatime_hook, args: tuple = (), - kwargs: dict[str, Any] = {}, + kwargs: dict[str, Any] | None = None, ) -> None: """Init. @@ -27,9 +28,11 @@ def __init__( :param args: :type args: tuple :param kwargs: - :type kwargs: dict[str, Any] + :type kwargs: dict[str, Any] | None :rtype: None """ + if kwargs is None: + kwargs = {} if ps1: self.ps1 = ps1 else: @@ -56,7 +59,7 @@ def __str__(self) -> str: def install_hook( hook: Callable = wakatime_hook, args: tuple = (), - kwargs: dict[str, Any] = {"plugin": "repl-python-wakatime"}, + kwargs: dict[str, Any] | None = None, ) -> object: """Install hook. @@ -65,8 +68,10 @@ def install_hook( :param args: :type args: tuple :param kwargs: - :type kwargs: dict[str, Any] + :type kwargs: dict[str, Any] | None :rtype: object """ + if kwargs is None: + kwargs = {"plugin": "repl-python-wakatime"} sys.ps1 = Ps1(hook=hook, args=args, kwargs=kwargs) return sys.ps1 diff --git a/src/repl_python_wakatime/utils/project.py b/src/repl_python_wakatime/utils/project.py index 3cb01b0..03d713b 100644 --- a/src/repl_python_wakatime/utils/project.py +++ b/src/repl_python_wakatime/utils/project.py @@ -3,11 +3,11 @@ """ import os -from typing import Callable +from collections.abc import Callable def get_project( - filenames: list[str] = [".git"], + filenames: list[str] | None = None, detect_func: Callable[[str], bool] = os.path.isdir, ) -> str: """Get project. Its function is like ``git rev-parse --show-toplevel``. @@ -18,10 +18,12 @@ def get_project( use current directory as ``project``. :param filenames: - :type filenames: list[str] + :type filenames: list[str] | None :param detect_func: :type detect_func: Callable[[str], bool] """ + if filenames is None: + filenames = [".git"] cwd = os.getcwd() project = cwd oldproject = "" diff --git a/tests/test_api.py b/tests/test_api.py index bbf2f40..7243a85 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -3,7 +3,6 @@ """ import pytest - from repl_python_wakatime.hooks.codestats import CodeStats from repl_python_wakatime.utils.api import get_api_key