Skip to content

Options: Consistent handling of private-by-docstring (*_)fields properties #975

@richardebeling

Description

@richardebeling

Edit:

As explained in #975 (comment), the properties fields, concrete_fields, local_concrete_fields, many_to_many and related_objects of the Option class are private according to their docstrings, even through they don't start with a leading underscore. This resolved my original issue. As a user, we should just use get_fields() and then filter the results as we wish.

However, @sterliakov noticed an inconsistency:

fields property is present in stubs, but marked as private in Django source too. I think it's worth discussing, and after final decision we should either drop fields or add other four properties too. Before my changes only fields and many_to_many were present.

So I'm leaving this issue open for now, although the original issue text below is outdated / has been resolved.


Problem description

In this commit of #909, the many_to_many property of the Option class was removed. In django, this is still available on current master.

After the removal, this code:

from django.db.models import Model

def test_mypy(model_instance: Model):
    return model_instance._meta.many_to_many

fails type checking with this error:

test.py:4: error: "Options[Any]" has no attribute "many_to_many"; maybe "local_many_to_many"?

Expected result

The code should pass, or is there any reason that many_to_many was removed from the stubs? I couldn't find any changelogs, and the message of the commit introducing the change doesn't give much insight either:

Define properties where possible, remove 'None' argument annotations, remove inadequate type in make_immutable_fields_list.

@sterliakov was there a reason to remove this?

Versions

Tested with django-stubs 1.11.0. The many_to_many property definition is missing on master if I see it correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions