classes

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

https://github.com/dry-python/classes

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 16 committers (6.3%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.2%) to scientific vocabulary

Keywords

fp mypy mypy-plugins mypy-stubs pep561 python python3 typeclasses typesafety

Keywords from Contributors

python39 python313 python312 python311 python310 pydantic parsing json-schema standards hints
Last synced: 6 months ago · JSON representation

Repository

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

Basic Info
  • Host: GitHub
  • Owner: dry-python
  • License: bsd-2-clause
  • Language: Python
  • Default Branch: master
  • Homepage: https://classes.rtfd.io
  • Size: 881 KB
Statistics
  • Stars: 716
  • Watchers: 14
  • Forks: 29
  • Open Issues: 37
  • Releases: 5
Topics
fp mypy mypy-plugins mypy-stubs pep561 python python3 typeclasses typesafety
Created over 6 years ago · Last pushed about 1 year ago
Metadata Files
Readme Changelog Contributing License

README.md

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

bash pip install classes

You also need to configure mypy correctly and install our plugin:

```ini

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 bind 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 built 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:

```python 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?

```python

from typing import Union from classes import typeclass

@typeclass ... def to_json(instance) -> str: ... """This is a typeclass definition to convert things to json."""

@tojson.instance(int) ... @tojson.instance(float) ... def tojson_int(instance: Union[int, float]) -> str: ... return str(instance)

@tojson.instance(bool) ... def _tojson_bool(instance: bool) -> str: ... return 'true' if instance else 'false'

@tojson.instance(list) ... def _tojsonlist(instance: list) -> str: ... return '[{0}]'.format( ... ', '.join(tojson(listitem) for listitem in instance), ... )

```

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

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

```python

tojson(True) 'true' tojson(1) '1' to_json([False, 1, 2.5]) '[false, 1, 2.5]'

```

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

```python

Pretending to import the existing library from somewhere:

from tojson import tojson

import datetime as dt

@tojson.instance(dt.datetime) ... def _tojson_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: - Typeclasses in Python

— ⭐️ —

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

Read more in our Telegram group

GitHub Events

Total
  • Issues event: 4
  • Watch event: 54
  • Issue comment event: 13
  • Push event: 2
  • Pull request review event: 7
  • Pull request review comment event: 2
  • Pull request event: 7
  • Fork event: 3
Last Year
  • Issues event: 4
  • Watch event: 54
  • Issue comment event: 13
  • Push event: 2
  • Pull request review event: 7
  • Pull request review comment event: 2
  • Pull request event: 7
  • Fork event: 3

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 363
  • Total Committers: 16
  • Avg Commits per committer: 22.688
  • Development Distribution Score (DDS): 0.664
Past Year
  • Commits: 4
  • Committers: 3
  • Avg Commits per committer: 1.333
  • Development Distribution Score (DDS): 0.5
Top Committers
Name Email Commits
sobolevn m****l@s****e 122
dependabot[bot] 4****] 112
dependabot-preview[bot] 2****] 106
Pablo Aguilar p****r@o****r 8
mikhail-akimov d****o@g****m 3
Maxim Ivanov 4****g 2
Anton Agestam g****t@a****e 1
David Brochart d****t@g****m 1
Farhad Taebi f****i@u****e 1
Nicholas Mobbs n****n 1
Sergey 5****p 1
ShalokShalom s****m@p****h 1
Vincent Poulailleau v****u@g****m 1
github-actions[bot] 4****] 1
q0w 4****w 1
Dmytro Litvinov l****t@g****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 6
  • Total pull requests: 107
  • Average time to close issues: 3 months
  • Average time to close pull requests: 13 days
  • Total issue authors: 3
  • Total pull request authors: 10
  • Average comments per issue: 1.5
  • Average comments per pull request: 0.76
  • Merged pull requests: 57
  • Bot issues: 0
  • Bot pull requests: 95
Past Year
  • Issues: 4
  • Pull requests: 6
  • Average time to close issues: 3 months
  • Average time to close pull requests: about 4 hours
  • Issue authors: 1
  • Pull request authors: 2
  • Average comments per issue: 1.0
  • Average comments per pull request: 1.33
  • Merged pull requests: 3
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • ivanovmg (4)
  • zhukovgreen (1)
  • sobolevn (1)
Pull Request Authors
  • dependabot[bot] (92)
  • ivanovmg (6)
  • davidbrochart (2)
  • dependabot-preview[bot] (2)
  • antonagestam (1)
  • sobolevn (1)
  • q0w (1)
  • thepabloaguilar (1)
  • ShalokShalom (1)
  • zhukovgreen (1)
Top Labels
Issue Labels
bug (5) documentation (1)
Pull Request Labels
dependencies (94) python (92) github_actions (1)

Packages

  • Total packages: 2
  • Total downloads:
    • pypi 21,966 last-month
  • Total dependent packages: 5
    (may contain duplicates)
  • Total dependent repositories: 40
    (may contain duplicates)
  • Total versions: 7
  • Total maintainers: 2
pypi.org: classes

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

  • Versions: 6
  • Dependent Packages: 5
  • Dependent Repositories: 34
  • Downloads: 21,886 Last month
Rankings
Dependent packages count: 1.6%
Stargazers count: 2.5%
Dependent repos count: 2.5%
Downloads: 2.9%
Average: 3.5%
Forks count: 8.0%
Maintainers (2)
Last synced: 6 months ago
pypi.org: effects

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

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 6
  • Downloads: 80 Last month
Rankings
Stargazers count: 2.5%
Dependent repos count: 6.0%
Forks count: 8.0%
Dependent packages count: 10.1%
Average: 10.8%
Downloads: 27.5%
Maintainers (1)
Last synced: 6 months ago

Dependencies

.github/workflows/test.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
  • codecov/codecov-action v3 composite
poetry.lock pypi
  • 123 dependencies
pyproject.toml pypi
  • codespell ^2.2 develop
  • doc8 ^1.0 develop
  • flake8-pytest-style ^1.6 develop
  • m2r2 ^0.3 develop
  • mypy ^0.942 develop
  • nitpick ^0.32 develop
  • phantom-types ^1.0 develop
  • pytest ^7.2 develop
  • pytest-cov ^4.0 develop
  • pytest-mypy-plugins ^1.9 develop
  • pytest-randomly ^3.12 develop
  • safety ^2.3 develop
  • sphinx ^5.2 develop
  • sphinx-autodoc-typehints ^1.20 develop
  • sphinx-typlog-theme ^0.8 develop
  • sphinxcontrib-mermaid ^0.7 develop
  • tomlkit ^0.11 develop
  • wemake-python-styleguide ^0.17 develop
  • python ^3.7
  • typing_extensions >=3.10,<5.0