From 8fe826eeb56d7a9cc8c322564fb3a1a31f531776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 13 Feb 2023 22:05:46 +0100 Subject: [PATCH 1/3] Add documentation for ``cyclic-import`` Example of cyclic import where the cyclic import does not crash python --- doc/data/messages/c/cyclic-import/bad/bad.py | 8 ++++++++ doc/data/messages/c/cyclic-import/bad/bad2.py | 5 +++++ doc/data/messages/c/cyclic-import/details.rst | 4 +++- doc/data/messages/c/cyclic-import/good.py | 11 ++++++++++- 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 doc/data/messages/c/cyclic-import/bad/bad.py create mode 100644 doc/data/messages/c/cyclic-import/bad/bad2.py diff --git a/doc/data/messages/c/cyclic-import/bad/bad.py b/doc/data/messages/c/cyclic-import/bad/bad.py new file mode 100644 index 0000000000..d0b681d5c7 --- /dev/null +++ b/doc/data/messages/c/cyclic-import/bad/bad.py @@ -0,0 +1,8 @@ +def count_to_one(): + return 1 + + +def count_to_three(): + from .bad2 import count_to_two # [cyclic-import] + + return count_to_two() + 1 diff --git a/doc/data/messages/c/cyclic-import/bad/bad2.py b/doc/data/messages/c/cyclic-import/bad/bad2.py new file mode 100644 index 0000000000..5f6d6ed59a --- /dev/null +++ b/doc/data/messages/c/cyclic-import/bad/bad2.py @@ -0,0 +1,5 @@ +from .bad import count_to_one + + +def count_to_two(): + return count_to_one() + 1 diff --git a/doc/data/messages/c/cyclic-import/details.rst b/doc/data/messages/c/cyclic-import/details.rst index c1e9b976ac..5db72735cf 100644 --- a/doc/data/messages/c/cyclic-import/details.rst +++ b/doc/data/messages/c/cyclic-import/details.rst @@ -1 +1,3 @@ -You can help us make the doc better `by contributing `_ ! +The good code is just an example. There are various strategies to resolving +cyclic imports and the best choice relies heavily on the context of the code +and the affected modules. diff --git a/doc/data/messages/c/cyclic-import/good.py b/doc/data/messages/c/cyclic-import/good.py index c40beb573f..7924246ef8 100644 --- a/doc/data/messages/c/cyclic-import/good.py +++ b/doc/data/messages/c/cyclic-import/good.py @@ -1 +1,10 @@ -# This is a placeholder for correct code for this message. +def count_to_one(): + return 1 + + +def count_to_two(): + return count_to_one() + 1 + + +def count_to_three(): + return count_to_two() + 1 From cea16934a3e971a0d527ee7959f1833256c6639d Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 18 Jun 2023 13:16:07 -0400 Subject: [PATCH 2/3] Add __init__.py --- doc/data/messages/c/cyclic-import/bad/__init__.py | 0 doc/data/messages/c/cyclic-import/bad/bad.py | 2 +- doc/data/messages/c/cyclic-import/bad/bad2.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 doc/data/messages/c/cyclic-import/bad/__init__.py diff --git a/doc/data/messages/c/cyclic-import/bad/__init__.py b/doc/data/messages/c/cyclic-import/bad/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/doc/data/messages/c/cyclic-import/bad/bad.py b/doc/data/messages/c/cyclic-import/bad/bad.py index d0b681d5c7..dc37f89aa9 100644 --- a/doc/data/messages/c/cyclic-import/bad/bad.py +++ b/doc/data/messages/c/cyclic-import/bad/bad.py @@ -3,6 +3,6 @@ def count_to_one(): def count_to_three(): - from .bad2 import count_to_two # [cyclic-import] + from .bad2 import count_to_two # another cyclic-import return count_to_two() + 1 diff --git a/doc/data/messages/c/cyclic-import/bad/bad2.py b/doc/data/messages/c/cyclic-import/bad/bad2.py index 5f6d6ed59a..23417a821c 100644 --- a/doc/data/messages/c/cyclic-import/bad/bad2.py +++ b/doc/data/messages/c/cyclic-import/bad/bad2.py @@ -1,4 +1,4 @@ -from .bad import count_to_one +from .bad import count_to_one # [cyclic-import] def count_to_two(): From 8e5f746083a70cc530c2797c79a9456a43711e2c Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 18 Jun 2023 13:17:14 -0400 Subject: [PATCH 3/3] Remove stray comment --- doc/data/messages/c/cyclic-import/bad/bad.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/data/messages/c/cyclic-import/bad/bad.py b/doc/data/messages/c/cyclic-import/bad/bad.py index dc37f89aa9..75e5e2bce0 100644 --- a/doc/data/messages/c/cyclic-import/bad/bad.py +++ b/doc/data/messages/c/cyclic-import/bad/bad.py @@ -3,6 +3,6 @@ def count_to_one(): def count_to_three(): - from .bad2 import count_to_two # another cyclic-import + from .bad2 import count_to_two return count_to_two() + 1