Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from astroid import MANAGER, register_module_extender
from astroid.builder import AstroidBuilder
def pyqt4_qtcore_transform():
return AstroidBuilder(MANAGER).string_build('''
def SIGNAL(signal_name): pass
class QObject(object):
def emit(self, signal): pass
''')
register_module_extender(MANAGER, 'PyQt4.QtCore', pyqt4_qtcore_transform)
def fake_module_builder():
"""
Build a fake module to use within transformations.
@package_name is a parameter from the outher scope b/c according to
the docs this can't receive any parameters.
http://pylint.pycqa.org/projects/astroid/en/latest/extending.html?highlight=MANAGER#module-extender-transforms
"""
transforms_dir = os.path.join(os.path.dirname(__file__), 'transforms')
fake_module_path = os.path.join(transforms_dir, '%s.py' % re.sub(r'\.', '_', package_name))
with open(fake_module_path) as modulefile:
fake_module = modulefile.read()
return astroid.builder.AstroidBuilder(astroid.MANAGER).string_build(fake_module)
astroid.register_module_extender(astroid.MANAGER, package_name, fake_module_builder)
def apply_type_shim(cls: ClassDef, _context: Any = None) -> Iterator[ClassDef]:
"""
Morphs model fields to representative type
"""
base_nodes: List[ClassDef] = [cls]
# Use the type inference standard
try:
base_nodes.extend(list(cls.getattr("field_type")[0].infer()))
except AstroidError:
pass
return iter(base_nodes)
MANAGER.register_transform(nodes.ClassDef, inference_tip(apply_type_shim), is_model_field)
MANAGER.register_transform(nodes.ClassDef, transform_model, is_model)
return (
node.func.attrname == member
and isinstance(node.func.expr, astroid.Name)
and node.func.expr.name == "functools"
)
_looks_like_partial = partial(_looks_like_functools_member, member="partial")
MANAGER.register_transform(
astroid.FunctionDef, _transform_lru_cache, _looks_like_lru_cache
)
MANAGER.register_transform(
astroid.Call,
astroid.inference_tip(_functools_partial_inference),
_looks_like_partial,
)
ubyte = uint8
uint = uint32
uint0 = uint32
uintc = uint32
uintp = uint32
ulonglong = uint64
unicode = str_
unicode_ = str_
ushort = uint16
void0 = void
"""
)
astroid.register_module_extender(
astroid.MANAGER, "numpy.core.numerictypes", numpy_core_numerictypes_transform
)
def postprocess(tempname, filename):
pass
def set_extraction_path(path):
pass
def cleanup_resources(force=False):
pass
def get_distribution(dist):
return Distribution(dist)
_namespace_packages = {}
''')
register_module_extender(MANAGER, 'pkg_resources', pkg_resources_transform)
def set_extraction_path(path):
pass
def cleanup_resources(force=False):
pass
def get_distribution(dist):
return Distribution(dist)
_namespace_packages = {}
"""
)
register_module_extender(MANAGER, "pkg_resources", pkg_resources_transform)
"""Astroid hooks for dateutil"""
import textwrap
from astroid import MANAGER, register_module_extender
from astroid.builder import AstroidBuilder
def dateutil_transform():
return AstroidBuilder(MANAGER).string_build(textwrap.dedent('''
import datetime
def parse(timestr, parserinfo=None, **kwargs):
return datetime.datetime()
'''))
register_module_extender(MANAGER, 'dateutil.parser', dateutil_transform)
def mechanize_transform():
return AstroidBuilder(MANAGER).string_build('''
def pyqt4_qtcore_transform():
return AstroidBuilder(MANAGER).string_build('''