Package: lazytype
lazytypeWrite type hints, implement optional features, definepydanticmodels without
up-front import delays, and more without having to import slow modules; using alazytype.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.Pydanticmodels are also supported
through theLazyFieldinterface, though you don't need to have Pydantic
installed to useLazyTypes.InstallationFor users:pipinstalllazytypeOptionally make surepydanticis installed with thepydanticoption (if you
plan to useLazyFields):pipinstalllazytype[pydantic]For developers, clone this repository, change to its directory, and run:flitinstall--symlinkExamplesRegularLazyTypesThe interface is the same as the wrapped object:>>>a=LazyType['numpy.ndarray']((3,2))>>>a<Lazyarray([[-1.49166815e-154,-2.68679856e+154],[1.48219694e-323,0.00000000e+000],[0.00000000e+000,4.17201348e-309]])>>>>a._instancearray([[-2.00000000e+000,2.32036240e+077],[1.48219694e-323,0.00000000e+000],[0.00000000e+000,4.17201348e-309]])>>>a[:]=0>>>aarray([[0.,0.],[0.,0.],[0.,0.]])>>>a.dtypedtype('float64')>>>isinstance(a._instance,type(a))TrueYou 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:StrictcheckformodulenumpayavailabilityfailedpydanticFields withLazyFieldsYou can also useLazyTypes with Pydantic models to specify data types and
validators. This requires some extra methods provided by theLazyFieldclass;
you can specify anybuilt-in Pydantic
field-typeto use for schema
validation, followed by the arguments you would use to create a newLazyType. 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 forastropy.time.Timeusing the built-indatetimestring validator:>>>fromdatetimeimportdatetime>>>LazyField[datetime,'astropy.time.Time','strict':True]lazytype.LazyFieldTimeActually use the field in apydanticmodel:>>>frompydanticimportBaseModel>>>classLazyTest(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, forcingastropy.time.Timeto load:>>>t=LazyTest(foo='bar',time='2019-11-29 13:40:29.197')>>>t.time<Lazy<Timeobject:scale='utc'format='iso'value=2019-11-2913:40:29.197>>>>>t.time.gps1259070047.197Provide additional schema annotations, e.g. providing an example input
value:>>>classLazyTest(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']}