Skip to main content

A library providing tools to enforce type hints.

Project description

coerce_type

coerce_type is a library for the type coercion of values to the annotations they have. It can be triggered manually with coerce or be set to happen automatically for a dataclass by having it inherit a TypeCorrecting class

Installation

pip install coerce_type

Usage

There are two main uses of this library: The coerce function and the TypeCorrecting class constructor.

coerce

>>> from coerce_type import coerce
>>> target_type = int
>>> value = "1"
>>> coerce(value, target_type)
1

TypeCorrecting

>>>import datetime from coerce_type import TypeCorrecting, TypeCorrectingType
>>> from dataclasses import dataclass
>>> from datetime import date
>>> @dataclass()
>>> class Example(TypeCorrecting()):
>>>     a: int
>>>     b: datetime.date
>>> data = {
>>>     "a": "1",
>>>     "b": "2022-01-01"
>>> }
>>> example = Example(**data)
>>> example.a
1
>>> example.b
datetime.date(2022, 1, 1)
>>> isinstance(example, TypeCorrectingType)
True
>>> issubclass(Example,TypeCorrectingType)
True

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

coerce_type-1.3.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coerce_type-1.3.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file coerce_type-1.3.0.tar.gz.

File metadata

  • Download URL: coerce_type-1.3.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for coerce_type-1.3.0.tar.gz
Algorithm Hash digest
SHA256 97d82b6a1d5748bacf5e36f7459854ef593fe1451396ec4d85e22faaf027412b
MD5 c06f9db951ac3ef266f6920ead7ad1fe
BLAKE2b-256 0d1e19dc0971fb2c050d8010ba6eca6260e594c222bd5edc99ac6cca954dd416

See more details on using hashes here.

Provenance

The following attestation bundles were made for coerce_type-1.3.0.tar.gz:

Publisher: publish.yml on reyemDarnok/coerce_type

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file coerce_type-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: coerce_type-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for coerce_type-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df549aa502e9be224b57874ce9fd00cc1c9467d71f66a9ff6d42033791c0872f
MD5 c6b6a05be9f2ab76538925dc3d924ee5
BLAKE2b-256 15e6a5f978e8344318c301cf9398f47f6f6fb9082329b21d7cce631a1b5eb087

See more details on using hashes here.

Provenance

The following attestation bundles were made for coerce_type-1.3.0-py3-none-any.whl:

Publisher: publish.yml on reyemDarnok/coerce_type

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page