Skip to content

Commit 4517273

Browse files
authored
Closes #215, closes #54 (#233)
1 parent b91ffab commit 4517273

File tree

13 files changed

+735
-303
lines changed

13 files changed

+735
-303
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
poetry run flake8 .
4848
poetry run mypy classes ./tests/**/*.py
4949
poetry run codespell classes tests docs typesafety README.md CONTRIBUTING.md CHANGELOG.md
50-
poetry run pytest classes tests docs/pages
50+
poetry run pytest classes tests docs/pages README.md
5151
poetry run doc8 -q docs
5252
poetry run poetry check
5353
poetry run pip check

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,26 @@ How would new API look like with this concept?
8787
```python
8888
>>> from typing import Union
8989
>>> from classes import typeclass
90+
9091
>>> @typeclass
9192
... def to_json(instance) -> str:
9293
... """This is a typeclass definition to convert things to json."""
93-
...
94+
9495
>>> @to_json.instance(int)
9596
... @to_json.instance(float)
9697
... def _to_json_int(instance: Union[int, float]) -> str:
9798
... return str(instance)
98-
...
99+
99100
>>> @to_json.instance(bool)
100101
... def _to_json_bool(instance: bool) -> str:
101102
... return 'true' if instance else 'false'
102-
...
103+
103104
>>> @to_json.instance(list)
104105
... def _to_json_list(instance: list) -> str:
105106
... return '[{0}]'.format(
106107
... ', '.join(to_json(list_item) for list_item in instance),
107108
... )
108-
...
109+
109110

110111
```
111112

@@ -126,9 +127,11 @@ Typeclass is represented as a regular function, so you can use it like one:
126127
And it easy to extend this typeclass with your own classes as well:
127128

128129
```python
129-
>>> # Pretending to import the existing library from somewhere:
130-
>>> # from to_json import to_json
130+
# Pretending to import the existing library from somewhere:
131+
# from to_json import to_json
132+
131133
>>> import datetime as dt
134+
132135
>>> @to_json.instance(dt.datetime)
133136
... def _to_json_datetime(instance: dt.datetime) -> str:
134137
... return instance.isoformat()
@@ -142,6 +145,12 @@ That's how simple, safe, and powerful typeclasses are!
142145
Make sure to [check out our full docs](https://github.com/dry-python/classes) to learn more.
143146

144147

145-
## License
148+
## More!
149+
150+
Want more?
151+
[Go to the docs!](https://classes.readthedocs.io)
152+
146153

147-
BSD 2-Clause
154+
<p align="center">&mdash; ⭐️ &mdash;</p>
155+
<p align="center"><i>Drylabs maintains dry-python and helps those who want to use it inside their organizations.</i></p>
156+
<p align="center"><i>Read more at <a href="https://drylabs.io">drylabs.io</a></i></p>

classes/_typeclass.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
113113
>>> assert example(WithField()) == 'with field'
114114
115+
See our `official docs <https://classes.readthedocs.io>`_ to learn more!
115116
"""
116117

117118
from abc import get_cache_token
@@ -298,33 +299,8 @@ class _TypeClass( # noqa: WPS214
298299
"""
299300
That's how we represent typeclasses.
300301
301-
You should also use this type to annotate places
302-
where you expect some specific typeclass to be used.
303-
304-
.. code:: python
305-
306-
>>> from typing import Callable
307-
>>> from classes import typeclass
308-
309-
>>> @typeclass
310-
... def used(instance, other: int) -> int:
311-
... '''Example typeclass to be used later.'''
312-
313-
>>> @used.instance(int)
314-
... def _used_int(instance: int, other: int) -> int:
315-
... return instance + other
316-
317-
>>> def accepts_typeclass(
318-
... callback: Callable[[int, int], int],
319-
... ) -> int:
320-
... return callback(1, 3)
321-
322-
>>> assert accepts_typeclass(used) == 4
323-
324-
Take a note, that we structural subtyping here.
325-
And all typeclasses that match ``Callable[[int, int], int]`` signature
326-
will typecheck.
327-
302+
You probably don't need to use this type directly,
303+
use its public methods and public :func:`~typeclass` constructor.
328304
"""
329305

330306
__slots__ = (
@@ -408,6 +384,30 @@ def __call__(
408384
Since, we define ``__call__`` method for this class,
409385
it can be used and typechecked everywhere,
410386
where a regular ``Callable`` is expected.
387+
388+
.. code:: python
389+
390+
>>> from typing import Callable
391+
>>> from classes import typeclass
392+
393+
>>> @typeclass
394+
... def used(instance, other: int) -> int:
395+
... '''Example typeclass to be used later.'''
396+
397+
>>> @used.instance(int)
398+
... def _used_int(instance: int, other: int) -> int:
399+
... return instance + other
400+
401+
>>> def accepts_typeclass(
402+
... callback: Callable[[int, int], int],
403+
... ) -> int:
404+
... return callback(1, 3)
405+
406+
>>> assert accepts_typeclass(used) == 4
407+
408+
Take a note, that we use structural subtyping here.
409+
And all typeclasses that match ``Callable[[int, int], int]`` signature
410+
will typecheck.
411411
"""
412412
self._control_abc_cache()
413413
instance_type = type(instance)

docs/_static/.gitkeep

Whitespace-only changes.

docs/_static/overrides.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.globaltoc > p.caption {
2+
display: block;
3+
font-size: 1.05em;
4+
font-weight: 700;
5+
text-decoration: none;
6+
margin-bottom: 1em;
7+
border: 0;
8+
}
9+
10+
/* For some reason it did not have a scroll attached. */
11+
12+
.mermaid {
13+
overflow: scroll;
14+
}

docs/_templates/layout.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{# layout.html #}
2+
{# Import the layout of the theme. #}
3+
{% extends "!layout.html" %}
4+
5+
{% set css_files = css_files + ['_static/overrides.css'] %}

docs/index.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,23 @@ Contents
1414
:maxdepth: 2
1515
:caption: Userguide
1616

17+
pages/why.rst
1718
pages/concept.rst
18-
pages/typeclass.rst
19-
pages/typesafety.rst
19+
pages/supports.rst
20+
pages/generics.rst
21+
22+
.. toctree::
23+
:maxdepth: 1
24+
:caption: Tooling
25+
2026
pages/dry-python.rst
2127

28+
.. toctree::
29+
:maxdepth: 1
30+
:caption: API
31+
32+
pages/api-docs.rst
33+
2234
.. toctree::
2335
:maxdepth: 1
2436
:caption: Changelog
File renamed without changes.

0 commit comments

Comments
 (0)