Skip to content

RcParams is fundamentally broken #12576

Closed

Description

@timhoffm

Bug report

Anyone tried:

>>> import matplotlib
>>> len(matplotlib.rcParams)
0

A great example of shooting yourself in the foot with multiple inheritance. Essentially, we do

class RcParams(MutableMapping, dict):
    def __getitem__(self, key):
        return dict.__getitem__(self, key)

    def __setitem__(self, key, val):
        return dict.__setitem__(self, key, val)

but do not reimplement __len__. You can work out the consequences yourself.

Proposed solution

Do not inherit RcParams from dict, and use a private data dict instead.

Activity

  1. timhoffm commented on Oct 20, 2018

    @timhoffm
    MemberAuthor

    Fun fact: This results in rcParams.clear() to not have any effect.

  2. added this to the v3.1 milestone on Oct 23, 2018
  3. tacaswell commented on Oct 23, 2018

    @tacaswell
    Member

    Could we just implement __len__ as a quicker fix? I am curious of this ever worked....

  4. timhoffm commented on Oct 24, 2018

    @timhoffm
    MemberAuthor

    Yes, implementing __len__ is probably a workaround, and I'm fine with that. Don't have time to go into the details in the next days.

    I assume that this worked when RcParams was just inheriting from dict (not tested).

    Anyway, in the long term, we should aim at just inheriting from MutableMapping.

  5. anntzer commented on Oct 25, 2018

    @anntzer
    Contributor

    Could we just implement len as a quicker fix? I am curious of this ever worked....

    I think it's a bug in CPython: https://bugs.python.org/issue35063

  6. timhoffm commented on Oct 25, 2018

    @timhoffm
    MemberAuthor

    Not having fully read the bug, but implementing __len__ would work around this, right?

  7. anntzer commented on Oct 25, 2018

    @anntzer
    Contributor

    Yes.
    The bug is that RcParams() should have raised an exception given than __len__ is a not-overridden abstractmethod.

  8. timhoffm commented on Nov 4, 2018

    @timhoffm
    MemberAuthor

    Closing as the current problem is fixed by #12630.

    Still, I would like to get rid of the dict-inheritance in the the long run (#12577).

  9. modified the milestones: v3.1, v3.0.x on Nov 4, 2018
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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions