Skip to content

Commit

Permalink
Merge pull request #3550 from JensHeinrich/feature/handle-from_regex-…
Browse files Browse the repository at this point in the history
…import
  • Loading branch information
Zac-HD committed Feb 17, 2023
2 parents 173de52 + 288860a commit b3c3ebe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -78,6 +78,7 @@ their individual contributions.
* `Jakub Nabaglo <https://github.com/nbgl>`_ (j@nab.gl)
* `James Lamb <https://github.com/jameslamb>`_
* `Jenny Rouleau <https://github.com/jennyrou>`_
* `Jens Heinrich <https://github.com/JensHeinrich>`_
* `Jeremy Thurgood <https://github.com/jerith>`_
* `J.J. Green <http://soliton.vm.bytemark.co.uk/pub/jjg/>`_
* `JP Viljoen <https://github.com/froztbyte>`_ (froztbyte@froztbyte.net)
Expand Down
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
@@ -0,0 +1,5 @@
RELEASE_TYPE: patch

This patch fixes missing imports of the :mod:`re` module, when :doc:`ghostwriting <ghostwriter>`
tests which include compiled patterns or regex flags.
Thanks to Jens Heinrich for reporting and promptly fixing this bug!
12 changes: 10 additions & 2 deletions hypothesis-python/src/hypothesis/extra/ghostwriter.py
Expand Up @@ -597,8 +597,16 @@ def _imports_for_strategy(strategy):
# If we have a lazy from_type strategy, because unwrapping it gives us an
# error or invalid syntax, import that type and we're done.
if isinstance(strategy, LazyStrategy):
if strategy.function is st.from_type:
return _imports_for_object(strategy._LazyStrategy__args[0])
if strategy.function.__name__ in (
st.from_type.__name__,
st.from_regex.__name__,
):
return {
imp
for arg in set(strategy._LazyStrategy__args)
| set(strategy._LazyStrategy__kwargs.values())
for imp in _imports_for_object(arg)
}
elif _get_module(strategy.function).startswith("hypothesis.extra."):
return {(_get_module(strategy.function), strategy.function.__name__)}
module = _get_module(strategy.function).replace("._array_helpers", ".numpy")
Expand Down
11 changes: 11 additions & 0 deletions hypothesis-python/tests/ghostwriter/test_ghostwriter.py
Expand Up @@ -42,6 +42,7 @@
from hypothesis.extra import cli, ghostwriter
from hypothesis.internal.compat import BaseExceptionGroup
from hypothesis.strategies import builds, from_type, just, lists
from hypothesis.strategies._internal.core import from_regex
from hypothesis.strategies._internal.lazy import LazyStrategy

varied_excepts = pytest.mark.parametrize("ex", [(), ValueError, (TypeError, re.error)])
Expand Down Expand Up @@ -431,6 +432,16 @@ def test_unrepr_identity_elem():
lists(builds(Decimal)),
{("decimal", "Decimal")},
),
# find the needed import for from_regex if needed
(
from_regex(re.compile(".+")),
{"re"},
),
# but don't add superfluous imports
(
from_regex(".+"),
set(),
),
],
)
def test_get_imports_for_strategy(strategy, imports):
Expand Down

0 comments on commit b3c3ebe

Please sign in to comment.