Skip to content

Commit

Permalink
Fix function inspection of functools.partials in migration files
Browse files Browse the repository at this point in the history
  • Loading branch information
Caspar Wylie authored and Caspar Wylie committed Feb 3, 2023
1 parent 202a6d9 commit ff38ad3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions django_migration_linter/migration_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hashlib
import inspect
import functools
import logging
import os
import re
Expand Down Expand Up @@ -473,10 +474,16 @@ def analyse_data_migration(

return errors, ignored, warnings

@staticmethod
def discover_function(function):
if isinstance(function, functools.partial):
return function.func
return function

def lint_runpython(
self, runpython: RunPython
) -> tuple[list[Issue], list[Issue], list[Issue]]:
function_name = runpython.code.__name__
function_name = self.discover_function(runpython.code).__name__
error = []
ignored = []
warning = []
Expand Down Expand Up @@ -551,8 +558,9 @@ def lint_runpython(
def get_runpython_model_import_issues(code: Callable) -> list[Issue]:
model_object_regex = re.compile(r"[^a-zA-Z0-9._]?([a-zA-Z0-9._]+?)\.objects")

function_name = code.__name__
source_code = inspect.getsource(code)
function = MigrationLinter.discover_function(code)
function_name = function.__name__
source_code = inspect.getsource(function)

called_models = model_object_regex.findall(source_code)
issues = []
Expand Down Expand Up @@ -582,8 +590,9 @@ def get_runpython_model_import_issues(code: Callable) -> list[Issue]:
def get_runpython_model_variable_naming_issues(code: Callable) -> list[Issue]:
model_object_regex = re.compile(r"[^a-zA-Z]?([a-zA-Z0-9]+?)\.objects")

function_name = code.__name__
source_code = inspect.getsource(code)
function = MigrationLinter.discover_function(code)
function_name = function.__name__
source_code = inspect.getsource(function)

called_models = model_object_regex.findall(source_code)
issues = []
Expand Down

0 comments on commit ff38ad3

Please sign in to comment.