Skip to content

Conversation

brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented Mar 2, 2020

@brandtbucher brandtbucher requested a review from rhettinger as a code owner March 2, 2020 05:36
@brandtbucher brandtbucher added the type-feature A feature request or enhancement label Mar 2, 2020
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

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"})
Copy link
Member

Choose a reason for hiding this comment

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

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

Copy link
Member Author

@brandtbucher brandtbucher Mar 2, 2020

Choose a reason for hiding this comment

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

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):
Copy link
Member

Choose a reason for hiding this comment

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

I see no tests for ‘|=‘?

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

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

@codecov
Copy link

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

Choose a reason for hiding this comment

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

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. :-(

Comment on lines 191 to 193
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)
Copy link
Member

Choose a reason for hiding this comment

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

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)
Copy link
Member

Choose a reason for hiding this comment

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

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);
Copy link
Member

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

Thanks, much more readable!

@brandtbucher
Copy link
Member Author

I agree, thanks!

@gvanrossum
Copy link
Member

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.

@brandtbucher brandtbucher deleted the 584-defaultdict branch July 21, 2022 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants