Description
Although Django seems to support it, pylint_django does not find models which are specified lowercase and this is due to the following code block: https://github.com/pylint-dev/pylint-django/blob/master/pylint_django/transforms/foreignkey.py#121
for module in list(MANAGER.astroid_cache.values()):
# only load model classes from modules which match the module in
# which *we think* they are defined. This will prevent inferring
# other models of the same name which are found elsewhere!
if model_name in module.locals and module.name.endswith(module_name):
class_defs = _get_model_class_defs_from_module(module, model_name, module_name)
if class_defs:
return iter([class_defs[0].instantiate_class()])
module.locals
contains class names the way they appear in the model files, for example 'MyModel': [<ClassDef.MyModell.193 at 0x7fe5ebc42560>]
.
However, if I define my Foreign Key with the target myapp.mymodel
, which is strictly valid in Django, model_name in module.locals
will fail because model_name
contains mymodel
and module.locals
has a key MyModel
.
I am using the following versions:
- django 4.2.20
- pylint 3.3.6
- pylint_django 2.6.1
I have no idea what would be the best way to improve this check.
A naive approach would be to do:
if any([model_name.lower() == local_model.lower() for local_model in module.locals]) and module.name.endswith(module_name)
but I am not sure about the performance of this.