This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

Utility to check if your subtypes pass supertypes tests.

Liskov Substitution is related in SOLID principles. It has been formulated by Barbara Liskov and Jeanette Wing in order to define more precisely the notion of subtypes.

For more details read: http://reports-archive.adm.cs.cmu.edu/anon/1999/CMU-CS-99-156.ps

To ensure a certain respect of Liskov Substitution principle in your program, you can simply make your subtypes tests extends your supertypes tests, but importing a supertype test directly in subtype file make the test being recognized by unit tests runners and run it several times.

You can just import it in a function, but to not repeat this every time, I’ve made a small util, wich offers 3 ways to declare a subtype test.

Each solution gives you different expressiveness depending on your preference:
  • “subtype” function which just import and return a class from a module given as string argument.
  • “behave_as” metaclass generator function which returns a metaclass from given modules.
  • “can_substitute” decorator wich returns the class extending modules given as arguments.
Liskov and Wing classified subtype relationships in two broad categories:
  • Extension subtypes: add methods or eventually states to supertypes
  • Constrained subtypes: when supertype enable variations in subtypes

Since version 0.2 you can find some helpers to define constraints.(see example 4)


Table of Contents

Installation

Simply install it from pypi:

pip install liskov

or from sources:

git clone git@github.com:apieum/liskov.git
cd liskov
python setup.py install

Usage

Example 1 - “subtype”:

Use a lambda if too long.
from liskov import subtype

BasicCalc = lambda: subtype('testCalc.BasicCalcTest')
BaseConverter = lambda: subtype('testConvert.BaseConverterTest')
class ScientificCalcTest(BasicCalc(), BaseConverter()):
  def test_it_is_a_subtype_of_BasicCalc(self):
    from testCalc import BasicCalcTest
    assert isinstance(self, BasicCalcTest)

  def test_it_is_a_subtype_of_BaseConverter(self):
    from testConvert import BaseConverterTest
    assert isinstance(self, BaseConverterTest)

Example 2 - “behave_as”:

Python 2 version

from liskov import behave_as

class ScientificCalcTest(object):
  __metaclass__ = behave_as('testCalc.BasicCalcTest', 'testConvert.BaseConverterTest')

  def test_it_is_a_subtype_of_BasicCalc(self):
    from testCalc import BasicCalcTest
    assert isinstance(self, BasicCalcTest)

  def test_it_is_a_subtype_of_BaseConverter(self):
    from testConvert import BaseConverterTest
    assert isinstance(self, BaseConverterTest)

Python 3 version

from liskov import behave_as

metaclass = behave_as('testCalc.BasicCalcTest', 'testConvert.BaseConverterTest')

class ScientificCalcTest(object, metaclass=metaclass):
  def test_it_is_a_subtype_of_BasicCalc(self):
    from testCalc import BasicCalcTest
    assert isinstance(self, BasicCalcTest)

  def test_it_is_a_subtype_of_BaseConverter(self):
    from testConvert import BaseConverterTest
    assert isinstance(self, BaseConverterTest)

Example 3 - “can_substitute”:

from liskov import can_substitute

@can_substitute('testCalc.BasicCalcTest', 'testConvert.BaseConverterTest')
class ScientificCalcTest(object):
  def test_it_is_a_subtype_of_BasicCalc(self):
    from testCalc import BasicCalcTest
    assert isinstance(self, BasicCalcTest)

  def test_it_is_a_subtype_of_BaseConverter(self):
    from testConvert import BaseConverterTest
    assert isinstance(self, BaseConverterTest)

Example 4 - Constraints:

This example follow Liskov and Wing constrained subtypes Elephants hierarchy example from “Behavioural Subtyping using invariants and constraints” (link above)

Elephants can be white, green or blue RoyalElephant is always blue AlbinoElephant is always white

Each instance of Elephant in ElephantTest is made with “new_elephant” ElephantTest test if an Elephant can be white, green or blue.

Declare Constraints with a decorator

from liskov import can_substitute, under_constraint
import elephant

@can_substitute('elephant.ElephantTest')
@under_constraint('test_it_can_be_grey', 'test_it_can_be_white')
class RoyalElephantTest(object):
  def new_elephant(self, *args):
    return elephant.RoyalElephant()

Declare Constraints with metaclass

Python 2 version

from liskov import behave_as
import elephant

class RoyalElephantTest(object):
  __metaclass__ = behave_as('elephant.ElephantTest').except_for('test_it_can_be_grey', 'test_it_can_be_white')
  def new_elephant(self, *args):
    return elephant.RoyalElephant()

Python 3 version

from liskov import behave_as
import elephant

metaclass = behave_as('elephant.ElephantTest').except_for('test_it_can_be_grey', 'test_it_can_be_white')
class RoyalElephantTest(object, metaclass=metaclass):
  def new_elephant(self, *args):
    return elephant.RoyalElephant()
Declare Constraints with subtype function
bind “subtype” to “constrain” with any of these operators: “& | + -“
from liskov import subtype, constrain
import elephant

ConstrainedElephantTest = lambda: subtype('elephant.ElephantTest') & constrain('test_it_can_be_grey', 'test_it_can_be_white')

class RoyalElephantTest(ConstrainedElephantTest()):
  def new_elephant(self, *args):
    return elephant.RoyalElephant()

Development

Fell free to give feedback or improvment.

Launch test:

git clone git@github.com:apieum/liskov.git
cd liskov
nosetests --with-spec --spec-color
Release History

Release History

0.2.2

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
liskov-0.2.2-py2.7.egg (8.8 kB) Copy SHA256 Checksum SHA256 2.7 Egg Oct 5, 2013
liskov-0.2.2.tar.gz (5.0 kB) Copy SHA256 Checksum SHA256 Source Oct 5, 2013

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting