Skip to content

Commit

Permalink
flag renames
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Avanov committed Apr 10, 2020
1 parent ee58c1f commit 6e6affc
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 118 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.rst
Expand Up @@ -5,7 +5,11 @@ CHANGELOG
0.21.0
===============

* Support for global attribute name overrides with ``typeit.flags.GLOBAL_NAME_OVERRIDE``
* Support for global attribute name overrides with ``typeit.flags.GlobalNameOverride``
* ``typeit.TypeConstructor`` is now recommended over ``typeit.type_constructor``.
The latter is left for backward compatibility.
* ``NonStrictPrimitives`` and ``SumTypeDict`` are now recommended over corresponding
``NON_STRICT_PRIMITIVES`` ``SUM_TYPE_DICT``. The latter are left for backward compatibility.


0.20.0
Expand Down Expand Up @@ -81,7 +85,7 @@ CHANGELOG

* Fix bug in parsing union types with sequence variants.
* Primitive types switched to strict matching.
* Non-strict primitives flag `NON_STRICT_PRIMITIVES` is available for overrides.
* Non-strict primitives flag `NonStrictPrimitives` is available for overrides.
* Added support for `typing.Mapping`
* Added support for `pathlib.Path`

Expand Down
6 changes: 2 additions & 4 deletions README.rst
Expand Up @@ -19,8 +19,6 @@
:alt: Latest PyPI Release


**Development status: Beta**

Typeit
------

Expand All @@ -36,7 +34,7 @@ The snipped above produces output similar to this:
.. code-block:: python
from typing import Any, NamedTuple, Optional, Sequence
from typeit import type_constructor
from typeit import TypeConstructor
class Main(NamedTuple):
Expand All @@ -50,7 +48,7 @@ The snipped above produces output similar to this:
}
mk_main, serialize_main = type_constructor & overrides ^ Main
mk_main, serialize_main = TypeConstructor & overrides ^ Main
Documentation
Expand Down
12 changes: 6 additions & 6 deletions docs/cookbook/docker_compose.rst
Expand Up @@ -49,7 +49,7 @@ The command will generate ``./docker_config.py`` with definitions similar to thi
# Source code of ./docker_config.py
from typing import Any, NamedTuple, Optional, Sequence
from typeit import type_constructor
from typeit import TypeConstructor
class ServicesRedis(NamedTuple):
Expand Down Expand Up @@ -79,7 +79,7 @@ The command will generate ``./docker_config.py`` with definitions similar to thi
services: Services
mk_main, serialize_main = type_constructor ^ Main
mk_main, serialize_main = TypeConstructor ^ Main
Neat! This already is a good enough representation to play with, and we can verify that
it does work as expected:
Expand Down Expand Up @@ -115,10 +115,10 @@ and ``DockerConfig.version`` is restricted to ``"2.0"`` and ``"2.1"`` only (and
services: Services
mk_config, serialize_config = type_constructor ^ DockerConfig
mk_config, serialize_config = TypeConstructor ^ DockerConfig
Looks good! There is just one thing that we still want to improve - service ports.
And for that we need to extend our ``type_constructor``.
And for that we need to extend our ``TypeConstructor``.

Extending
---------
Expand Down Expand Up @@ -189,7 +189,7 @@ can be constructed with ``PortMappingSchema`` conversion schema:
# Source code of ./docker_config.py
Typer = typeit.type_constructor & PortMappingSchema[PortMapping]
Typer = typeit.TypeConstructor & PortMappingSchema[PortMapping]
We named the new extended type constructor ``Typer``, and we're done with the task!
Let's take a look at the final result.
Expand Down Expand Up @@ -263,7 +263,7 @@ Here's what we get as the final solution for our task:
services: Services
Typer = typeit.type_constructor & PortMappingSchema[PortMapping]
Typer = typeit.TypeConstructor & PortMappingSchema[PortMapping]
mk_config, serialize_config = Typer ^ DockerConfig
Expand Down
5 changes: 0 additions & 5 deletions docs/index.rst
Expand Up @@ -8,11 +8,6 @@ Type it!

**typeit** brings typed data into your Python 3.6+ projects.

.. CAUTION::

The project is in a beta development status, and a few public
APIs may change in a backward-incompatible manner.


Quickstart Guide
================
Expand Down
50 changes: 28 additions & 22 deletions docs/quickstart_guide.rst
Expand Up @@ -24,7 +24,7 @@ You should see output similar to this:
.. code-block:: python
from typing import Any, NamedTuple, Optional, Sequence
from typeit import type_constructor
from typeit import TypeConstructor
class Main(NamedTuple):
Expand All @@ -38,7 +38,7 @@ You should see output similar to this:
}
mk_main, serialize_main = type_constructor & overrides ^ Main
mk_main, serialize_main = TypeConstructor & overrides ^ Main
You can use this snippet as a starting point to improve further.
Expand All @@ -60,19 +60,25 @@ and rename the whole structure to better indicate the nature of the data:
}
mk_person, serialize_person = type_constructor & overrides ^ Person
mk_person, serialize_person = TypeConstructor & overrides ^ Person
``typeit`` will handle creation of the constructor ``mk_person :: Dict -> Person`` and the serializer
``serialize_person :: Person -> Dict`` for you.

``type_constructor & overrides`` produces a new type constructor that takes overrides into consideration,
and ``type_constructor ^ Person`` reads as "type constructor applied on the Person structure" and essentially
is the same as ``type_constructor(Person)``, but doesn't require parentheses around overrides (and extensions):
``TypeConstructor & overrides`` produces a new type constructor that takes overrides into consideration,
and ``TypeConstructor ^ Person`` reads as "type constructor applied on the Person structure" and essentially
is the same as ``TypeConstructor(Person)``, but doesn't require parentheses around overrides (and extensions):

.. code-block:: python
(type_constructor & overrides & extension & ...)(Person)
(TypeConstructor & overrides & extension & ...)(Person)
If you don't like this combinator syntax, you can use a more verbose version that does exactly the same thing:

.. code-block:: python
TypeConstructor.override(overrides).override(extension).apply_on(Person)
Overrides
Expand All @@ -84,7 +90,7 @@ Overrides


As you might have noticed in the example above, ``typeit`` generated a snippet with
a dictionary called ``overrides``, which is passed to the ``type_constructor`` alongside
a dictionary called ``overrides``, which is passed to the ``TypeConstructor`` alongside
our ``Person`` type:

.. code-block:: python
Expand All @@ -94,7 +100,7 @@ our ``Person`` type:
}
mk_person, serialize_person = type_constructor & overrides ^ Person
mk_person, serialize_person = TypeConstructor & overrides ^ Person
This is the way we can indicate that our Python structure has different field
Expand Down Expand Up @@ -134,7 +140,7 @@ any nested types, for instance:
}
mk_person, serialize_person = type_constructor & overrides ^ Person
mk_person, serialize_person = TypeConstructor & overrides ^ Person
Handling errors
Expand Down Expand Up @@ -296,12 +302,12 @@ And, of course, you can use Sum Types in signatures of your serializable data:
.. code-block:: python
from typing import NamedTuple, Sequence
from typeit import type_constructor
from typeit import TypeConstructor
class Payments(NamedTuple):
latest: Sequence[Payment]
mk_payments, serialize_payments = type_constructor ^ Payments
mk_payments, serialize_payments = TypeConstructor ^ Payments
json_ready = serialize_payments(Payments(latest=[adam_paid, jane_paid, fred_paid]))
payments = mk_payments(json_ready)
Expand All @@ -314,7 +320,7 @@ Constructor Flags

Constructor flags allow you to define global overrides that affect all structures (toplevel and nested) in a uniform fashion.

``typeit.flags.GLOBAL_NAME_OVERRIDE`` -
``typeit.flags.GlobalNameOverride`` -
useful when you want to globally modify output field names from pythonic `snake_style` to another naming convention
scheme (`camelCase`, `dasherized-names`, etc). Here's a few examples:

Expand All @@ -329,7 +335,7 @@ scheme (`camelCase`, `dasherized-names`, etc). Here's a few examples:
field_one: str
field_two: FoldedData
constructor, to_serializable = type_constructor & GLOBAL_NAME_OVERRIDE(inflection.camelize) ^ Data
constructor, to_serializable = TypeConstructor & GlobalNameOverride(inflection.camelize) ^ Data
data = Data(field_one='one',
field_two=FoldedData(field_three='three'))
Expand All @@ -349,24 +355,24 @@ the `serialized` dictionary will look like
}
``typeit.flags.NON_STRICT_PRIMITIVES`` -
``typeit.flags.NonStrictPrimitives`` -
disables strict checking of primitive types. With this flag, a type constructor for a structure
with a ``x: int`` attribute annotation would allow input values of ``x`` to be strings that could be parsed
as integer numbers. Without this flag, the type constructor will reject those values. The same rule is applicable
to combinations of floats, ints, and bools:

.. code-block:: python
construct, deconstruct = type_constructor ^ int
nonstrict_construct, nonstrict_deconstruct = type_constructor & NON_STRICT_PRIMITIVES ^ int
construct, deconstruct = TypeConstructor ^ int
nonstrict_construct, nonstrict_deconstruct = TypeConstructor & NonStrictPrimitives ^ int
construct('1') # raises typeit.Error
construct(1) # OK
nonstrict_construct('1') # OK
nonstrict_construct(1) # OK
``typeit.flags.SUM_TYPE_DICT`` - switches the way SumType is parsed and serialized. By default,
``typeit.flags.SumTypeDict`` - switches the way SumType is parsed and serialized. By default,
SumType is represented as a tuple of ``(<tag>, <payload>)`` in a serialized form. With this flag,
it will be represented and parsed from a dictionary:

Expand All @@ -386,7 +392,7 @@ override it with the following syntax:
.. code-block:: python
# Use "_type" as the key by which SumType's tag can be found in the mapping
mk_sum, serialize_sum = type_constructor & SUM_TYPE_DICT('_type') ^ int
mk_sum, serialize_sum = TypeConstructor & SumTypeDict('_type') ^ int
Here's an example how this flag changes the behaviour of the parser:
Expand All @@ -400,9 +406,9 @@ Here's an example how this flag changes the behaviour of the parser:
... number: str
... amount: str
...
>>> _, serialize_std_payment = typeit.type_constructor ^ Payment
>>> _, serialize_dict_payment = typeit.type_constructor & typeit.flags.SUM_TYPE_DICT ^ Payment
>>> _, serialize_dict_v2_payment = typeit.type_constructor & typeit.flags.SUM_TYPE_DICT('$type') ^ Payment
>>> _, serialize_std_payment = typeit.TypeConstructor ^ Payment
>>> _, serialize_dict_payment = typeit.TypeConstructor & typeit.flags.SumTypeDict ^ Payment
>>> _, serialize_dict_v2_payment = typeit.TypeConstructor & typeit.flags.SumTypeDict('$type') ^ Payment
>>>
>>> payment = Payment.Card(number='1111 1111 1111 1111', amount='10')
>>>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -42,7 +42,7 @@ def requirements(at_path: Path):
description='typeit brings typed data into your project',
long_description=README,
classifiers=[
'Development Status :: 4 - Beta',
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'License :: OSI Approved',
'License :: OSI Approved :: MIT License',
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/test_codegen.py
Expand Up @@ -49,14 +49,14 @@ def test_parser_github_pull_request_payload():
python_source, __ = cg.codegen_py(TypeitSchema(typ, overrides))
assert 'overrides' in python_source
assert "PullRequest.links: '_links'," in python_source
assert 'mk_main, serialize_main = type_constructor & overrides ^ Main' in python_source
assert 'mk_main, serialize_main = TypeConstructor & overrides ^ Main' in python_source

PullRequestType = get_type_hints(typ)['pull_request']

assert PullRequestType.links in overrides
assert overrides[PullRequestType.links] == '_links'

constructor, serializer = typeit.type_constructor(
constructor, serializer = typeit.TypeConstructor(
typ,
overrides=overrides
)
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/test_dataclasses.py
Expand Up @@ -13,7 +13,7 @@ class InventoryItem:
unit_price: float
quantity_on_hand: int = 0

mk_inv, serialize_inv = typeit.type_constructor(InventoryItem)
mk_inv, serialize_inv = typeit.TypeConstructor(InventoryItem)

serialized = {
'name': 'test',
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/test_extending.py
Expand Up @@ -36,7 +36,7 @@ def serialize(self, node, appstruct: Optional[Money]):
r = (appstruct.currency, appstruct.amount)
return super().serialize(node, r)

mk_x, serialize_x = ( typeit.type_constructor
mk_x, serialize_x = ( typeit.TypeConstructor
& MoneySchema[Money]
+ schema.types.Enum(Currency)
+ schema.primitives.NonStrictStr()
Expand Down

0 comments on commit 6e6affc

Please sign in to comment.