
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Smart, pythonic, ad-hoc, typed polymorphism for Python.
mypy
, PEP561 compatiblepip 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!
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:
json
is parseable by the given schema?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.
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
FAQs
Smart, pythonic, ad-hoc, typed polymorphism for Python
We found that classes demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.