This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: collections.abc.MappingView mixins rely on undocumented _mapping
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.9, Python 3.8, Python 3.7, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, gsnedders, rhettinger
Priority: normal Keywords:

Created on 2019-06-03 16:48 by gsnedders, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (9)
msg344447 - (view) Author: Geoffrey Sneddon (gsnedders) * Date: 2019-06-03 16:48
The mixin methods on MappingView and its subclasses all rely on the undocumented MappingView._mapping (set in the undocumented MappingView.__init__).

This should probably be documented at least insofar as Set._from_iterable is.
msg344451 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-06-03 17:16
The _mapping attribute is a private implementation detail and should remain undocumented.  A user of mapping views creates that attribute via the __init__() method.

In contrast, _from_iterable is explicitly meant to be overridden because it is the only way to control object creation.

Thank you for the suggestion.
msg344480 - (view) Author: Geoffrey Sneddon (gsnedders) * Date: 2019-06-03 21:35
How do you use ItemsView without:

 * relying on __init__ and _mapping, which are both private implementation details, or
 * reimplementing __len__, __contains__, and __iter__?

Given you can't use the mixin implementations of __len__, __contains__, and __iter__ without relying on private implementation details, should they actually be considered mixin implementations? Shouldn't they just be abstract methods given you can't actually use them?
msg344532 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-06-04 08:39
The __init__ method is public.  Here is how to use ItemsView:

>>> from collections.abc import ItemsView
>>> d = dict(python='snake', ruby='gem', go='board game', c='speed of light')
>>> iv = ItemsView(d)
>>> ('python', 'snake') in iv
True
>>> list(iv)
[('python', 'snake'), ('ruby', 'gem'), ('go', 'board game'), ('c', 'speed of light')]
>>> len(iv)
4
>>> d['python'] = 'language'
>>> list(iv)
[('python', 'language'), ('ruby', 'gem'), ('go', 'board game'), ('c', 'speed of light')]

Note, there is no direct access to _mapping.
msg344546 - (view) Author: Geoffrey Sneddon (gsnedders) * Date: 2019-06-04 11:09
Then I guess what I consider a bug is "__init__ is undocumented".
msg344617 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-06-04 17:47
> Then I guess what I consider a bug is "__init__ is undocumented".

No, this just how Python works.
msg344619 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-06-04 17:56
Hit <submit> too early.

In Python, the norm is that the class name is documented.  When you call the class, the __init__() method gets called automatically (as documented in the language reference and in the tutorial).

For example:

    >>> class A:
            def __init__(self, seq):
                    self._seq = seq
            def __len__(self):
                    return len(self._seq)
      
    >>> a = A('apple')
    >>> len(a)	  
    5

In this example, we document that class "A" can be called with a sequence and that len() can be called on instances of A.  The "dunder" methods are public, but are called and documented indirectly.  The "_seq" attribute is marked as private and would not be documented, since it is an implementation detail and not intended to be accessed directly.
msg344633 - (view) Author: Geoffrey Sneddon (gsnedders) * Date: 2019-06-04 19:19
You've missed my point: the arguments that MappingView.__init__ takes are undocumented. Nowhere mentions class MappingView(mapping), as I would expect from how initializers are documented elsewhere.
msg344634 - (view) Author: Geoffrey Sneddon (gsnedders) * Date: 2019-06-04 19:20
Like, where in the documentation can I learn that MappingView's initializer takes an argument "mapping"?
History
Date User Action Args
2022-04-11 14:59:16adminsetgithub: 81326
2019-06-04 19:20:29gsnedderssetmessages: + msg344634
2019-06-04 19:19:55gsnedderssetmessages: + msg344633
2019-06-04 17:56:06rhettingersetmessages: + msg344619
2019-06-04 17:47:39rhettingersetmessages: + msg344617
2019-06-04 11:09:30gsnedderssetmessages: + msg344546
2019-06-04 08:39:59rhettingersetmessages: + msg344532
2019-06-03 21:35:29gsnedderssetmessages: + msg344480
2019-06-03 17:16:22rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg344451

stage: resolved
2019-06-03 17:00:53xtreaksetnosy: + rhettinger

versions: - Python 3.5, Python 3.6
2019-06-03 16:48:36gsnedderscreate