Best Python code snippet using pandera_python
engine.py
Source:engine.py  
...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()...test_engine.py
Source:test_engine.py  
...34    with pytest.raises(35        TypeError, match="Data type 'foo' not understood by FakeEngine"36    ):37        engine.dtype("foo")38def test_register_from_parametrized_dtype(engine: Engine):39    """Test that a dtype with from_parametrized_dtype can be registered."""40    @engine.register_dtype41    class _Dtype(BaseDataType):42        @classmethod43        def from_parametrized_dtype(cls, x: int):44            return x45    assert engine.dtype(42) == 4246    with pytest.raises(47        TypeError, match="Data type 'foo' not understood by FakeEngine"48    ):49        engine.dtype("foo")50def test_register_from_parametrized_dtype_union(engine: Engine):51    """Test that a dtype with from_parametrized_dtype and Union annotation52    can be registered.53    """54    @engine.register_dtype55    class _Dtype(BaseDataType):56        @classmethod57        def from_parametrized_dtype(cls, x: Union[int, str]):58            return x59    assert engine.dtype(42) == 4260def test_register_notclassmethod_from_parametrized_dtype(engine: Engine):61    """Test that a dtype with invalid from_parametrized_dtype62    cannot be registered.63    """64    with pytest.raises(65        ValueError,66        match="_InvalidDtype.from_parametrized_dtype must be a classmethod.",67    ):68        @engine.register_dtype69        class _InvalidDtype(BaseDataType):70            def from_parametrized_dtype(  # pylint:disable=no-self-argument,no-self-use71                cls, x: int72            ):73                return x74def test_register_dtype_complete(engine: Engine, equivalents: List[Any]):75    """Test that a dtype with equivalents and from_parametrized_dtype76    can be registered.77    """78    @engine.register_dtype(equivalents=equivalents)79    class _Dtype(BaseDataType):80        @classmethod81        def from_parametrized_dtype(cls, x: Union[int, str]):82            return x83    assert engine.dtype(42) == 4284    assert engine.dtype("foo") == "foo"85    for equivalent in equivalents:86        assert engine.dtype(equivalent) == _Dtype()87    with pytest.raises(88        TypeError,89        match="Data type '<class 'str'>' not understood by FakeEngine",90    ):91        engine.dtype(str)92def test_register_dtype_overwrite(engine: Engine):93    """Test that register_dtype overwrites existing registrations."""94    @engine.register_dtype(equivalents=["foo"])95    class _DtypeA(BaseDataType):96        @classmethod97        def from_parametrized_dtype(cls, x: Union[int, str]):98            return _DtypeA()99    assert engine.dtype("foo") == _DtypeA()100    assert engine.dtype("bar") == _DtypeA()101    assert engine.dtype(42) == _DtypeA()102    @engine.register_dtype(equivalents=["foo"])103    class _DtypeB(BaseDataType):104        @classmethod105        def from_parametrized_dtype(cls, x: int):106            return _DtypeB()107    assert engine.dtype("foo") == _DtypeB()108    assert engine.dtype("bar") == _DtypeA()109    assert engine.dtype(42) == _DtypeB()110def test_register_base_pandera_dtypes():111    """Test that base datatype cannot be registered."""112    class FakeEngine(  # pylint:disable=too-few-public-methods113        metaclass=Engine, base_pandera_dtypes=(BaseDataType, BaseDataType)114    ):115        pass116    with pytest.raises(117        ValueError,118        match=re.escape(119            "Subclasses of ['tests.core.test_engine.BaseDataType', "...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
