From 36c53ceaa3e72876d4b438e124fc90a2cbc4ecef Mon Sep 17 00:00:00 2001 From: Stefan Scherfke Date: Sat, 2 Apr 2022 16:11:48 +0200 Subject: [PATCH] Fix pytest crashes with hypothesis and pydantic (#3727) Pytest (sometimes?) crashes when it is invoked with `-vv` and pydantic and hypthesis are installed. This is because `_registered(typ)` modifies `_DEFINED_TYPES` while it is being iterated: ``` INTERNALERROR> File ".../lib/python3.9/site-packages/pydantic/_hypothesis_plugin.py", line 361, in INTERNALERROR> for typ in pydantic.types._DEFINED_TYPES: INTERNALERROR> File ".../lib/python3.9/_weakrefset.py", line 65, in __iter__ INTERNALERROR> for itemref in self.data: INTERNALERROR> RuntimeError: Set changed size during iteration ``` --- pydantic/_hypothesis_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydantic/_hypothesis_plugin.py b/pydantic/_hypothesis_plugin.py index 79d787e..890e192 100644 --- a/pydantic/_hypothesis_plugin.py +++ b/pydantic/_hypothesis_plugin.py @@ -358,7 +358,7 @@ def resolve_constr(cls): # type: ignore[no-untyped-def] # pragma: no cover # Finally, register all previously-defined types, and patch in our new function -for typ in pydantic.types._DEFINED_TYPES: +for typ in list(pydantic.types._DEFINED_TYPES): _registered(typ) pydantic.types._registered = _registered st.register_type_strategy(pydantic.Json, resolve_json)