-
-
Notifications
You must be signed in to change notification settings - Fork 369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add literal string support to include and exclude filters #1068
Add literal string support to include and exclude filters #1068
Conversation
It's not really equivalent though. See this example with a typo: from attrs import asdict, define, fields
from attrs.filters import exclude
@define
class User:
id: str
password: str
data = User("id", "s3cret")
fs = fields(User)
asdict(data, filter=exclude("passwrd"))
asdict(data, filter=exclude(fs.passwrd)) When passing in a string, the typo goes unnoticed and the password field is serialized. |
@Tinche Thanks for your reply 😄. Yeah, you're right, Could we document this potential consequence explicitly to warn users? IMHO, it probably depends on whether I also have an extreme radical thought that perhaps I could implement a generic type signature in |
fcb007e
to
f609858
Compare
Rebase to the latest head ref |
c30f382
to
d22135b
Compare
Rebase from head ref |
Add a note to document incorrect typo behaviors while using literal string in filters
|
@hynek Could you let actions run again? Or you may have other suggestions? Thanks :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have left some comments.
@chrysle Thank you for review. Updated. |
hynek's comments
@hynek Thanks for your advices. Updated |
docs/examples.md
Outdated
@@ -240,8 +241,34 @@ For the common case where you want to [`include`](attrs.filters.include) or [`ex | |||
>>> asdict(C("foo", "2", 3), | |||
... filter=filters.include(int, fields(C).x)) | |||
{'x': 'foo', 'z': 3} | |||
|
|||
>>> asdict(C("foo", "2", 3), | |||
... filter=filters.include(fields(C).x), "z") |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
Co-authored-by: chrysle <fritzihab@posteo.de>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this should be fine now.
Summary
It's a little too cumbersome that helper util
filters
only acceptstype
andAttribute
as input, if there are too many fields to include or exclude usingasdict
.For example,
This PR adds literal string name of
Attribute
as input argument toexclude
andinclude
which provides a more convenience approach for usages of filter. The following codes are similar to the above:And typo issues of input arguments wouldn't cause extra side effects.
This's my first PR to
attrs
, please feel free to inform me where I'm wrong and what I missed. Thanks!Pull Request Check List
Our CI fails if coverage is not 100%.
.pyi
).tests/typing_example.py
.attr/__init__.pyi
, they've also been re-imported inattrs/__init__.pyi
.docs/api.rst
by hand.@attr.s()
have to be added by hand too.versionadded
,versionchanged
, ordeprecated
directives.Find the appropriate next version in our
__init__.py
file..rst
files is written using semantic newlines.changelog.d
.