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

MappingProxyType accepts string #87994

Closed
andy-maier mannequin opened this issue Apr 13, 2021 · 3 comments
Closed

MappingProxyType accepts string #87994

andy-maier mannequin opened this issue Apr 13, 2021 · 3 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@andy-maier
Copy link
Mannequin

andy-maier mannequin commented Apr 13, 2021

BPO 43828
Nosy @rhettinger, @andy-maier

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-04-14.03:09:09.323>
created_at = <Date 2021-04-13.08:11:21.863>
labels = ['invalid', 'type-bug', 'library', '3.9']
title = 'MappingProxyType accepts string'
updated_at = <Date 2021-04-14.05:48:35.441>
user = 'https://github.com/andy-maier'

bugs.python.org fields:

activity = <Date 2021-04-14.05:48:35.441>
actor = 'andymaier'
assignee = 'none'
closed = True
closed_date = <Date 2021-04-14.03:09:09.323>
closer = 'rhettinger'
components = ['Library (Lib)']
creation = <Date 2021-04-13.08:11:21.863>
creator = 'andymaier'
dependencies = []
files = []
hgrepos = []
issue_num = 43828
keywords = []
message_count = 3.0
messages = ['390935', '391027', '391036']
nosy_count = 2.0
nosy_names = ['rhettinger', 'andymaier']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue43828'
versions = ['Python 3.9']

@andy-maier
Copy link
Mannequin Author

andy-maier mannequin commented Apr 13, 2021

MappingProxyType objects can currently be initialized from a string object. Given is purpose, I think this is a bug and should result in TypeError being raised.

Example code (on CPython 3.9.1):

>>> from types import MappingProxyType
>>> mp = MappingProxyType('abc')
>>> list(mp)
['a', 'b', 'c']
>>> mp.items()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'items'

Other invalid input types are properly checked:

>>> mp = MappingProxyType(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: mappingproxy() argument must be a mapping, not int

>>> mp = MappingProxyType(['a'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: mappingproxy() argument must be a mapping, not list

>>> mp = MappingProxyType(('a',))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: mappingproxy() argument must be a mapping, not tuple

@andy-maier andy-maier mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 13, 2021
@rhettinger
Copy link
Contributor

This isn't a bug because the MappingProxy doesn't promise to do early detection and because it can't really be accomplished cleanly.

Perfect detection of non-mappings isn't possible. Some easy, minimal checks are made for early detection using PyMapping_Check, PyList_Check, and PyTuple_Check. Anything that gets past those checks will just need to be duck-typed downstream when it is used.

We could add a few other specific exclusions but that isn't a general solution and it slows down instantiation. Further it could break code that is intended to work (subclasses of str that mapping methods) or that just happen to work:

    >>> from types import MappingProxyType
    >>> mp = MappingProxyType('abc')
    >>> mp[0]
    'a'

@andy-maier
Copy link
Mannequin Author

andy-maier mannequin commented Apr 14, 2021

I accept that the issue was closed, but wanted to document some things:

  1. The dict class manages very well to detect that a string is invalid input:
>>> d = dict('abc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 1; 2 is required
  1. When initialized with strings, it looses some of its dictionary methods, but does a quite reasonable job in pointing that out in the error message:
>>> mp = MappingProxyType('abc')
>>> mp.items()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'items'

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant