From 9d088192dc55975f32f28bdce4a3cc8ca41cdd3b Mon Sep 17 00:00:00 2001 From: jorenham Date: Wed, 1 May 2024 13:46:05 +0200 Subject: [PATCH 1/3] bump ruff to 0.4.2 --- poetry.lock | 38 +++++++++++++++++++------------------- pyproject.toml | 4 ++-- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/poetry.lock b/poetry.lock index 121ef95..e8e2866 100644 --- a/poetry.lock +++ b/poetry.lock @@ -149,28 +149,28 @@ pytest = ">=4.0.0" [[package]] name = "ruff" -version = "0.3.4" +version = "0.4.2" description = "An extremely fast Python linter and code formatter, written in Rust." optional = false python-versions = ">=3.7" files = [ - {file = "ruff-0.3.4-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl", hash = "sha256:60c870a7d46efcbc8385d27ec07fe534ac32f3b251e4fc44b3cbfd9e09609ef4"}, - {file = "ruff-0.3.4-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:6fc14fa742e1d8f24910e1fff0bd5e26d395b0e0e04cc1b15c7c5e5fe5b4af91"}, - {file = "ruff-0.3.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d3ee7880f653cc03749a3bfea720cf2a192e4f884925b0cf7eecce82f0ce5854"}, - {file = "ruff-0.3.4-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:cf133dd744f2470b347f602452a88e70dadfbe0fcfb5fd46e093d55da65f82f7"}, - {file = "ruff-0.3.4-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:3f3860057590e810c7ffea75669bdc6927bfd91e29b4baa9258fd48b540a4365"}, - {file = "ruff-0.3.4-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:986f2377f7cf12efac1f515fc1a5b753c000ed1e0a6de96747cdf2da20a1b369"}, - {file = "ruff-0.3.4-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c4fd98e85869603e65f554fdc5cddf0712e352fe6e61d29d5a6fe087ec82b76c"}, - {file = "ruff-0.3.4-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:64abeed785dad51801b423fa51840b1764b35d6c461ea8caef9cf9e5e5ab34d9"}, - {file = "ruff-0.3.4-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:df52972138318bc7546d92348a1ee58449bc3f9eaf0db278906eb511889c4b50"}, - {file = "ruff-0.3.4-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:98e98300056445ba2cc27d0b325fd044dc17fcc38e4e4d2c7711585bd0a958ed"}, - {file = "ruff-0.3.4-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:519cf6a0ebed244dce1dc8aecd3dc99add7a2ee15bb68cf19588bb5bf58e0488"}, - {file = "ruff-0.3.4-py3-none-musllinux_1_2_i686.whl", hash = "sha256:bb0acfb921030d00070539c038cd24bb1df73a2981e9f55942514af8b17be94e"}, - {file = "ruff-0.3.4-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:cf187a7e7098233d0d0c71175375c5162f880126c4c716fa28a8ac418dcf3378"}, - {file = "ruff-0.3.4-py3-none-win32.whl", hash = "sha256:af27ac187c0a331e8ef91d84bf1c3c6a5dea97e912a7560ac0cef25c526a4102"}, - {file = "ruff-0.3.4-py3-none-win_amd64.whl", hash = "sha256:de0d5069b165e5a32b3c6ffbb81c350b1e3d3483347196ffdf86dc0ef9e37dd6"}, - {file = "ruff-0.3.4-py3-none-win_arm64.whl", hash = "sha256:6810563cc08ad0096b57c717bd78aeac888a1bfd38654d9113cb3dc4d3f74232"}, - {file = "ruff-0.3.4.tar.gz", hash = "sha256:f0f4484c6541a99862b693e13a151435a279b271cff20e37101116a21e2a1ad1"}, + {file = "ruff-0.4.2-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl", hash = "sha256:8d14dc8953f8af7e003a485ef560bbefa5f8cc1ad994eebb5b12136049bbccc5"}, + {file = "ruff-0.4.2-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:24016ed18db3dc9786af103ff49c03bdf408ea253f3cb9e3638f39ac9cf2d483"}, + {file = "ruff-0.4.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0e2e06459042ac841ed510196c350ba35a9b24a643e23db60d79b2db92af0c2b"}, + {file = "ruff-0.4.2-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:3afabaf7ba8e9c485a14ad8f4122feff6b2b93cc53cd4dad2fd24ae35112d5c5"}, + {file = "ruff-0.4.2-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:799eb468ea6bc54b95527143a4ceaf970d5aa3613050c6cff54c85fda3fde480"}, + {file = "ruff-0.4.2-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:ec4ba9436a51527fb6931a8839af4c36a5481f8c19e8f5e42c2f7ad3a49f5069"}, + {file = "ruff-0.4.2-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:6a2243f8f434e487c2a010c7252150b1fdf019035130f41b77626f5655c9ca22"}, + {file = "ruff-0.4.2-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:8772130a063f3eebdf7095da00c0b9898bd1774c43b336272c3e98667d4fb8fa"}, + {file = "ruff-0.4.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6ab165ef5d72392b4ebb85a8b0fbd321f69832a632e07a74794c0e598e7a8376"}, + {file = "ruff-0.4.2-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:1f32cadf44c2020e75e0c56c3408ed1d32c024766bd41aedef92aa3ca28eef68"}, + {file = "ruff-0.4.2-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:22e306bf15e09af45ca812bc42fa59b628646fa7c26072555f278994890bc7ac"}, + {file = "ruff-0.4.2-py3-none-musllinux_1_2_i686.whl", hash = "sha256:82986bb77ad83a1719c90b9528a9dd663c9206f7c0ab69282af8223566a0c34e"}, + {file = "ruff-0.4.2-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:652e4ba553e421a6dc2a6d4868bc3b3881311702633eb3672f9f244ded8908cd"}, + {file = "ruff-0.4.2-py3-none-win32.whl", hash = "sha256:7891ee376770ac094da3ad40c116258a381b86c7352552788377c6eb16d784fe"}, + {file = "ruff-0.4.2-py3-none-win_amd64.whl", hash = "sha256:5ec481661fb2fd88a5d6cf1f83403d388ec90f9daaa36e40e2c003de66751798"}, + {file = "ruff-0.4.2-py3-none-win_arm64.whl", hash = "sha256:cbd1e87c71bca14792948c4ccb51ee61c3296e164019d2d484f3eaa2d360dfaf"}, + {file = "ruff-0.4.2.tar.gz", hash = "sha256:33bcc160aee2520664bc0859cfeaebc84bb7323becff3f303b8f1f2d81cb4edc"}, ] [[package]] @@ -261,4 +261,4 @@ uvloop = ["uvloop"] [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "002fc4c17719ce8330cece427f5cde395cbee9a81268a4e1016fbdcad8d435ce" +content-hash = "440a012d45e4b565d7badd95a46c06a3e64206f0a72e43100647cbd40c283cde" diff --git a/pyproject.toml b/pyproject.toml index bf1370c..64a908b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ markers = 'sys_platform != "win32"' [tool.poetry.group.lint.dependencies] codespell = "^2.2.6" -ruff = "^0.3.0" +ruff = "^0.4.2" [tool.poetry.group.typecheck.dependencies] pyright = "^1.1.351" @@ -157,7 +157,7 @@ select = [ "LOG", # flake8-logging "RUF", # ruff ] -extend-ignore = [ +ignore = [ # flake8-annotations "ANN001", # missing-type-function-argument (deprecated) "ANN002", # missing-type-args (deprecated) From e3d7e1537af6136787e36e0add17db3f3ac4a6f6 Mon Sep 17 00:00:00 2001 From: jorenham Date: Wed, 1 May 2024 14:12:38 +0200 Subject: [PATCH 2/3] stricter config, and require `typing_extensions` on python 3.10 --- poetry.lock | 2 +- pyproject.toml | 58 +++++++++++++++----------------------------------- 2 files changed, 18 insertions(+), 42 deletions(-) diff --git a/poetry.lock b/poetry.lock index e8e2866..c63d3f4 100644 --- a/poetry.lock +++ b/poetry.lock @@ -261,4 +261,4 @@ uvloop = ["uvloop"] [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "440a012d45e4b565d7badd95a46c06a3e64206f0a72e43100647cbd40c283cde" +content-hash = "1531ad8d69335d100007cc320ab8bf4642a51a7cca64d34d4629bda1f54336de" diff --git a/pyproject.toml b/pyproject.toml index 64a908b..4c4d44d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,16 +13,13 @@ readme = "README.md" repository = "https://github.com/jorenham/mainpy" classifiers = [ "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "Natural Language :: English", - "Topic :: Software Development", - "Topic :: Utilities", + "Operating System :: OS Independent", "Typing :: Typed", ] [tool.poetry.dependencies] python = "^3.8" -typing_extensions = {version = "^4.1", python = "<3.10"} +typing_extensions = {version = "^4.1", python = "<3.11"} [tool.poetry.dependencies.uvloop] version = ">=0.14,<1.0" @@ -43,7 +40,7 @@ pytest = "^8.0" [tool.poetry.group.test-github] optional = true [tool.poetry.group.test-github.dependencies] -pytest-github-actions-annotate-failures = ">=0.2,<1.0" +pytest-github-actions-annotate-failures = ">=0.2.0,<1" [tool.poetry.extras] uvloop = ["uvloop"] @@ -98,13 +95,9 @@ src = ["mainpy", "tests"] target-version = "py38" line-length = 79 indent-width = 4 +show-fixes = true force-exclude = true -extend-exclude = [ - ".github", - ".vscode", - ".venv", - "py.typed", -] +extend-exclude = [".github", ".vscode"] [tool.ruff.lint] ignore-init-module-imports = true @@ -119,6 +112,7 @@ select = [ "YTT", # flake8-2020 "ANN", # flake8-annotations "ASYNC", # flake8-async + "TRIO", # flake8-trio "S", # flake8-bandit "BLE", # flake8-blind-except "B", # flake8-bugbear @@ -147,41 +141,30 @@ select = [ "TCH", # flake8-type-checking "ARG", # flake8-unused-arguments "PTH", # flake8-use-pathlib + "TD", # flake8-todos + "FIX", # flake8-fixme "ERA", # eradicate + "PD", # pandas-vet "PGH", # pygrep-hooks "PL", # pylint "TRY", # tryceratops "FLY", # flynt + "NPY", # numpy + "AIR", # airflow "PERF", # perflint, "FURB", # refurb "LOG", # flake8-logging "RUF", # ruff ] ignore = [ - # flake8-annotations - "ANN001", # missing-type-function-argument (deprecated) - "ANN002", # missing-type-args (deprecated) - "ANN401", # any-type - # flake8-bandit - "S101", # assert - # pylint - "PLC0415", # import-outside-top-level - "PLW1641", # eq-without-hash (bug: doesn't consider super) - # tryceratops - "TRY003", # raise-vanilla-args - # ruff - "RUF021", # parenthesize-chained-operators + "PLC0415", # pylint: import-outside-top-level ] [tool.ruff.lint.per-file-ignores] -"**/__init__.py" = [ - "F401", # pyflakes: unused-import -] "tests/*" = [ - "D", # pydocstyle + "S", # flake8-bandit "ANN", # flake8-annotations "ARG001", # flake8-unused-arguments: unused-function-argument "SLF001", # flake8-self: private-member-access - "PT004", # flake8-pytest-style: pytest-missing-fixture-name-underscore ] [tool.ruff.lint.pycodestyle] @@ -190,28 +173,21 @@ max-line-length = 79 [tool.ruff.lint.isort] case-sensitive = true combine-as-imports = true -force-wrap-aliases = true known-first-party = ["mainpy"] lines-after-imports = 2 lines-between-types = 0 -no-lines-before = ["future", "local-folder"] -split-on-trailing-comma = false - -[tool.ruff.lint.pydocstyle] -convention = "google" -ignore-decorators = ["typing.overload"] - -[tool.ruff.lint.flake8-annotations] -suppress-none-returning = true [tool.ruff.lint.flake8-quotes] inline-quotes = "single" +[tool.ruff.lint.flake8-type-checking] +strict = true + [tool.ruff.format] docstring-code-format = true -quote-style = "single" indent-style = "space" line-ending = "lf" +quote-style = "single" skip-magic-trailing-comma = true From 7609ebf69606fe48a583de2d7b015bafe63a68d0 Mon Sep 17 00:00:00 2001 From: jorenham Date: Wed, 1 May 2024 14:12:45 +0200 Subject: [PATCH 3/3] ruff fixes --- mainpy/__init__.py | 32 +++++++++++++++----------------- tests/test_mainpy.py | 3 ++- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/mainpy/__init__.py b/mainpy/__init__.py index a91cae3..bf7f44c 100644 --- a/mainpy/__init__.py +++ b/mainpy/__init__.py @@ -2,7 +2,6 @@ import asyncio import functools -import importlib.util import inspect import os import sys @@ -21,15 +20,13 @@ if TYPE_CHECKING: import contextvars - -if sys.version_info < (3, 10): - from typing_extensions import TypeAlias +if sys.version_info < (3, 11): + from typing_extensions import Never, TypeAlias else: - from typing import TypeAlias + from typing import Never, TypeAlias __all__ = ('main',) - _R = TypeVar('_R') _F = TypeVar('_F', bound=Callable[..., Any]) @@ -57,9 +54,8 @@ def _infer_debug() -> bool: try: env_debug = int(env_debug) except ValueError as e: - raise OSError( - f'Invalid value for `DEBUG` env var: {env_debug!r}', - ) from e + errmsg = f'Invalid value for `DEBUG` env var: {env_debug!r}' + raise OSError(errmsg) from e return bool(env_debug) @@ -69,10 +65,12 @@ def _infer_uvloop() -> bool: if 'uvloop' in sys.modules: return True + import importlib.util + return importlib.util.find_spec('uvloop') is not None -def _enable_debug(): +def _enable_debug() -> None: """Enable debug mode.""" env = os.environ @@ -91,7 +89,6 @@ def _enable_debug(): def main(__f: _AFunc[_R], /) -> _R | _AFunc[_R]: ... @overload def main(__f: _SFunc[_R], /) -> _R | _SFunc[_R]: ... - @overload def main( *, @@ -127,19 +124,20 @@ def main( ) if not callable(func): - raise TypeError(f'expected a callable, got {func!r}') + errmsg = f'expected a callable, got {type(func)}' + raise TypeError(errmsg) if func.__module__ != '__main__': frame = inspect.currentframe() if not frame or frame.f_globals.get('__name__') != '__main__': return func - if debug or debug is None and _infer_debug(): + if debug or (debug is None and _infer_debug()): _enable_debug() - if ( - is_async is False - or is_async is None and not asyncio.iscoroutinefunction(func) + if is_async is False or ( + is_async is None + and not asyncio.iscoroutinefunction(func) ): return func() @@ -165,6 +163,6 @@ def main( @main -def __main(): +def __main() -> Never: # this should never run raise AssertionError diff --git a/tests/test_mainpy.py b/tests/test_mainpy.py index 0e1b991..4c700ba 100644 --- a/tests/test_mainpy.py +++ b/tests/test_mainpy.py @@ -34,7 +34,7 @@ def no_uvloop(): mp._infer_uvloop = lambda: False try: - yield + yield orig finally: mp._infer_uvloop = orig @@ -96,6 +96,7 @@ def test_async_implicit_uvloop(monkeypatch: pytest.MonkeyPatch): @mp.main @_patch_module(monkeypatch, '__main__') async def loop_module(): + await asyncio.sleep(0) loop = asyncio.get_running_loop() return loop.__module__.split('.')[0]