Skip to main content

Smart, pythonic, ad-hoc, typed polymorphism for Python

Project description

classes

classes logo


Build Status Coverage Status Documentation Status Python Version wemake-python-styleguide Checked with mypy


Smart, pythonic, ad-hoc, typed polymorphism for Python.

Features

  • Provides a bunch of primitives to write declarative business logic
  • Enforces better architecture
  • Fully typed with annotations and checked with mypy, PEP561 compatible
  • Allows to write a lot of simple code without inheritance or interfaces
  • Pythonic and pleasant to write and to read (!)
  • Easy to start: has lots of docs, tests, and tutorials

Installation

pip install classes

You also need to configure mypy correctly and install our plugin to fix this existing issue:

# In setup.cfg or mypy.ini:
[mypy]
plugins =
  classes.contrib.mypy.typeclass_plugin

Without this step, your project will report type-violations here and there.

We also recommend to use the same mypy settings we use.

Make sure you know how to get started, check out our docs!

Example

Imagine, that you want to bound implementation to some particular type. Like, strings behave like this, numbers behave like that, and so on.

The good realworld example is djangorestframework. It is build around the idea that different data types should be converted differently to and from json format.

What is the "traditional" (or outdated if you will!) approach? To create tons of classes for different data types and use them.

That's how we end up with classes like so:

class IntField(Field):
    def from_json(self, value):
        return value

    def to_json(self, value):
        return value

It literally has a lot of problems:

  • It is hard to type this code. How can I be sure that my json will be parsed by the given schema?
  • It contains a lot of boilerplate
  • It has complex API: there are usually several methods to override, some fields to adjust. Moreover, we use a class, not a callable
  • It is hard to extend the default library for new custom types you will have in your own project

There should be a better way of solving this problem! And typeclasses are a better way!

How would new API look like with this concept?

>>> from typing import Union
>>> from classes import typeclass
>>> @typeclass
... def to_json(instance) -> str:
...     """This is a typeclass definition to covert things to json."""
...
>>> @to_json.instance(int)
... @to_json.instance(float)
... def _to_json_int(instance: Union[int, float]) -> str:
...     return str(instance)
...
>>> @to_json.instance(bool)
... def _to_json_bool(instance: bool) -> str:
...     return 'true' if instance else 'false'
...
>>> @to_json.instance(list)
... def _to_json_list(instance: list) -> str:
...     return '[{0}]'.format(
...         ', '.join(to_json(list_item) for list_item in instance),
...     )
...

See how easy it is to works with types and implementation?

Typeclass is represented as a regular function, so you can use it like one:

>>> to_json(True)
'true'
>>> to_json(1)
'1'
>>> to_json([False, 1, 2])
'[false, 1, 2]'

And it easy to extend this typeclass with your own classes as well:

>>> # Pretending to import the existing library from somewhere:
>>> # from to_json import to_json
>>> import datetime as dt
>>> @to_json.instance(dt.datetime)
... def _to_json_datetime(instance: dt.datetime) -> str:
...     return instance.isoformat()
...
>>> to_json(dt.datetime(2019, 10, 31, 12, 28, 00))
'2019-10-31T12:28:00'

That's how simple, safe, and powerful typeclasses are! Make sure to check out our docs to learn more.

License

BSD 2-Clause

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

classes-0.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

classes-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file classes-0.1.0.tar.gz.

File metadata

  • Download URL: classes-0.1.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.4 Darwin/18.6.0

File hashes

Hashes for classes-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0cac8e05f7b825ac3de0566a0bf65acc59a3298e751d5da18faad1f3d3f0bc1a
MD5 8c8233434e904cf1e41120f82cde0a19
BLAKE2b-256 e065f28e1db714b3fc64bc9e0f0746b654fc1c5448c211504011ac4b5d89be83

See more details on using hashes here.

File details

Details for the file classes-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: classes-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.4 Darwin/18.6.0

File hashes

Hashes for classes-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aecf45bb816be9fc68f5ad5a8788e3ac9d5ca3c7d266d20ed1b48f0854e81a6f
MD5 f13a9feb8a1dafbe6a599f96cac659e5
BLAKE2b-256 4dcdb997f89a687634f150f0ca1070aa2b9789bb02277676de0209cfc8de1a09

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page