Skip to content

E1101 (no-member) when ForeignKey target model is specified as a string, in lowercase #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
73VW opened this issue Apr 24, 2025 · 0 comments

Comments

@73VW
Copy link

73VW commented Apr 24, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant