Best Python code snippet using pandera_python
test_static_type_checking.py
Source:test_static_type_checking.py
...13import pytest14import pandera as pa15from tests.mypy.modules import pandas_dataframe16test_module_dir = Path(os.path.dirname(__file__))17def _get_mypy_errors(stdout) -> typing.Dict[int, typing.Dict[str, str]]:18 """Parse line number and error message."""19 errors: typing.Dict[int, typing.Dict[str, typing.Any]] = {}20 # last line is summary of errors21 for error in [x for x in stdout.split("\n") if x != ""][:-1]:22 matches = re.match(23 r".+\.py:(?P<lineno>\d+): error: (?P<msg>.+) \[(?P<errcode>.+)\]",24 error,25 )26 if matches is not None:27 match_dict = matches.groupdict()28 errors[int(match_dict["lineno"])] = {29 "msg": match_dict["msg"],30 "errcode": match_dict["errcode"],31 }32 return errors33def test_mypy_pandas_dataframe(capfd) -> None:34 """Test that mypy raises expected errors on pandera-decorated functions."""35 # pylint: disable=subprocess-run-check36 subprocess.run(37 [38 sys.executable,39 "-m",40 "mypy",41 str(test_module_dir / "modules" / "pandas_dataframe.py"),42 ],43 text=True,44 )45 errors = _get_mypy_errors(capfd.readouterr().out)46 # assert error messages on particular lines of code47 assert errors[35] == {48 "msg": (49 'Argument 1 to "pipe" of "NDFrame" has incompatible type '50 '"Type[DataFrame[Any]]"; expected '51 '"Union[Callable[..., DataFrame[SchemaOut]], '52 'Tuple[Callable[..., DataFrame[SchemaOut]], str]]"'53 ),54 "errcode": "arg-type",55 }56 assert errors[41] == {57 "msg": (58 "Incompatible return value type (got "59 '"pandas.core.frame.DataFrame", expected '60 '"pandera.typing.pandas.DataFrame[SchemaOut]")'61 ),62 "errcode": "return-value",63 }64 assert errors[54] == {65 "msg": (66 'Argument 1 to "fn" has incompatible type '67 '"pandas.core.frame.DataFrame"; expected '68 '"pandera.typing.pandas.DataFrame[Schema]"'69 ),70 "errcode": "arg-type",71 }72 assert errors[58] == {73 "msg": (74 'Argument 1 to "fn" has incompatible type '75 '"DataFrame[AnotherSchema]"; expected "DataFrame[Schema]"'76 ),77 "errcode": "arg-type",78 }79@pytest.mark.parametrize(80 "fn",81 [82 pandas_dataframe.fn_mutate_inplace,83 pandas_dataframe.fn_assign_and_get_index,84 pandas_dataframe.fn_cast_dataframe_invalid,85 ],86)87def test_pandera_runtime_errors(fn) -> None:88 """Test that pandera catches cases that mypy doesn't catch."""89 # both functions don't add a required column "age"90 try:91 fn(pandas_dataframe.schema_df)92 except pa.errors.SchemaError as e:93 assert e.failure_cases["failure_case"].item() == "age"94# pylint: disable=line-too-long95PANDAS_CONCAT_FALSE_POSITIVES = {96 13: {97 "msg": 'No overload variant of "concat" matches argument type "Generator[DataFrame, None, None]"', # noqa98 "errcode": "call-overload",99 },100 16: {101 "msg": 'No overload variant of "concat" matches argument type "Generator[Series, None, None]"', # noqa102 "errcode": "call-overload",103 },104}105PANDAS_TIME_FALSE_POSITIVES = {106 4: {107 "msg": 'Unsupported operand types for + ("Timestamp" and "YearEnd")', # noqa108 "errcode": "operator",109 },110 6: {111 "msg": 'Missing positional argument "value" in call to "Timedelta"', # noqa112 "errcode": "call-arg",113 },114 9: {115 "msg": 'Missing positional argument "value" in call to "Timedelta"', # noqa116 "errcode": "call-arg",117 },118 10: {119 "msg": 'Argument 1 to "Timedelta" has incompatible type "float"; expected "Union[Timedelta, timedelta, timedelta64, str, int]"', # noqa120 "errcode": "arg-type",121 },122}123@pytest.mark.parametrize(124 "module,config,expected_errors",125 [126 ["pandas_concat.py", None, PANDAS_CONCAT_FALSE_POSITIVES],127 ["pandas_concat.py", "plugin_mypy.ini", {}],128 ["pandas_time.py", None, PANDAS_TIME_FALSE_POSITIVES],129 ["pandas_time.py", "plugin_mypy.ini", {}],130 ],131)132def test_pandas_stubs_false_positives(133 capfd,134 module,135 config,136 expected_errors,137) -> None:138 """Test pandas-stubs type stub false positives."""139 if config is None:140 cache_dir = str(test_module_dir / ".mypy_cache" / "test-mypy-default")141 else:142 cache_dir = str(test_module_dir / ".mypy_cache" / f"test-{config}")143 commands = [144 sys.executable,145 "-m",146 "mypy",147 str(test_module_dir / "modules" / module),148 "--cache-dir",149 cache_dir,150 ]151 if config:152 commands += ["--config-file", str(test_module_dir / "config" / config)]153 # pylint: disable=subprocess-run-check154 subprocess.run(155 commands,156 text=True,157 )158 resulting_errors = _get_mypy_errors(capfd.readouterr().out)159 assert resulting_errors == expected_errors160@pytest.mark.parametrize("module", ["pandas_concat", "pandas_time"])161def test_pandas_modules_importable(module):162 """Make sure that static type linting modules can be executed."""...
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!!