0

I am using Sphinx v1.8.5 to build documentation for Python 2 code that has external dependencies that I mock using autodoc_mock_imports. I get an error message when I try to use the mocked object (RAMP) as a default argument:

def foo(name, amp=RAMP):

The error says:

NameError: name 'RAMP' is not defined

If I do not use RAMP as a default argument, the error disappears. What am I doing wrong?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
honey_badger
  • 472
  • 3
  • 10
  • 1
    Please edit the question and explicitly include the code where you are defining the mock. Also show an example how RAMP is imported and any other uses of it in the module. Is the default value in the argument the only place where an error occurs? – bad_coder Dec 17 '20 at 23:21
  • Yep, RAMP is defined in the module `step`, which is imported without `from step import *`, but regular `import step`. So setting `step.RAMP` as the default value resolves the issue. Should have been more attentive. Thank you for your time! – honey_badger Dec 18 '20 at 08:49
  • 1
    You should self-accept the answer to indicate the problem was solved. It's important to signal for members searching questions to know if they are solved or not. – bad_coder Dec 18 '20 at 13:24
  • 1
    Thanks. I can only do it after 2 days, 7 hours to go :). – honey_badger Dec 19 '20 at 14:06

1 Answers1

1

Ok, so thanks to the comment of @bad_coder I realised that the hurdle was stemming from how the modules were imported. I needed to resolve the scope with module_defining_RAMP.RAMP instead of directly accessing RAMP.

Of course, then I got the ugly value of the mocked object in the output of my documentation:

foo(name, amp=<sphinx.ext.autodoc.importer._MockObject object>)

but I have already seen the solution to this problem while searching for my own. I needed to simply state the function signature in the doc string:

def foo(name, amp=RAMP):
"""
foo(name, amp=module_defining_RAMP.RAMP)
"""
honey_badger
  • 472
  • 3
  • 10