additional typing and method inference to pylint. All of these transforms are considered "global" to pylint-django, in that all checks and improvements requre them to be loaded. Additional transforms specific to checkers are loaded by the checker rather than here. For example, the ForeignKeyStringsChecker loads the foreignkey.py transforms itself as it may be disabled independently of the rest of pylint-django """ import os import re import astroid from pylint_django.transforms import fields, text_choices fields.add_transforms(astroid.MANAGER) text_choices.add_transforms(astroid.MANAGER) def _add_transform(package_name): 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))
"""Transforms.""" import os import re from pylint_django.transforms import foreignkey, fields from astroid import MANAGER from astroid.builder import AstroidBuilder from astroid import nodes foreignkey.add_transform(MANAGER) fields.add_transforms(MANAGER) def _add_transform(package_name, *class_names): """Transform package's classes.""" 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() fake = AstroidBuilder(MANAGER).string_build(fake_module) def set_fake_locals(module): """Set fake locals for package.""" if module.name != package_name: return for class_name in class_names: module.locals[class_name] = fake.locals[class_name] MANAGER.register_transform(nodes.Module, set_fake_locals)
"""Transforms.""" import os import re from pylint_django.transforms import foreignkey, fields from astroid import MANAGER from astroid.builder import AstroidBuilder from astroid import nodes foreignkey.add_transform(MANAGER) fields.add_transforms(MANAGER) def _add_transform(package_name, *class_names): """Transform package's classes.""" 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() fake = AstroidBuilder(MANAGER).string_build(fake_module) def set_fake_locals(module): """Set fake locals for package.""" if module.name != package_name: return for class_name in class_names: module._locals[class_name] = fake._locals[class_name] # pylint: disable=protected-access MANAGER.register_transform(nodes.Module, set_fake_locals)