Skip to content

Match Python 3 metaclass syntax #162

@benjaminp

Description

@benjaminp

Originally reported by: Evan (Bitbucket: evan_, GitHub: Unknown)


The current signature of with_metaclass is awkward in that it puts the metaclass before the bases, whereas declaring classes naturally in Python 3 puts the metaclass last as a keyword-only argument, e.g. class Class(base1, base2, metaclass=meta). Keyword-only arguments aren't supported by Python 2, but they can be faked by accepting **kwargs and doing a bit of filtering.

There are two ways of implementing this without breaking backwards compatibility:

  1. Update with_metaclass to handle both ways of passing arguments, falling back to the original signature if metaclass is not given as a keyword argument. This is the simplest, but people unfamiliar with keyword-only arguments may assume from the signature with_metaclass(*bases, metaclass) that they are able to do with_metaclass(base1, base2, meta), which will end up using base1 as the metaclass with no warning.

  2. Create another function which is a simple wrapper around with_metaclass. This function could mimic a real keyword-only argument, in which case other_function(base1, base2, meta) would raise a TypeError. This could optionally also involve deprecating with_metaclass.

Thoughts? Happy to do a PR to implement this.


Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions