Skip to content

Commit

Permalink
Adjust code so that it produces proper .pyi files from mypy stubgen
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik authored and pmolodo committed Jan 22, 2021
1 parent fbab68c commit 685a142
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 84 deletions.
14 changes: 14 additions & 0 deletions maintenance/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ def functionTemplateFactory(funcName, module, returnFunc=None,

result = ''
if existing:
if isinstance(inFunc.__doc__, util.LazyDocString):
# inFunc was already decorated with @addCmdDocs
_logger.warning("Warning: %s.%s is decorated with "
"@addCmdDocs but it will be re-wrapped" %
(inFunc.__module__, funcName))

sourceFuncName = sourceFuncName.rsplit('.', 1)[1]
result += '\n_{func} = {func}\n'.format(func=sourceFuncName)
sourceFuncName = '_' + sourceFuncName
Expand All @@ -272,6 +278,14 @@ def functionTemplateFactory(funcName, module, returnFunc=None,
else:
if existing:
guessedName = factories._guessCmdName(inFunc)
if isinstance(inFunc.__doc__, util.LazyDocString):
# inFunc was already decorated with @addCmdDocs
return ''

_logger.warning("%s.%s should be decorated with @addCmdDocs to "
"generate better stubs" %
(inFunc.__module__, guessedName))

if guessedName != funcName:
# for, ie, fileInfo = FileInfo(), optionVar = OptionVar(),
# workspace = Workspace()
Expand Down
43 changes: 22 additions & 21 deletions pymel/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys
import pymel as _pymel
_pymel.core = sys.modules[__name__]
import pymel.versions as _versions
import pymel.internal.startup as _startup
import pymel.internal as _internal

Expand All @@ -18,7 +17,6 @@
import pymel.internal.pmcmds as _pmcmds
_pmcmds.addAllWrappedCmds()

import pymel.api as _api
import pymel.api.plugins as _plugins
from pymel.core.general import *
from pymel.core.context import *
Expand All @@ -31,12 +29,15 @@
from pymel.core.language import *
from pymel.core.other import *

import pymel.core.nodetypes as nodetypes
import pymel.core.nodetypes as nt
import pymel.core.datatypes as datatypes
import pymel.core.datatypes as dt
import pymel.core.uitypes as uitypes
import pymel.core.uitypes as ui
from pymel import api
from pymel.core import nodetypes
from pymel.core import datatypes
from pymel.core import uitypes
from pymel import versions

nt = nodetypes
dt = datatypes
ui = uitypes

from future.utils import PY2
if PY2:
Expand Down Expand Up @@ -209,7 +210,7 @@ def _pluginLoaded(*args):
# - The plug-in imports pymel, causing initialization and entering here.
if (pluginName in _pluginData) and 'callbackId' in _pluginData[pluginName] \
and _pluginData[pluginName]['callbackId'] != None:
_api.MEventMessage.removeCallback(_pluginData[pluginName]['callbackId'])
api.MEventMessage.removeCallback(_pluginData[pluginName]['callbackId'])

_logger.debug("Plugin loaded: %s", pluginName)
_pluginData[pluginName] = {}
Expand Down Expand Up @@ -243,7 +244,7 @@ def addPluginPyNodes(*args):
_logger.warning("could not find callback id!")
else:
if id is not None:
_api.MEventMessage.removeCallback(id)
api.MEventMessage.removeCallback(id)
if hasattr(id, 'disown'):
id.disown()

Expand All @@ -262,22 +263,22 @@ def addPluginPyNodes(*args):
continue
_addPluginNode(pluginName, mayaType)

# Note - in my testing, a single _api.MFileIO.isReadingFile() call would
# Note - in my testing, a single api.MFileIO.isReadingFile() call would
# also catch opening + referencing operations... but in commit
# 6e53d7818e9363d55d417c3a80ea7df94c4998ec, a check only against
# isReadingFile is commented out... so I'm playing it safe, and assuming
# there are edge cases where isOpeningFile is True but isReadingFile is
# not

# Detect if we are currently opening/importing a file and load as a callback versus execute now
if (_api.MFileIO.isReadingFile() or _api.MFileIO.isOpeningFile() or
_api.MFileIO.isReferencingFile()):
if _api.MFileIO.isReferencingFile():
if (api.MFileIO.isReadingFile() or api.MFileIO.isOpeningFile() or
api.MFileIO.isReferencingFile()):
if api.MFileIO.isReferencingFile():
_logger.debug("Installing temporary plugin-loaded nodes callback - PostSceneRead")
id = _api.MEventMessage.addEventCallback('PostSceneRead', addPluginPyNodes)
elif _api.MFileIO.isImportingFile():
id = api.MEventMessage.addEventCallback('PostSceneRead', addPluginPyNodes)
elif api.MFileIO.isImportingFile():
_logger.debug("Installing temporary plugin-loaded nodes callback - SceneImported")
id = _api.MEventMessage.addEventCallback('SceneImported', addPluginPyNodes)
id = api.MEventMessage.addEventCallback('SceneImported', addPluginPyNodes)
else:
# pre-2012 referencing operations will fall into this branch,
# which will not work (ie, pre-2012, plugins loaded due to
Expand All @@ -290,9 +291,9 @@ def addPluginPyNodes(*args):
# messages in commits 6e53d7818e9363d55d417c3a80ea7df94c4998ec
# and 81bc5ee28f1775a680449fec8724e21e703a52b8).
_logger.debug("Installing temporary plugin-loaded nodes callback - SceneOpened")
id = _api.MEventMessage.addEventCallback('SceneOpened', addPluginPyNodes)
id = api.MEventMessage.addEventCallback('SceneOpened', addPluginPyNodes)
_pluginData[pluginName]['callbackId'] = id
# scriptJob not respected in batch mode, had to use _api
# scriptJob not respected in batch mode, had to use api
#cmds.scriptJob( event=('SceneOpened',doSomethingElse), runOnce=1 )
else:
_logger.debug("Running plugin-loaded nodes callback")
Expand Down Expand Up @@ -355,7 +356,7 @@ def _installCallbacks():
_logger.debug("Adding pluginLoaded callback")
#_pluginLoadedCB = pluginLoadedCallback(module)

id = _api.MSceneMessage.addStringArrayCallback(_api.MSceneMessage.kAfterPluginLoad, _pluginLoaded)
id = api.MSceneMessage.addStringArrayCallback(api.MSceneMessage.kAfterPluginLoad, _pluginLoaded)
if hasattr(id, 'disown'):
id.disown()
else:
Expand All @@ -369,7 +370,7 @@ def _installCallbacks():
# mel.unloadPlugin( addCallback='''python("import pymel; pymel._pluginUnloaded('#1')")''' )

_logger.debug("Adding pluginUnloaded callback")
id = _api.MSceneMessage.addStringArrayCallback(_api.MSceneMessage.kAfterPluginUnload, _pluginUnloaded)
id = api.MSceneMessage.addStringArrayCallback(api.MSceneMessage.kAfterPluginUnload, _pluginUnloaded)
if hasattr(id, 'disown'):
id.disown()

Expand Down
9 changes: 3 additions & 6 deletions pymel/core/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import pymel.internal.pmcmds as cmds # type: ignore[no-redef]


@_factories.addCmdDocs
def currentTime(*args, **kwargs):
"""
Modifications:
Expand All @@ -35,6 +36,7 @@ def setCurrentTime(time):
return cmds.currentTime(time)


@_factories.addCmdDocs
def listAnimatable(*args, **kwargs):
"""
Modifications:
Expand All @@ -58,6 +60,7 @@ def keyframe(*args, **kwargs):
return res


@_factories.addCmdDocs
def deformer(*args, **kwargs):
return [_general.PyNode(x) for x in cmds.deformer(*args, **kwargs)]

Expand Down Expand Up @@ -410,8 +413,6 @@ def copyKey(*args, **kwargs):

copySkinWeights = _factories.getCmdFunc('copySkinWeights')

currentTime = _factories.addCmdDocs(currentTime)

@_factories.addCmdDocs
def cutKey(*args, **kwargs):
for flag in ['t', 'time']:
Expand Down Expand Up @@ -442,8 +443,6 @@ def deformableShape(*args, **kwargs):
res = _factories.maybeConvert(res, _general.PyNode)
return res

deformer = _factories.addCmdDocs(deformer)

deformerWeights = _factories.getCmdFunc('deformerWeights')

@_factories.addCmdDocs
Expand Down Expand Up @@ -678,8 +677,6 @@ def lattice(*args, **kwargs):
res = _factories.maybeConvert(res, _general.PyNode)
return res

listAnimatable = _factories.addCmdDocs(listAnimatable)

marker = _factories.getCmdFunc('marker')

mimicManipulation = _factories.getCmdFunc('mimicManipulation')
Expand Down
Loading

0 comments on commit 685a142

Please sign in to comment.