Skip to main content

classes

classes logo


Build Status codecov Documentation Status Python Version wemake-python-styleguide Telegram chat


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:

# In setup.cfg or mypy.ini:
[mypy]
plugins =
  classes.contrib.mypy.classes_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 is parseable by the given schema?
  • It produces 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 simple function
  • It is hard to extend the default library for new custom types you will have in your own project
  • It is hard to override

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 convert 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.5])
'[false, 1, 2.5]'

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 full docs to learn more.

More!

Want more? Go to the docs! Or read these articles:

— ⭐️ —

Drylabs maintains dry-python and helps those who want to use it inside their organizations.

Read more at drylabs.io

Metadata

Release files for classes 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for classes 0.4.1
File Size Uploaded Provenance
classes-0.4.1.tar.gz 28.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for classes 0.4.1
File Interpreter ABI Platform
classes-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 65.7 kB

Release files / classes-0.4.1.tar.gz

Download URL classes-0.4.1.tar.gz
Size 28.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4de4fdd6c5c38607bbd8ad76703d7cc4dbe007cfa78e8ef1f62fc6ac55303e23
BLAKE2b-256 checksum
How to use checksums
426c543dc06fcd4d076d1399978971ebedfb90a7bd4315a9d4357f080e73fcae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.9 Darwin/18.7.0

Release files / classes-0.4.1-py3-none-any.whl

Download URL classes-0.4.1-py3-none-any.whl
Size 36.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
934a19eb23d2fe0c0430af6eae6d81a4d4fcc53519cf737f69c3bba3994d54e9
BLAKE2b-256 checksum
How to use checksums
7db456b2f3a02120013daef821373ac02f31e7451b1fb5301007b89806e19426
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.9 Darwin/18.7.0

Security for classes 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page