From 1ee9737cf5462a7867bc25db5ed558d525283f2b Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Fri, 12 Feb 2021 10:46:34 +0000 Subject: [PATCH] make hypothesis optional for testing (#2343) * make hypothesis optional for testing * fix coveragin in "_hypothesis_plugin.py" * uncomment ci checks --- pydantic/_hypothesis_plugin.py | 4 ++-- tests/conftest.py | 7 ++++++- tests/test_hypothesis_plugin.py | 12 +++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pydantic/_hypothesis_plugin.py b/pydantic/_hypothesis_plugin.py index 6de9a57..1cfa7c1 100644 --- a/pydantic/_hypothesis_plugin.py +++ b/pydantic/_hypothesis_plugin.py @@ -59,7 +59,7 @@ else: try: email_validator.validate_email(s, check_deliverability=False) return True - except email_validator.EmailNotValidError: + except email_validator.EmailNotValidError: # pragma: no cover return False # Note that these strategies deliberately stay away from any tricky Unicode @@ -217,7 +217,7 @@ def resolves( def resolve_json(cls): # type: ignore[no-untyped-def] try: inner = st.none() if cls.inner_type is None else st.from_type(cls.inner_type) - except Exception: + except Exception: # pragma: no cover finite = st.floats(allow_infinity=False, allow_nan=False) inner = st.recursive( base=st.one_of(st.none(), st.booleans(), st.integers(), finite, st.text()), diff --git a/tests/conftest.py b/tests/conftest.py index c562a8d..63fa53e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,7 +10,12 @@ import pytest from _pytest.assertion.rewrite import AssertionRewritingHook # See https://hypothesis.readthedocs.io/en/latest/strategies.html#interaction-with-pytest-cov -pytest_plugins = ['hypothesis.extra.pytestplugin'] +try: + import hypothesis # noqa +except ImportError: + pytest_plugins = [] +else: + pytest_plugins = ['hypothesis.extra.pytestplugin'] def _extract_source_code_from_function(function): diff --git a/tests/test_hypothesis_plugin.py b/tests/test_hypothesis_plugin.py index ed52570..d455520 100644 --- a/tests/test_hypothesis_plugin.py +++ b/tests/test_hypothesis_plugin.py @@ -1,11 +1,21 @@ import typing import pytest -from hypothesis import given, strategies as st import pydantic from pydantic.networks import import_email_validator +try: + from hypothesis import given, strategies as st +except ImportError: + + def given(*args, **kwargs): + return lambda f: f + + st = type('st', (), {'data': lambda: None}) + + pytestmark = pytest.mark.skipif(True, reason='"hypothesis" not installed') + def gen_models(): class MiscModel(pydantic.BaseModel):