Skip to content

Commit

Permalink
Add PyFunction and PyVariable; directives for python functions and va…
Browse files Browse the repository at this point in the history
…riables
  • Loading branch information
tk0miya committed Apr 13, 2019
1 parent a337cb7 commit 6af3896
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Deprecated
* ``sphinx.directives.TocTree``
* ``sphinx.directives.VersionChange``
* ``sphinx.domains.python.PyClassmember``
* ``sphinx.domains.python.PyModulelevel``
* ``sphinx.domains.std.StandardDomain._resolve_citation_xref()``
* ``sphinx.domains.std.StandardDomain.note_citations()``
* ``sphinx.domains.std.StandardDomain.note_citation_refs()``
Expand Down
10 changes: 9 additions & 1 deletion doc/extdev/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,17 @@ The following is a list of deprecated interfaces.
- 4.0
- ``sphinx.domains.python.PyAttribute``,
``sphinx.domains.python.PyMethod``,
``sphinx.domains.python.PyClassMethod`` and
``sphinx.domains.python.PyClassMethod``,
``sphinx.domains.python.PyObject`` and
``sphinx.domains.python.PyStaticMethod``

* - ``sphinx.domains.python.PyModulelevel``
- 2.1
- 4.0
- ``sphinx.domains.python.PyFunction``,
``sphinx.domains.python.PyObject`` and
``sphinx.domains.python.PyVariable``

* - ``sphinx.domains.std.StandardDomain._resolve_citation_xref()``
- 2.1
- 4.0
Expand Down
39 changes: 37 additions & 2 deletions sphinx/domains/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ class PyModulelevel(PyObject):
Description of an object on module level (functions, data).
"""

def run(self):
# type: () -> List[nodes.Node]
warnings.warn('PyClassmember is deprecated.',
RemovedInSphinx40Warning)

return super().run()

This comment has been minimized.

Copy link
@blueyed

blueyed Mar 3, 2020

Contributor

warnings.warn('PyClassmember is deprecated.',

@tk0miya
I'm a bit confused about this warning. which is on PyModulelevel - and there is the same on PyClassmember (

def run(self) -> List[Node]:
warnings.warn('PyClassmember is deprecated.',
RemovedInSphinx40Warning)
)
Given the commit it should say "PyModulelevel" probably?

But it/they should also have a stacklevel probably?

This comment has been minimized.

Copy link
@tk0miya

tk0miya Mar 4, 2020

Author Member

You're right. This is a typo. And should have a stacklevel.

This comment has been minimized.

Copy link
@tk0miya

tk0miya Mar 4, 2020

Author Member

I'll fix it in later (maybe tomorrow).

This comment has been minimized.

Copy link
@tk0miya

tk0miya Mar 5, 2020

Author Member

I noticed adding stacklevel is meaningless for this case. It only points statemachine of reST parser... So I fixed only typo: #7252


def needs_arglist(self):
# type: () -> bool
return self.objtype == 'function'
Expand All @@ -428,6 +435,34 @@ def get_index_text(self, modname, name_cls):
return ''


class PyFunction(PyObject):
"""Description of a function."""

def needs_arglist(self):
# type: () -> bool
return True

def get_index_text(self, modname, name_cls):
# type: (str, Tuple[str, str]) -> str
name, cls = name_cls
if modname:
return _('%s() (in module %s)') % (name, modname)
else:
return _('%s() (built-in function)') % name


class PyVariable(PyObject):
"""Description of a variable."""

def get_index_text(self, modname, name_cls):
# type: (str, Tuple[str, str]) -> str
name, cls = name_cls
if modname:
return _('%s (in module %s)') % (name, modname)
else:
return _('%s (built-in variable)') % name


class PyClasslike(PyObject):
"""
Description of a class-like object (classes, interfaces, exceptions).
Expand Down Expand Up @@ -839,8 +874,8 @@ class PythonDomain(Domain):
} # type: Dict[str, ObjType]

directives = {
'function': PyModulelevel,
'data': PyModulelevel,
'function': PyFunction,
'data': PyVariable,
'class': PyClasslike,
'exception': PyClasslike,
'method': PyMethod,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_domain_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,29 @@ def test_pyobject_prefix(app):
assert doctree[1][1][3].astext().strip() == 'FooBar.say' # not stripped


def test_pydata(app):
text = ".. py:data:: var\n"
domain = app.env.get_domain('py')
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, desc_name, "var"],
[desc_content, ()])]))
assert 'var' in domain.objects
assert domain.objects['var'] == ('index', 'data')


def test_pyfunction(app):
text = ".. py:function:: func\n"
domain = app.env.get_domain('py')
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, ([desc_name, "func"],
[desc_parameterlist, ()])],
[desc_content, ()])]))
assert 'func' in domain.objects
assert domain.objects['func'] == ('index', 'function')


def test_pymethod(app):
text = (".. py:class:: Class\n"
"\n"
Expand Down

0 comments on commit 6af3896

Please sign in to comment.