Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

mwchase/class-namespaces

Repository files navigation

class-namespaces

No Maintenance Intended Documentation Status https://travis-ci.org/mwchase/class-namespaces.svg?branch=master https://coveralls.io/repos/github/mwchase/class-namespaces/badge.svg?branch=master https://api.codacy.com/project/badge/Grade/f73ed5e3849c4049b8c9e3f17f6589da Code Health Scrutinizer Code Quality Code Coverage Build Status Code Climate Test Coverage Issue Count

Well-behaved class namespacing in Python. Inspired by https://erezsh.wordpress.com/2008/06/27/namespaces-lets-do-more-of-those-python-hackery/

Basic Usage

Example code:

import class_namespaces as cn

class MyCls(cn.Namespaceable):

    var = 1

    with cn.Namespace() as my_ns:
        var = 2

assert MyCls.var == 1
assert MyCls.my_ns.var == 2

Other things that work:

  • Descriptors (methods, classmethods, staticmethods, properties, custom descriptors)
  • super()
  • Prepopulating Namespaces. The constructor takes the same arguments as a dict.
  • abstractmethods. See the compat module.

Things that don't work:

  • Various ways of putting a Namespace in a Namespace that I didn't see an obvious way to handle. In particular...
    • There is no way to put an established namespace directly into another namespace.
  • Some pytest constructs behave weirdly inside the class definitions. Hopefully, this doesn't matter to anyone not writing tests for the package.
  • No way to have instance Namespaces on non-hashable types, or subclasses of some built-in types, particularly tuple. Try using data descriptors instead.

Things that might work:

  • New namespace features in Python 3.6. Current testing is spotty.
  • Combining with other metaclasses. Unfortunately, the current setup is somewhat brittle. It may be necessary to experiment with the ordering of bases.