Skip to content

Commit 7b71a60

Browse files
authored
Fix and enable Ruff lints PYI002, PYI026 (#496)
* 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 * Simplify CodeBlockPreprocessor definition
1 parent 617c0ba commit 7b71a60

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ ignore = ["PYI021", "PYI024", "PYI041", "PYI043"]
4040
"F405",
4141
"F822",
4242
"F821",
43-
"PYI026", # TODO fix these errors
4443
"PGH003", # TODO fix these errors
45-
"PYI002", # TODO fix these errors
4644
]
45+
"rest_framework-stubs/compat.pyi" = ["PYI042"]
4746

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

rest_framework-stubs/compat.pyi

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
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]
34+
3335
try:
3436
import markdown
35-
def apply_markdown(text: str) -> str: ...
36-
37-
except ImportError:
38-
apply_markdown = None # type: ignore
39-
markdown = None # type: ignore
40-
41-
if markdown is not None and pygments is not None:
4237
from markdown.preprocessors import Preprocessor
38+
def apply_markdown(text: str) -> str: ...
4339

4440
class CodeBlockPreprocessor(Preprocessor):
4541
pattern: Any
4642
formatter: Any
4743
def run(self, lines: list[str]) -> list[str]: ...
4844

45+
except ImportError:
46+
apply_markdown: TypeAlias = None # type: ignore[no-redef]
47+
markdown: TypeAlias = None # type: ignore[no-redef]
48+
4949
def pygments_css(style: Any) -> str | None: ...
5050
def pygments_highlight(text: str, lang: str, style: Any) -> Any: ...
5151
def md_filter_add_syntax_highlight(md: Any) -> bool: ...

0 commit comments

Comments
 (0)