Skip to main content

Wrappers for typing & pydantic models; module loading deferred till __init__.

Project description

lazytype

Write type hints, implement optional features, define pydantic models without up-front import delays, and more without having to import slow modules; using a lazytype.LazyType, you can wrap a slow-loading class so that its module doesn't load until you instantiate it or run an actual type check.

Pydantic models are also supported through the LazyField interface, though you don't need to have Pydantic installed to use LazyTypes.

Installation

For users:

pip install lazytype

Optionally make sure pydantic is installed with the pydantic option (if you plan to use LazyFields):

pip install lazytype[pydantic]

For developers, clone this repository, change to its directory, and run:

flit install --symlink

Examples

Regular LazyTypes

The interface is the same as the wrapped object:

>>> a = LazyType['numpy.ndarray']((3, 2))
>>> a
<Lazy array([[-1.49166815e-154, -2.68679856e+154],
                [ 1.48219694e-323,  0.00000000e+000],
                [ 0.00000000e+000,  4.17201348e-309]])>
>>> a._instance
array([[-2.00000000e+000,  2.32036240e+077],
        [ 1.48219694e-323,  0.00000000e+000],
        [ 0.00000000e+000,  4.17201348e-309]])
>>> a[:] = 0
>>> a
array([[0., 0.],
        [0., 0.],
        [0., 0.]])
>>> a.dtype
dtype('float64')
>>> isinstance(a._instance, type(a))
True

You can optionally require that a check run for the module availability at definition time:

# this works if you have "numpy" installed
>>> LazyArray = LazyType['numpy.ndarray', 'strict':True]
lazytype.Lazyndarray

# this doesn't (unless you have a package called "numpay"...)
>>> LazyArray = LazyType['numpay.ndarray', 'strict':True]
ImportError: Strict check for module numpay availability failed

pydantic Fields with LazyFields

You can also use LazyTypes with Pydantic models to specify data types and validators. This requires some extra methods provided by the LazyField class; you can specify any built-in Pydantic field-type to use for schema validation, followed by the arguments you would use to create a new LazyType. This allows you to map an existing field-type to a custom, slow-loading field-type that can trivially accept the same input arguments, all without loading the wrapped class's module until your model is instantiated.

Create a lazy-loading field for astropy.time.Time using the built-in datetime string validator:

>>> from datetime import datetime
>>> LazyField[datetime, 'astropy.time.Time', 'strict':True]
lazytype.LazyFieldTime

Actually use the field in a pydantic model:

>>> from pydantic import BaseModel
>>> class LazyTest(BaseModel):
...     foo: str
...     time: LazyField[datetime, 'astropy.time.Time', 'strict':True]

See the JSON schema of the resulting model:

>>> LazyTest.schema()
{'title': 'LazyTest',
 'type': 'object',
 'properties': {'foo': {'title': 'Foo', 'type': 'string'},
  'time': {'title': 'Time', 'type': 'string', 'format': 'date-time'}},
 'required': ['foo', 'time']}

Actually instantiate something, forcing astropy.time.Time to load:

>>> t = LazyTest(foo='bar', time='2019-11-29 13:40:29.197')
>>> t.time
<Lazy <Time object: scale='utc' format='iso' value=2019-11-29 13:40:29.197>>
>>> t.time.gps
1259070047.197

Provide additional schema annotations, e.g. providing an example input value:

>>> class LazyTest(BaseModel):
...     foo: str
...     time: LazyField[str::{'example': '2019-11-29 13:40:29.197'},
...                     'astropy.time.Time', 'strict':True]
>>> LazyTest.schema()
{'title': 'LazyTest',
 'type': 'object',
 'properties': {'foo': {'title': 'Foo', 'type': 'string'},
  'time': {'title': 'Time',
   'type': 'string',
   'example': '2019-11-29 13:40:29.197'}},
 'required': ['foo', 'time']}

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

lazytype-0.2.1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

lazytype-0.2.1-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file lazytype-0.2.1.tar.gz.

File metadata

  • Download URL: lazytype-0.2.1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.24.0

File hashes

Hashes for lazytype-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7f9b121b0d98e82b984c6c506c5f6130c36bb67aa1be907c1830aca693bae20e
MD5 c0a627ea1097c78b3f3997242efdd05c
BLAKE2b-256 292dfbd200e39ddb2637275f8698417823abedeb231cea39805982500ee279c4

See more details on using hashes here.

File details

Details for the file lazytype-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: lazytype-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.24.0

File hashes

Hashes for lazytype-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 453ace11b87c505a4afa10dc30d78e6c6121fb986611fb25fb5e8b3ad4f5e4c1
MD5 02c3b303a967beabc228d5b533a55fb7
BLAKE2b-256 334cb4ff378ec659a8a7dc9b1ddaa9d028b45817c9cd194faac465a58956cc7f

See more details on using hashes here.

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