My favorites | Sign in
Project Home Downloads Issues Source
New issue   Search
  Advanced search   Search tips   Subscriptions
Issue 157: Autospeccing should not copy spec from class to instance
1 person starred this issue and may be notified of changes. Back to list
Status:  New
Owner:  ----

Sign in to add a comment
Reported by, May 11, 2012

I tried to mock django-form class, and then it's instance access to .cleaned_data would throw an AttributeError.

Since most of object-attributes are added in __init__, I find that strange.

Is it on purpose? Even if yes -- I velieve we should change that. Thanks.
May 11, 2012
p.s.: as in past, I find that the best thing on this would be to remember spec of class-functions, but disable attributeerror on non-existing attributes access.
May 11, 2012
Project Member #2 fuzzyman
Well, it is deliberate - and even documented as one of the drawbacks of auto-speccing:

Not throwing AttributeError on unknown attributes would negate what is really the *main point* of speccing - that it restricts the attributes available on mock objects. 
May 11, 2012
Ok, after some thoughts, I tend to agree here, actually. Maybe docs should be added. My main points are:

1. if it's a rare case -- you should just add to-be-known attributes to your mock object. like
a_M = create_autospec(A); a_M.cleaned_data = MagicMock()

2. generally (not only in python), there should be value-objects and service-objects (which in python are mostly just functions), and value-objects should always be easy to construct/create and avoid doing something in __init__(). for hard constructing there's fabrics, and for doing something there are methods.

The problem is that many frameworks / libs do not go with this methodics, but:
1. it's a question to library / framework authors
2. mock with autospec still lets you add attributes, so that solves everything.
May 11, 2012
Project Member #4 fuzzyman
RIght, I agree with you that this limitation makes auto-speccing less generally useful than it might be - and that attaching manually created mocks as attributes is probably the best workaround. I can add this to the docs.
Sign in to add a comment

Powered by Google Project Hosting