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.5.0.tar.gz (10.4 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.5.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: coerce_type-1.5.0.tar.gz
  • Upload date:
  • Size: 10.4 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.5.0.tar.gz
Algorithm Hash digest
SHA256 7a06fdf08adfd8141ff7b60d553dd9ea0e32037dbd9279fdb1b8dc01c6dcc489
MD5 9986b2c4903a41fa1ce45ca8dbc03d00
BLAKE2b-256 bb84f9b238c90e1550bda1338123acba1702f25244f227a06f61d12344326b4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for coerce_type-1.5.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.5.0-py3-none-any.whl.

File metadata

  • Download URL: coerce_type-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1778f1fa9da20200c9b0758256ae50a0d581deff833a5dba7d69f0add00b4ab
MD5 7ab6f93dc0470caa6137095d11815345
BLAKE2b-256 410fa5b56457391d3a9813af1e200fd494ad7d4e06dd34230060e9112fb52651

See more details on using hashes here.

Provenance

The following attestation bundles were made for coerce_type-1.5.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