Skip to content

Commit a7ca882

Browse files
committed
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
1 parent 133e572 commit a7ca882

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ ignore = ["PYI021", "PYI024", "PYI041", "PYI043"]
3939
"F405",
4040
"F822",
4141
"F821",
42-
"PYI026", # TODO fix these errors
4342
"PGH003", # TODO fix these errors
44-
"PYI002", # TODO fix these errors
4543
]
44+
"rest_framework-stubs/compat.pyi" = ["PYI042"]
4645

4746
[tool.ruff.flake8-tidy-imports.banned-api]
4847
"_typeshed.Self".msg = "Use typing_extensions.Self (PEP 673) instead."

rest_framework-stubs/compat.pyi

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,56 @@
11
from typing import Any
22

33
from django.db.models import QuerySet
4+
from typing_extensions import TypeAlias
45

56
try:
67
from django.contrib.postgres import fields as postgres_fields
78
except ImportError:
8-
postgres_fields = None # type: ignore
9+
postgres_fields: TypeAlias = None # type: ignore[no-redef]
910
try:
1011
import coreapi
1112
except ImportError:
12-
coreapi = None
13+
coreapi: TypeAlias = None # type: ignore[no-redef]
1314
try:
1415
import uritemplate
1516
except ImportError:
16-
uritemplate = None # type: ignore
17+
uritemplate: TypeAlias = None # type: ignore[no-redef]
1718
try:
1819
import coreschema
1920
except ImportError:
20-
coreschema = None
21+
coreschema: TypeAlias = None # type: ignore[no-redef]
2122
try:
2223
import yaml
2324
except ImportError:
24-
yaml = None # type: ignore
25+
yaml: TypeAlias = None # type: ignore[no-redef]
2526
try:
2627
import requests
2728
except ImportError:
28-
requests = None # type: ignore
29+
requests: TypeAlias = None # type: ignore[no-redef]
2930
try:
3031
import pygments
3132
except ImportError:
32-
pygments = None
33+
pygments: TypeAlias = None # type: ignore[no-redef]
3334
try:
3435
import markdown
3536
def apply_markdown(text: str) -> str: ...
3637

3738
except ImportError:
38-
apply_markdown = None # type: ignore
39-
markdown = None # type: ignore
39+
apply_markdown: TypeAlias = None # type: ignore[no-redef]
40+
markdown: TypeAlias = None # type: ignore[no-redef]
4041

41-
if markdown is not None and pygments is not None:
42+
try:
43+
import pygments
4244
from markdown.preprocessors import Preprocessor
4345

4446
class CodeBlockPreprocessor(Preprocessor):
4547
pattern: Any
4648
formatter: Any
4749
def run(self, lines: list[str]) -> list[str]: ...
4850

51+
except ImportError:
52+
pass
53+
4954
def pygments_css(style: Any) -> str | None: ...
5055
def pygments_highlight(text: str, lang: str, style: Any) -> Any: ...
5156
def md_filter_add_syntax_highlight(md: Any) -> bool: ...

0 commit comments

Comments
 (0)