Skip to content

Commit

Permalink
Add literal string support to includer and exclude filters
Browse files Browse the repository at this point in the history
  • Loading branch information
lqhuang committed Apr 12, 2023
1 parent 683d056 commit c37f9f7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
.pytest_cache
.tox
.vscode
.venv*
build
dist
docs/_build
Expand Down
23 changes: 17 additions & 6 deletions src/attr/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def _split_what(what):
"""
return (
frozenset(cls for cls in what if isinstance(cls, type)),
frozenset(cls for cls in what if isinstance(cls, str)),
frozenset(cls for cls in what if isinstance(cls, Attribute)),
)

Expand All @@ -22,14 +23,19 @@ def include(*what):
Include *what*.
:param what: What to include.
:type what: `list` of `type` or `attrs.Attribute`\\ s
:type what: `list` of classes `type`, field names `str` or
`attrs.Attribute`\\ s
:rtype: `callable`
"""
cls, attrs = _split_what(what)
cls, names, attrs = _split_what(what)

def include_(attribute, value):
return value.__class__ in cls or attribute in attrs
return (
value.__class__ in cls
or attribute.name in names
or attribute in attrs
)

return include_

Expand All @@ -39,13 +45,18 @@ def exclude(*what):
Exclude *what*.
:param what: What to exclude.
:type what: `list` of classes or `attrs.Attribute`\\ s.
:type what: `list` of classes `type`, field names `str` or
`attrs.Attribute`\\ s.
:rtype: `callable`
"""
cls, attrs = _split_what(what)
cls, names, attrs = _split_what(what)

def exclude_(attribute, value):
return value.__class__ not in cls and attribute not in attrs
return not (
value.__class__ in cls
or attribute.name in names
or attribute in attrs
)

return exclude_
4 changes: 2 additions & 2 deletions src/attr/filters.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ from typing import Any, Union

from . import Attribute, _FilterType

def include(*what: Union[type, Attribute[Any]]) -> _FilterType[Any]: ...
def exclude(*what: Union[type, Attribute[Any]]) -> _FilterType[Any]: ...
def include(*what: Union[type, str, Attribute[Any]]) -> _FilterType[Any]: ...
def exclude(*what: Union[type, str, Attribute[Any]]) -> _FilterType[Any]: ...
19 changes: 18 additions & 1 deletion tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def test_splits(self):
"""
assert (
frozenset((int, str)),
frozenset(("abcd", "123")),
frozenset((fields(C).a,)),
) == _split_what((str, fields(C).a, int))
) == _split_what((str, "123", fields(C).a, int, "abcd"))


class TestInclude:
Expand All @@ -46,6 +47,10 @@ class TestInclude:
((str,), "hello"),
((str, fields(C).a), 42),
((str, fields(C).b), "hello"),
(("a",), 42),
(("a",), "hello"),
(("a", str), 42),
(("a", fields(C).b), "hello"),
],
)
def test_allow(self, incl, value):
Expand All @@ -62,6 +67,10 @@ def test_allow(self, incl, value):
((int,), "hello"),
((str, fields(C).b), 42),
((int, fields(C).b), "hello"),
(("b",), 42),
(("b",), "hello"),
(("b", str), 42),
(("b", fields(C).b), "hello"),
],
)
def test_drop_class(self, incl, value):
Expand All @@ -84,6 +93,10 @@ class TestExclude:
((int,), "hello"),
((str, fields(C).b), 42),
((int, fields(C).b), "hello"),
(("b",), 42),
(("b",), "hello"),
(("b", str), 42),
(("b", fields(C).b), "hello"),
],
)
def test_allow(self, excl, value):
Expand All @@ -100,6 +113,10 @@ def test_allow(self, excl, value):
((str,), "hello"),
((str, fields(C).a), 42),
((str, fields(C).b), "hello"),
(("a",), 42),
(("a",), "hello"),
(("a", str), 42),
(("a", fields(C).b), "hello"),
],
)
def test_drop_class(self, excl, value):
Expand Down

0 comments on commit c37f9f7

Please sign in to comment.