[New-bugs-announce] [issue36581] __dir__ on unittest.mock not safe for all spec types

Dylan Semler report at bugs.python.org
Tue Apr 9 18:46:11 EDT 2019


New submission from Dylan Semler <dylan.semler at gmail.com>:

If a MagicMock is created with a spec or spec_set that is a non-list iterable of strings (like a tuple), calling dir() on said mock produces a Traceback.  Here's a minimum example:

🡒 cat poc.py
from unittest.mock import MagicMock

mock = MagicMock(spec=('a', 'tuple'))
dir(mock)

🡒 python3 poc.py 
Traceback (most recent call last):
  File "poc.py", line 4, in <module>
    dir(mock)
  File "/usr/lib64/python3.6/unittest/mock.py", line 677, in __dir__
    return sorted(set(extras + from_type + from_dict +
TypeError: can only concatenate tuple (not "list") to tuple

----------
components: Library (Lib)
messages: 339813
nosy: Dylan Semler
priority: normal
severity: normal
status: open
title: __dir__ on unittest.mock not safe for all spec types
type: crash
versions: Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36581>
_______________________________________


More information about the New-bugs-announce mailing list