Skip to content

Commit

Permalink
Parameterizing "self" reference in transform functions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpmcconnell committed Feb 21, 2021
1 parent d1dae03 commit ef973ef
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions sphinxcontrib/prettyspecialmethods.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def patch_node(node, text=None, children=None, *, constructor=None):


def function_transformer(new_name):
def xf(name_node, parameters_node):
def xf(name_node, parameters_node, selfref):
return (
patch_node(name_node, new_name, ()),
patch_node(parameters_node, '', [
SphinxNodes.desc_parameter('', 'self'),
SphinxNodes.desc_parameter('', selfref),
*parameters_node.children,
])
)
Expand All @@ -54,20 +54,20 @@ def xf(name_node, parameters_node):


def unary_op_transformer(op):
def xf(name_node, parameters_node):
def xf(name_node, parameters_node, selfref):
return (
patch_node(name_node, op, ()),
emphasis('', 'self'),
emphasis('', selfref),
)

return xf


def binary_op_transformer(op):
def xf(name_node, parameters_node):
def xf(name_node, parameters_node, selfref):
return inline(
'', '',
emphasis('', 'self'),
emphasis('', selfref),
Text(' '),
patch_node(name_node, op, ()),
Text(' '),
Expand All @@ -77,22 +77,22 @@ def xf(name_node, parameters_node):
return xf


def brackets(parameters_node):
def brackets(parameters_node, selfref):
return [
emphasis('', 'self'),
emphasis('', selfref),
SphinxNodes.desc_name('', '', Text('[')),
emphasis('', parameters_node.children[0].astext()),
SphinxNodes.desc_name('', '', Text(']')),
]


SPECIAL_METHODS = {
'__getitem__': lambda name_node, parameters_node: inline(
'', '', *brackets(parameters_node)
'__getitem__': lambda name_node, parameters_node, selfref: inline(
'', '', *brackets(parameters_node, selfref)
),
'__setitem__': lambda name_node, parameters_node: inline(
'__setitem__': lambda name_node, parameters_node, selfref: inline(
'', '',
*brackets(parameters_node),
*brackets(parameters_node, selfref),
Text(' '),
SphinxNodes.desc_name('', '', Text('=')),
Text(' '),
Expand All @@ -101,26 +101,26 @@ def brackets(parameters_node):
if len(parameters_node.children) > 1 else ''
)),
),
'__delitem__': lambda name_node, parameters_node: inline(
'__delitem__': lambda name_node, parameters_node, selfref: inline(
'', '',
SphinxNodes.desc_name('', '', Text('del')),
Text(' '),
*brackets(parameters_node),
*brackets(parameters_node, selfref),
),
'__contains__': lambda name_node, parameters_node: inline(
'__contains__': lambda name_node, parameters_node, selfref: inline(
'', '',
emphasis('', parameters_node.children[0].astext()),
Text(' '),
SphinxNodes.desc_name('', '', Text('in')),
Text(' '),
emphasis('', 'self'),
emphasis('', selfref),
),

'__await__': lambda name_node, parameters_node: inline(
'__await__': lambda name_node, parameters_node, selfref: inline(
'', '',
SphinxNodes.desc_name('', '', Text('await')),
Text(' '),
emphasis('', 'self'),
emphasis('', selfref),
),

'__lt__': binary_op_transformer('<'),
Expand Down Expand Up @@ -156,8 +156,8 @@ def brackets(parameters_node):
'__abs__': function_transformer('abs'),
'__invert__': unary_op_transformer('~'),

'__call__': lambda name_node, parameters_node: (
emphasis('', 'self'),
'__call__': lambda name_node, parameters_node, selfref: (
emphasis('', selfref),
patch_node(parameters_node, '', parameters_node.children)
),
'__getattr__': function_transformer('getattr'),
Expand Down Expand Up @@ -202,7 +202,7 @@ def apply(self):
if method_name in SPECIAL_METHODS:
parameters_node = ref.next_node(SphinxNodes.desc_parameterlist)

name_node.replace_self(SPECIAL_METHODS[method_name](name_node, parameters_node))
name_node.replace_self(SPECIAL_METHODS[method_name](name_node, parameters_node, 'self'))
parameters_node.replace_self(())


Expand Down

0 comments on commit ef973ef

Please sign in to comment.