Skip to content

Commit 4148a7b

Browse files
srittauAlexWaygood
andauthored
Upgrade bleach stubs for 6.0 (#9583)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 307aadc commit 4148a7b

File tree

5 files changed

+73
-44
lines changed

5 files changed

+73
-44
lines changed

stubs/bleach/METADATA.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "5.0.*"
1+
version = "6.0.*"

stubs/bleach/bleach/__init__.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from collections.abc import Container, Iterable
2+
from typing_extensions import TypeAlias
23

4+
from .callbacks import _Callback
35
from .css_sanitizer import CSSSanitizer
4-
from .linkifier import DEFAULT_CALLBACKS as DEFAULT_CALLBACKS, Linker as Linker, _Callback
6+
from .linkifier import DEFAULT_CALLBACKS as DEFAULT_CALLBACKS, Linker as Linker
57
from .sanitizer import (
68
ALLOWED_ATTRIBUTES as ALLOWED_ATTRIBUTES,
79
ALLOWED_PROTOCOLS as ALLOWED_PROTOCOLS,
@@ -15,11 +17,13 @@ __all__ = ["clean", "linkify"]
1517
__releasedate__: str
1618
__version__: str
1719

20+
_HTMLAttrKey: TypeAlias = tuple[str | None, str] # noqa: Y047
21+
1822
def clean(
1923
text: str,
20-
tags: Container[str] = ...,
24+
tags: Iterable[str] = ...,
2125
attributes: _Attributes = ...,
22-
protocols: Container[str] = ...,
26+
protocols: Iterable[str] = ...,
2327
strip: bool = ...,
2428
strip_comments: bool = ...,
2529
css_sanitizer: CSSSanitizer | None = ...,

stubs/bleach/bleach/callbacks.pyi

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
from collections.abc import MutableMapping
2-
from typing import Any
2+
from typing import Protocol
33
from typing_extensions import TypeAlias
44

5-
_Attrs: TypeAlias = MutableMapping[Any, str]
5+
from bleach import _HTMLAttrKey
66

7-
def nofollow(attrs: _Attrs, new: bool = ...) -> _Attrs: ...
8-
def target_blank(attrs: _Attrs, new: bool = ...) -> _Attrs: ...
7+
_HTMLAttrs: TypeAlias = MutableMapping[_HTMLAttrKey, str]
8+
9+
class _Callback(Protocol): # noqa: Y046
10+
def __call__(self, attrs: _HTMLAttrs, new: bool = ...) -> _HTMLAttrs: ...
11+
12+
def nofollow(attrs: _HTMLAttrs, new: bool = ...) -> _HTMLAttrs: ...
13+
def target_blank(attrs: _HTMLAttrs, new: bool = ...) -> _HTMLAttrs: ...

stubs/bleach/bleach/linkifier.pyi

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
from _typeshed import Incomplete
2-
from collections.abc import Container, Iterable, MutableMapping
2+
from collections.abc import Container, Iterable, Iterator
33
from re import Pattern
4-
from typing import Any, Protocol
5-
from typing_extensions import TypeAlias
64

5+
from .callbacks import _Callback
76
from .html5lib_shim import Filter
87

9-
_Attrs: TypeAlias = MutableMapping[Any, str]
10-
11-
class _Callback(Protocol):
12-
def __call__(self, attrs: _Attrs, new: bool = ...) -> _Attrs: ...
13-
148
DEFAULT_CALLBACKS: list[_Callback]
159

1610
TLDS: list[str]
@@ -28,21 +22,34 @@ class Linker:
2822
def __init__(
2923
self,
3024
callbacks: Iterable[_Callback] = ...,
31-
skip_tags: Container[str] | None = ...,
32-
parse_email: bool = ...,
25+
skip_tags: Container[str] | None = None,
26+
parse_email: bool = False,
3327
url_re: Pattern[str] = ...,
3428
email_re: Pattern[str] = ...,
3529
recognized_tags: Container[str] | None = ...,
3630
) -> None: ...
3731
def linkify(self, text: str) -> str: ...
3832

3933
class LinkifyFilter(Filter):
40-
callbacks: Any
34+
callbacks: Iterable[_Callback]
4135
skip_tags: Container[str]
4236
parse_email: bool
43-
url_re: Any
44-
email_re: Any
37+
url_re: Pattern[str]
38+
email_re: Pattern[str]
4539
def __init__(
46-
self, source, callbacks=..., skip_tags: Container[str] | None = ..., parse_email: bool = ..., url_re=..., email_re=...
40+
self,
41+
source,
42+
callbacks: Iterable[_Callback] | None = ...,
43+
skip_tags: Container[str] | None = None,
44+
parse_email: bool = False,
45+
url_re: Pattern[str] = ...,
46+
email_re: Pattern[str] = ...,
4747
) -> None: ...
48-
def __getattr__(self, item: str) -> Incomplete: ...
48+
def apply_callbacks(self, attrs, is_new): ...
49+
def extract_character_data(self, token_list): ...
50+
def handle_email_addresses(self, src_iter): ...
51+
def strip_non_url_bits(self, fragment): ...
52+
def handle_links(self, src_iter): ...
53+
def handle_a_tag(self, token_buffer): ...
54+
def extract_entities(self, token): ...
55+
def __iter__(self) -> Iterator[Incomplete]: ...

stubs/bleach/bleach/sanitizer.pyi

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,78 @@
1-
from collections.abc import Callable, Container, Iterable
1+
from _typeshed import Incomplete
2+
from collections.abc import Callable, Iterable
23
from re import Pattern
3-
from typing import Any
4+
from typing import Protocol
45
from typing_extensions import TypeAlias
56

7+
from . import _HTMLAttrKey
68
from .css_sanitizer import CSSSanitizer
79
from .html5lib_shim import BleachHTMLParser, BleachHTMLSerializer, SanitizerFilter
810

9-
ALLOWED_TAGS: list[str]
11+
ALLOWED_TAGS: frozenset[str]
1012
ALLOWED_ATTRIBUTES: dict[str, list[str]]
11-
ALLOWED_PROTOCOLS: list[str]
13+
ALLOWED_PROTOCOLS: frozenset[str]
1214

1315
INVISIBLE_CHARACTERS: str
1416
INVISIBLE_CHARACTERS_RE: Pattern[str]
1517
INVISIBLE_REPLACEMENT_CHAR: str
1618

1719
# A html5lib Filter class
18-
_Filter: TypeAlias = Any
20+
class _Filter(Protocol):
21+
def __call__(self, *, source: BleachSanitizerFilter) -> Incomplete: ...
22+
23+
_AttributeFilter: TypeAlias = Callable[[str, str, str], bool]
24+
_AttributeDict: TypeAlias = dict[str, list[str] | _AttributeFilter] | dict[str, list[str]] | dict[str, _AttributeFilter]
25+
_Attributes: TypeAlias = _AttributeFilter | _AttributeDict | list[str]
26+
27+
_TreeWalker: TypeAlias = Callable[[Incomplete], Incomplete]
1928

2029
class Cleaner:
21-
tags: Container[str]
30+
tags: Iterable[str]
2231
attributes: _Attributes
23-
protocols: Container[str]
32+
protocols: Iterable[str]
2433
strip: bool
2534
strip_comments: bool
2635
filters: Iterable[_Filter]
2736
css_sanitizer: CSSSanitizer | None
2837
parser: BleachHTMLParser
29-
walker: Any
38+
walker: _TreeWalker
3039
serializer: BleachHTMLSerializer
3140
def __init__(
3241
self,
33-
tags: Container[str] = ...,
42+
tags: Iterable[str] = ...,
3443
attributes: _Attributes = ...,
35-
protocols: Container[str] = ...,
44+
protocols: Iterable[str] = ...,
3645
strip: bool = ...,
3746
strip_comments: bool = ...,
3847
filters: Iterable[_Filter] | None = ...,
3948
css_sanitizer: CSSSanitizer | None = ...,
4049
) -> None: ...
4150
def clean(self, text: str) -> str: ...
4251

43-
_AttributeFilter: TypeAlias = Callable[[str, str, str], bool]
44-
_AttributeDict: TypeAlias = dict[str, list[str] | _AttributeFilter] | dict[str, list[str]] | dict[str, _AttributeFilter]
45-
_Attributes: TypeAlias = _AttributeFilter | _AttributeDict | list[str]
46-
4752
def attribute_filter_factory(attributes: _Attributes) -> _AttributeFilter: ...
4853

4954
class BleachSanitizerFilter(SanitizerFilter):
55+
allowed_tags: frozenset[str]
56+
allowed_protocols: frozenset[str]
5057
attr_filter: _AttributeFilter
51-
strip_disallowed_elements: bool
58+
strip_disallowed_tags: bool
5259
strip_html_comments: bool
60+
attr_val_is_uri: frozenset[_HTMLAttrKey]
61+
svg_attr_val_allows_ref: frozenset[_HTMLAttrKey]
62+
svg_allow_local_href: frozenset[_HTMLAttrKey]
63+
css_sanitizer: CSSSanitizer | None
5364
def __init__(
5465
self,
5566
source,
56-
allowed_elements: Container[str] = ...,
67+
allowed_tags: Iterable[str] = ...,
5768
attributes: _Attributes = ...,
58-
allowed_protocols: Container[str] = ...,
59-
strip_disallowed_elements: bool = ...,
60-
strip_html_comments: bool = ...,
61-
css_sanitizer: CSSSanitizer | None = ...,
62-
**kwargs,
69+
allowed_protocols: Iterable[str] = ...,
70+
attr_val_is_uri: frozenset[_HTMLAttrKey] = ...,
71+
svg_attr_val_allows_ref: frozenset[_HTMLAttrKey] = ...,
72+
svg_allow_local_href: frozenset[_HTMLAttrKey] = ...,
73+
strip_disallowed_tags: bool = False,
74+
strip_html_comments: bool = True,
75+
css_sanitizer: CSSSanitizer | None = None,
6376
) -> None: ...
6477
def sanitize_stream(self, token_iterator): ...
6578
def merge_characters(self, token_iterator): ...

0 commit comments

Comments
 (0)