You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
__dir__ on unittest.mock not safe for all spec types #80762
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=Noneclosed_at=Nonecreated_at=<Date2019-04-09.22:46:11.142>labels= ['type-bug', 'library', '3.9', '3.10', '3.11']
title='__dir__ on unittest.mock not safe for all spec types'updated_at=<Date2021-10-18.17:34:19.322>user='https://github.com/dsem'
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__returnsorted(set(extras + from_type + from_dict +TypeError: can only concatenate tuple (not "list") to tuple
spec: This can be either a list of strings or an existing object (a class or instance) that acts as the specification for the mock object. If you pass in an object then a list of strings is formed by calling dir on the object (excluding unsupported magic attributes and methods). Accessing any attribute not in this list will raise an AttributeError.
Docs state it should be list of strings. Can you please link to docs where an iterable of strings is mentioned? A simple patch would be to wrap it inside a list() call but I am not sure of making the interface more relaxed which was documented to accept list of strings. I am adding module mock devs to take a call on this.
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 8684f1dfa5..35dc7b044e 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -679,7 +679,7 @@ class NonCallableMock(Base):
if not FILTER_DIR:
return object.__dir__(self)
- extras = self._mock_methods or []+ extras = list(self._mock_methods or [])
from_type = dir(type(self))
from_dict = list(self.__dict__)
dsem commented on Apr 9, 2019
dsemmannequin
MannequinAuthor
More actions
I agree docs only say "list of strings" and so this isn't a case where there's behavior that contrasts with the documentation. I merely ran into this issue in my project and found it difficult to get to the root of so wanted to fix it upstream. I made a patch and reported this bug only as a formality to accompany my patch.
Support of a tuple of strings was never documented, although it worked for most parts except dir(). But since it was not documented, we can consider this as adding a new feature.
changed the title [-]__dir__ on unittest.mock not safe for all spec types[/-][+]`__dir__` on unittest.mock not safe for all spec types[/+]on Aug 12, 2026
Activity
dsem commentedon Apr 9, 2019
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
tirkarthi commentedon Apr 9, 2019
https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock
Docs state it should be list of strings. Can you please link to docs where an iterable of strings is mentioned? A simple patch would be to wrap it inside a list() call but I am not sure of making the interface more relaxed which was documented to accept list of strings. I am adding module mock devs to take a call on this.
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 8684f1dfa5..35dc7b044e 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -679,7 +679,7 @@ class NonCallableMock(Base):
if not FILTER_DIR:
return object.__dir__(self)
dsem commentedon Apr 9, 2019
I agree docs only say "list of strings" and so this isn't a case where there's behavior that contrasts with the documentation. I merely ran into this issue in my project and found it difficult to get to the root of so wanted to fix it upstream. I made a patch and reported this bug only as a formality to accompany my patch.
iritkatriel commentedon Oct 18, 2021
Changing type as crash typically refers to segfault or hang or the like, and not an exception.
Reproduced on 3.11.
serhiy-storchaka commentedon Aug 12, 2026
Support of a tuple of strings was never documented, although it worked for most parts except
dir(). But since it was not documented, we can consider this as adding a new feature.gh-80762: Fix dir() on Mock with tuple specs (GH-12753)
[-]__dir__ on unittest.mock not safe for all spec types[/-][+]`__dir__` on unittest.mock not safe for all spec types[/+]pythongh-80762: Fix dir() on Mock with tuple specs (pythonGH-12753)