Skip to content

support reading Argparse instances inside classes #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions sphinxarg/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,18 @@ def run(self):

# Skip this if we're dealing with a local file, since it obviously can't be imported
if 'filename' not in self.options:
_elements = attr_name.split('.')
try:
mod = __import__(module_name, globals(), locals(), [attr_name])
mod = __import__(module_name, globals(), locals(), [_elements[0]])
except ImportError:
raise self.error(f'Failed to import "{attr_name}" from "{module_name}".\n{sys.exc_info()[1]}')

if not hasattr(mod, attr_name):
raise self.error(('Module "%s" has no attribute "%s"\nIncorrect argparse :module: or :func: values?') % (module_name, attr_name))
func = getattr(mod, attr_name)
raise self.error(f'Failed to import "{_elements[0]}" from "{module_name}".\n{sys.exc_info()[1]}')
obj = mod
for i in _elements:
if not hasattr(obj, i):
raise self.error(f'"{obj}" has no attribute "{i}"\n'
'Incorrect argparse :module: or :func: values?')
obj = getattr(obj, i)
func = obj

if isinstance(func, ArgumentParser):
parser = func
Expand Down