Skip to content

Commit

Permalink
Fix and enable Ruff lints PYI002, PYI026
Browse files Browse the repository at this point in the history
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
  • Loading branch information
intgr committed Dec 8, 2023
1 parent 9cedbdc commit 76585c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
25 changes: 15 additions & 10 deletions rest_framework-stubs/compat.pyi
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
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):
pattern: Any
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: ...
Expand Down

0 comments on commit 76585c7

Please sign in to comment.