How to use _register_from_parametrized_dtype method in pandera

Best Python code snippet using pandera_python

engine.py

Source:engine.py Github

copy

Full Screen

...71 raise ValueError(72 f"Subclasses of {base_names} cannot be registered"73 f" with {cls.__name__}."74 )75 def _register_from_parametrized_dtype(76 cls,77 pandera_dtype_cls: Type[DataType],78 ) -> None:79 method = pandera_dtype_cls.__dict__["from_parametrized_dtype"]80 if not isinstance(method, classmethod):81 raise ValueError(82 f"{pandera_dtype_cls.__name__}.from_parametrized_dtype "83 + "must be a classmethod."84 )85 func = method.__func__86 annotations = get_type_hints(func).values()87 dtype = next(iter(annotations)) # get 1st annotation88 # parse typing.Union89 dtypes = typing_inspect.get_args(dtype) or [dtype]90 def _method(*args, **kwargs):91 return func(pandera_dtype_cls, *args, **kwargs)92 for source_dtype in dtypes:93 cls._check_source_dtype(source_dtype)94 cls._registry[cls].dispatch.register(source_dtype, _method)95 def _register_equivalents(96 cls, pandera_dtype_cls: Type[DataType], *source_dtypes: Any97 ) -> None:98 pandera_dtype = pandera_dtype_cls() # type: ignore99 for source_dtype in source_dtypes:100 cls._check_source_dtype(source_dtype)101 cls._registry[cls].equivalents[source_dtype] = pandera_dtype102 def register_dtype(103 cls: _EngineType,104 pandera_dtype_cls: Type[_DataType] = None,105 *,106 equivalents: Optional[List[Any]] = None,107 ) -> Callable:108 """Register a Pandera :class:`~pandera.dtypes.DataType` with the engine,109 as class decorator.110 :param pandera_dtype: The DataType to register.111 :param equivalents: Equivalent scalar data type classes or112 non-parametrized data type instances.113 .. note::114 The classmethod ``from_parametrized_dtype`` will also be115 registered. See :ref:`here<dtypes>` for more usage details.116 :example:117 >>> import pandera as pa118 >>>119 >>> class MyDataType(pa.DataType):120 ... pass121 >>>122 >>> class MyEngine(123 ... metaclass=pa.engines.engine.Engine,124 ... base_pandera_dtypes=MyDataType,125 ... ):126 ... pass127 >>>128 >>> @MyEngine.register_dtype(equivalents=[bool])129 ... class MyBool(MyDataType):130 ... pass131 """132 def _wrapper(pandera_dtype_cls: Type[_DataType]) -> Type[_DataType]:133 if not inspect.isclass(pandera_dtype_cls):134 raise ValueError(135 f"{cls.__name__}.register_dtype can only decorate a class,"136 f" got {pandera_dtype_cls}"137 )138 if equivalents:139 cls._register_equivalents(pandera_dtype_cls, *equivalents)140 if "from_parametrized_dtype" in pandera_dtype_cls.__dict__:141 cls._register_from_parametrized_dtype(pandera_dtype_cls)142 cls._registered_dtypes.add(pandera_dtype_cls)143 return pandera_dtype_cls144 if pandera_dtype_cls:145 return _wrapper(pandera_dtype_cls)146 return _wrapper147 def dtype(cls: _EngineType, data_type: Any) -> _DataType:148 """Convert input into a Pandera :class:`DataType` object."""149 if isinstance(data_type, cls._base_pandera_dtypes):150 return data_type151 if inspect.isclass(data_type) and issubclass(152 data_type, cls._base_pandera_dtypes153 ):154 try:155 return data_type()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful