From 76585c7dc8b26bce7d225ad059bdd965c8c3ae76 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Mon, 23 Oct 2023 00:39:51 +0300 Subject: [PATCH 1/2] Fix and enable Ruff lints PYI002, PYI026 These affect `rest_framework-stubs/compat.pyi` which is using some weird hacks that Ruff and mypy don't really like. * PYI002: If test must be a simple comparison against sys.platform or sys.version_info * PYI026: Use typing_extensions.TypeAlias for type aliases --- pyproject.toml | 3 +-- rest_framework-stubs/compat.pyi | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 091200edf..0bcc8bd12 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,10 +39,9 @@ ignore = ["PYI021", "PYI024", "PYI041", "PYI043"] "F405", "F822", "F821", - "PYI026", # TODO fix these errors "PGH003", # TODO fix these errors - "PYI002", # TODO fix these errors ] +"rest_framework-stubs/compat.pyi" = ["PYI042"] [tool.ruff.flake8-tidy-imports.banned-api] "_typeshed.Self".msg = "Use typing_extensions.Self (PEP 673) instead." diff --git a/rest_framework-stubs/compat.pyi b/rest_framework-stubs/compat.pyi index 3f3c243b8..883110ae3 100644 --- a/rest_framework-stubs/compat.pyi +++ b/rest_framework-stubs/compat.pyi @@ -1,44 +1,46 @@ from typing import Any from django.db.models import QuerySet +from typing_extensions import TypeAlias try: from django.contrib.postgres import fields as postgres_fields except ImportError: - postgres_fields = None # type: ignore + postgres_fields: TypeAlias = None # type: ignore[no-redef] try: import coreapi except ImportError: - coreapi = None + coreapi: TypeAlias = None # type: ignore[no-redef] try: import uritemplate except ImportError: - uritemplate = None # type: ignore + uritemplate: TypeAlias = None # type: ignore[no-redef] try: import coreschema except ImportError: - coreschema = None + coreschema: TypeAlias = None # type: ignore[no-redef] try: import yaml except ImportError: - yaml = None # type: ignore + yaml: TypeAlias = None # type: ignore[no-redef] try: import requests except ImportError: - requests = None # type: ignore + requests: TypeAlias = None # type: ignore[no-redef] try: import pygments except ImportError: - pygments = None + pygments: TypeAlias = None # type: ignore[no-redef] try: import markdown def apply_markdown(text: str) -> str: ... except ImportError: - apply_markdown = None # type: ignore - markdown = None # type: ignore + apply_markdown: TypeAlias = None # type: ignore[no-redef] + markdown: TypeAlias = None # type: ignore[no-redef] -if markdown is not None and pygments is not None: +try: + import pygments from markdown.preprocessors import Preprocessor class CodeBlockPreprocessor(Preprocessor): @@ -46,6 +48,9 @@ if markdown is not None and pygments is not None: formatter: Any def run(self, lines: list[str]) -> list[str]: ... +except ImportError: + pass + def pygments_css(style: Any) -> str | None: ... def pygments_highlight(text: str, lang: str, style: Any) -> Any: ... def md_filter_add_syntax_highlight(md: Any) -> bool: ... From b86a5f959cb2dfdbe44163f81640b1af4a5452b5 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Fri, 8 Dec 2023 15:32:56 +0200 Subject: [PATCH 2/2] Simplify CodeBlockPreprocessor definition --- rest_framework-stubs/compat.pyi | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/rest_framework-stubs/compat.pyi b/rest_framework-stubs/compat.pyi index 883110ae3..bd12eafde 100644 --- a/rest_framework-stubs/compat.pyi +++ b/rest_framework-stubs/compat.pyi @@ -31,17 +31,11 @@ try: import pygments except ImportError: pygments: TypeAlias = None # type: ignore[no-redef] -try: - import markdown - def apply_markdown(text: str) -> str: ... - -except ImportError: - apply_markdown: TypeAlias = None # type: ignore[no-redef] - markdown: TypeAlias = None # type: ignore[no-redef] try: - import pygments + import markdown from markdown.preprocessors import Preprocessor + def apply_markdown(text: str) -> str: ... class CodeBlockPreprocessor(Preprocessor): pattern: Any @@ -49,7 +43,8 @@ try: def run(self, lines: list[str]) -> list[str]: ... except ImportError: - pass + apply_markdown: TypeAlias = None # type: ignore[no-redef] + markdown: TypeAlias = None # type: ignore[no-redef] def pygments_css(style: Any) -> str | None: ... def pygments_highlight(text: str, lang: str, style: Any) -> Any: ...