Skip to content

Commit

Permalink
Add docs and changelog for new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lqhuang committed Apr 12, 2023
1 parent c37f9f7 commit 639ed1b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/1068.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
filters ``exclude`` and ``include`` now support to pass literal name of attribtes in ``str`` format
6 changes: 3 additions & 3 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ For that, {func}`attrs.asdict` offers a callback that decides whether an attribu
{'users': [{'email': 'jane@doe.invalid'}, {'email': 'joe@doe.invalid'}]}
```

For the common case where you want to [`include`](attrs.filters.include) or [`exclude`](attrs.filters.exclude) certain types or attributes, *attrs* ships with a few helpers:
For the common case where you want to [`include`](attrs.filters.include) or [`exclude`](attrs.filters.exclude) certain types, string name or attributes, *attrs* ships with a few helpers:

```{doctest}
>>> from attrs import asdict, filters, fields
Expand All @@ -228,7 +228,7 @@ For the common case where you want to [`include`](attrs.filters.include) or [`ex
>>> asdict(
... User("jane", "s33kred", 42),
... filter=filters.exclude(fields(User).password, int))
... filter=filters.exclude(fields(User).password, int, "password"))
{'login': 'jane'}
>>> @define
Expand All @@ -238,7 +238,7 @@ For the common case where you want to [`include`](attrs.filters.include) or [`ex
... z: int
>>> asdict(C("foo", "2", 3),
... filter=filters.include(int, fields(C).x))
... filter=filters.include(int, fields(C).x, "x"))
{'x': 'foo', 'z': 3}
```

Expand Down
4 changes: 4 additions & 0 deletions src/attr/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def include(*what):
`attrs.Attribute`\\ s
:rtype: `callable`
.. versionchanged:: 22.2.0 Accept field name string as input argument
"""
cls, names, attrs = _split_what(what)

Expand All @@ -49,6 +51,8 @@ def exclude(*what):
`attrs.Attribute`\\ s.
:rtype: `callable`
.. versionchanged:: 22.2.0 Accept field name string as input argument
"""
cls, names, attrs = _split_what(what)

Expand Down
2 changes: 2 additions & 0 deletions tests/typing_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def accessing_from_attr() -> None:
attr.converters.optional
attr.exceptions.FrozenError
attr.filters.include
attr.filters.exclude
attr.setters.frozen
attr.validators.and_
attr.cmp_using
Expand All @@ -453,6 +454,7 @@ def accessing_from_attrs() -> None:
attrs.converters.optional
attrs.exceptions.FrozenError
attrs.filters.include
attr.filters.exclude
attrs.setters.frozen
attrs.validators.and_
attrs.cmp_using
Expand Down

0 comments on commit 639ed1b

Please sign in to comment.