Visitar URL original
bpo-36144: Implement defaultdict union. by brandtbucher · Pull Request #18729 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20210509163648/https://github.com/python/cpython/pull/18729
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

bpo-36144: Implement defaultdict union. #18729

Merged
merged 8 commits into from Mar 6, 2020

Conversation

@brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented Mar 2, 2020 •

Copy link
Member

@gvanrossum gvanrossum left a comment

I don’t recall how ‘|=‘ is implemented — does it have some default behavior if ‘|’ is defined?

s = defaultdict(str, {0: "zero", 1: "one"})

self.assertIs((i | s).default_factory, int)
self.assertDictEqual(i | s, {1: "one", 2: 2, 0: "zero"})

This comment has been minimized.

@gvanrossum

gvanrossum Mar 2, 2020
Member

Is defaultdict not order-preserving, like dict? If it is, could the test check it?

This comment has been minimized.

@brandtbucher

brandtbucher Mar 2, 2020 •
Author Member

You found a flaw in my merge logic! I pushed a fix and added tests for key order.

@@ -183,5 +183,30 @@ def test_pickling(self):
o = pickle.loads(s)
self.assertEqual(d, o)

def test_union(self):

This comment has been minimized.

@gvanrossum

gvanrossum Mar 2, 2020
Member

I see no tests for ‘|=‘?

This comment has been minimized.

@brandtbucher

brandtbucher Mar 2, 2020
Author Member

defaultdict inherits a perfectly fine |= from dict.

I can add tests for it here if you'd like, but the tests for dict already have the same coverage.

This comment has been minimized.

@gvanrossum

gvanrossum Mar 3, 2020
Member

D'oh. Maybe add a comment to this effect?

@codecov
Copy link

@codecov codecov bot commented Mar 2, 2020 •

Codecov Report

Merging #18729 into master will increase coverage by 1.15%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##           master   #18729       +/-   ##
===========================================
+ Coverage   82.06%   83.22%    +1.15%     
===========================================
  Files        1956     1571      -385     
  Lines      589514   415419   -174095     
  Branches    44464    44488       +24     
===========================================
- Hits       483809   345733   -138076     
+ Misses      96052    60034    -36018     
+ Partials     9653     9652        -1     
Impacted Files Coverage Δ
Lib/distutils/tests/test_bdist_rpm.py 30.00% <0.00%> (-65.00%) ⬇️
Lib/distutils/command/bdist_rpm.py 7.63% <0.00%> (-56.88%) ⬇️
Lib/test/test_urllib2net.py 76.92% <0.00%> (-13.85%) ⬇️
Lib/test/test_smtpnet.py 78.57% <0.00%> (-7.15%) ⬇️
Lib/ftplib.py 63.85% <0.00%> (-6.06%) ⬇️
Lib/test/test_ftplib.py 87.11% <0.00%> (-4.72%) ⬇️
Tools/scripts/db2pickle.py 17.82% <0.00%> (-3.97%) ⬇️
Tools/scripts/pickle2db.py 16.98% <0.00%> (-3.78%) ⬇️
Lib/test/test_socket.py 71.94% <0.00%> (-3.77%) ⬇️
Lib/test/test_asyncio/test_base_events.py 91.84% <0.00%> (-3.30%) ⬇️
... and 458 more

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 eb8ac57...1662573. Read the comment docs.

Copy link
Member

@gvanrossum gvanrossum left a comment

Sorry for being so nitpicky. Also, I didn't even realize I'd found a logic error in your code. Shows you what I know. :-(

self.assertDictEqual(i | s, {1: "one", 2: 2, 0: "zero"})
self.assertEqual(list(i | s), [1, 2, 0])
self.assertIs((s | i).default_factory, str)
Comment on lines 191 to 193

This comment has been minimized.

@gvanrossum

gvanrossum Mar 3, 2020
Member

Maybe introduce an intermediate variable, e.g. i_s = i | s to make it abundantly clear that these three assertions are about the same result? Etc.

@@ -1990,6 +1990,13 @@ defdict_missing(defdictobject *dd, PyObject *key)
return value;
}

static inline PyObject*
defdict_copy_with_map(defdictobject *dd, PyObject *map)

This comment has been minimized.

@gvanrossum

gvanrossum Mar 3, 2020
Member

map is a slightly confusing or over-specified name for the argument -- it really represents anything you can pass to dict(), so it could be a mapping (we don't call those maps except in some internals I believe) or an iterable of (key, value) pairs. Maybe just arg?

Also the name of the function is getting awkward. I wonder if it should just be called "new_defdict"? (Arguably the caller could pass in the default factory and the logic here could just replace NULL with Py_None?)

static PyObject*
defdict_or(PyObject* left, PyObject* right)
{
int lhs = PyObject_IsInstance(left, (PyObject*)&defdict_type);

This comment has been minimized.

@gvanrossum

gvanrossum Mar 3, 2020
Member

Sorry about harping on names! To me, lhs is a noun, but this variable is a condition -- the meaning of the condition seems to be the negation of "should the arguments be swapped". If it weren't for the negation I'd name it "swap" -- but given that, maybe "left_is_self"?

Copy link
Member

@gvanrossum gvanrossum left a comment

Thanks, much more readable!

@gvanrossum gvanrossum merged commit 57c9d17 into python:master Mar 6, 2020
9 checks passed
9 checks passed
@github-actions
Docs
Details
@github-actions
Windows (x86)
Details
@github-actions
Windows (x64)
Details
@github-actions
macOS
Details
@github-actions
Ubuntu
Details
Azure Pipelines PR #20200306.28 succeeded
Details
@bedevere-bot
bedevere/issue-number Issue number 36144 found
Details
@bedevere-bot
bedevere/news News entry found in Misc/NEWS.d
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@brandtbucher
Copy link
Member Author

@brandtbucher brandtbucher commented Mar 6, 2020

I agree, thanks!

@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Mar 6, 2020

Oh, an incredibly minor piece of feedback: in the Subject/Title/Whatever line of commits, issues, PRs and the like, we prefer no final period. These things are like headlines in a newspaper. I don't know it's in the dev guide, but I'm sure I've seen this advice elsewhere, and it's a common convention.

vegerot added a commit to vegerot/cpython that referenced this pull request Jun 10, 2020
For PEP 585 (this isn't in the PEP but is an obvious follow-up).
vegerot added a commit to vegerot/cpython that referenced this pull request Jun 10, 2020
For PEP 585 (this isn't in the PEP but is an obvious follow-up).
gmelikov added a commit to gmelikov/cpython that referenced this pull request Aug 22, 2020
For PEP 585 (this isn't in the PEP but is an obvious follow-up).
chrisburr added a commit to chrisburr/cpython that referenced this pull request Dec 9, 2020
For PEP 585 (this isn't in the PEP but is an obvious follow-up).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants