Skip to content
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

Fix nested all include exclude #1588

Merged
merged 3 commits into from Jul 11, 2020

Conversation

xspirus
Copy link
Contributor

@xspirus xspirus commented Jun 2, 2020

Change Summary

Fix the behavior of __all__ key, when used in conjunction with index keys in advanced exclude/include of fields that are sequences.

Related issue number

Fixes #1579

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)

@xspirus xspirus force-pushed the fix-nested-all-include-exclude branch from 5bfbf42 to 067f6c7 Compare June 2, 2020 07:56
@codecov
Copy link

codecov bot commented Jun 2, 2020

Codecov Report

Merging #1588 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1588   +/-   ##
=======================================
  Coverage   99.94%   99.94%           
=======================================
  Files          21       21           
  Lines        3863     3895   +32     
  Branches      767      784   +17     
=======================================
+ Hits         3861     3893   +32     
  Misses          1        1           
  Partials        1        1           
Impacted Files Coverage Δ
pydantic/utils.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f977709...9b0e8bb. Read the comment docs.

@xspirus xspirus force-pushed the fix-nested-all-include-exclude branch 3 times, most recently from cce334a to b9c83e1 Compare June 2, 2020 14:08
Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks good, but it's pretty complex, I'll need to go through it again once these suggestions are done.

@@ -0,0 +1,2 @@
Fix behavior of `__all__` key when used in conjunction with index keys in advanced include/exclude of fields that are
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one line please.

pydantic/utils.py Show resolved Hide resolved
all_set = items.get('__all__')
if all_set:
if all_items:
default: Type[Union[set, dict]] # type: ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default: Type[Union[set, dict]] # type: ignore
default: Type[Union[Set[Any, Dict[Any, Any]]

Maybe?

elif isinstance(all_items, AbstractSet):
default = set
else:
default = ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not clear about the significance of the ellipsis here?


m = Model(
subs=[
SubModel(k=1, subsubs=[SubSubModel(i=1, j=1), SubSubModel(i=2, j=2)]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's clear and more consistent with other tests if you use dicts, not models when creating dicst,

Suggested change
SubModel(k=1, subsubs=[SubSubModel(i=1, j=1), SubSubModel(i=2, j=2)]),
dict(k=1, subsubs=[dict(i=1, j=1), dict(i=2, j=2)]),

etc.

'subs': [{'k': 1, 'subsubs': []}, {'k': 2, 'subsubs': []}]
}
# Merge sub dict-set
assert m.dict(exclude={'subs': {'__all__': {'subsubs': {0: {'i'}}}, 0: {'subsubs': {1}}}}) == {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you could parameterise these?

)

# Normal nested __all__
assert m.dict(include={'subs': {'__all__': {'subsubs': {'__all__': {'i'}}}}}) == {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and again, I think this should be parameterised.

@xspirus xspirus force-pushed the fix-nested-all-include-exclude branch from 3dcf327 to d784445 Compare June 27, 2020 14:37
@xspirus
Copy link
Contributor Author

xspirus commented Jun 27, 2020

@samuelcolvin Thanks for the review. I hope the docstring makes it a bit clearer on what is going on. Updated the tests as well.

This commit also fixes some weird cases in the recursive
`update_normalized_all` call and Ellipsis values.
@xspirus xspirus force-pushed the fix-nested-all-include-exclude branch from d784445 to 9b0e8bb Compare July 10, 2020 18:08
@samuelcolvin samuelcolvin merged commit 1f4ecd0 into pydantic:master Jul 11, 2020
@xspirus xspirus deleted the fix-nested-all-include-exclude branch July 11, 2020 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exclude with nested "__all__" does not work as expected
2 participants