Chilipp / docrep

A Python Module for intelligent reuse of docstrings
Apache License 2.0
30 stars 4 forks source link

Python 2: for class docstrings '__doc__' of 'type' objects is not writable #6

Closed lesteve closed 6 years ago

lesteve commented 6 years ago

Snippet reproducing the problem:

import docrep

docstrings = docrep.DocstringProcessor()

@docstrings.get_sectionsf('Base')
class Base(object):
    """ Base short description

    Base long description

    Parameters
    ----------
    param1 : int
    param2 : int
    """
    def __init__(self, param1, param2):
        pass

@docstrings.with_indent(4)
class Derived(Base):
    """ Derived short description.

    Derived long description.
    %(Base.parameters)s
    """
    pass

Full traceback:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/test-docrep.py in <module>()
     20 
     21 @docstrings.with_indent(4)
---> 22 class Derived(Base):
     23     """ Derived short description.
     24 

/home/local/lesteve/miniconda3/envs/scratch27/lib/python2.7/site-packages/docrep/__init__.pyc in replace(func)
    374                 func = func.im_func
    375             func.__doc__ = func.__doc__ and self.with_indents(
--> 376                 func.__doc__, indent=indent, stacklevel=4)
    377             return func
    378         return replace

AttributeError: attribute '__doc__' of 'type' objects is not writable

We found a work-around of doing something like this instead:


class Derived(Base):
   __doc__ = docstrings.with_indents(""" Derived class etc ... """)

Is that what the recommended approach?

Chilipp commented 6 years ago

Hi! Indeed, that is one solution. The other ones are mentioned in issue https://github.com/Chilipp/docrep/issues/5#issuecomment-382158083

Maybe I should include that in the docs... 😉

lesteve commented 6 years ago

Sorry for the noise, I did not see #5, closing. These two issues are coming from the same project (dask-jobqueue if you are curious).