Skip to content

MetaList inherits from both Element and MutableSequence, but their index attributes conflict聽#258

Description

@castedo

This is an example of what I would call a headache caused by adding type hints to an existing untyped library. 馃槄

It's probably fine to ignore this issue because probably nobody is ever going to try and call the method MutableSequence.index(...) on a panflue.MetaList. That said, it's clearly a bug.

I am working around the issue in panflute-stubs by not adding a type hint at all for index and making a specific exception for index to tell stubtest to ignore the absence of a type hint for this attribute.

In the current codebase the declaration of MetaList is

class MetaList(MetaValue, MutableSequence):

which means in inherits from Element the following index attribute:

class Element(object):
    ...
    def __new__(cls, *args, **kwargs):
        ...
        element.index = None

The documentation says it's a int | None and other code in Element confirms.

On the other hand, collections.abc.Sequence (from which collections.abc.MutableSequence derives) has the method:

index(value[, start[, stop])

which is a standard method of sequences in Python, see https://docs.python.org/3/library/stdtypes.html#sequence.index

Mypy if run on the panflute codebase or any attempt by panflute-stubs to type hint index will report the following error:

error: Definition of "index" in base class "Element" is incompatible with definition in base class "Sequence"  [misc]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions