
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
71 lines
1.8 KiB
Plaintext
71 lines
1.8 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Functions that use ``++yield++`` are known as "generators". Before Python 3.3, generators cannot ``++return++`` values. Similarly, functions that use ``++return++`` cannot use ``++yield++``. Doing so will cause a ``++SyntaxError++``.
|
|
|
|
|
|
Either upgrade your version of Python to a version >= 3.3, or don't use both return and yield in a function.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,python]
|
|
----
|
|
def adder(n):
|
|
num = 0
|
|
while num < n:
|
|
yield num
|
|
num += 1
|
|
return num #Noncompliant
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use only "return" or only "yield", not both.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 17 Mar 2015, 08:41:25 Elena Vilchik wrote:
|
|
\[~ann.campbell.2]
|
|
|
|
1. We should focus in rule description that "return" contains argument. "return" without argument can be used in generator.
|
|
|
|
2. About labels: may be we should create tag for syntax error, and to use it in all such python rules. For example "syntax". What do you think?
|
|
|
|
=== on 17 Mar 2015, 17:48:41 Ann Campbell wrote:
|
|
\[~elena.vilchik] I've updated the description. See if it's enough.
|
|
|
|
|
|
I'm not wild about the proposed tag.
|
|
|
|
=== on 27 Nov 2019, 14:52:09 Nicolas Harraudeau wrote:
|
|
Deprecating this rule because it is possible to use ``++return++`` in a generator since Python 3.3, which was released on September 29, 2012.
|
|
|
|
|
|
The following code:
|
|
|
|
----
|
|
return 42
|
|
----
|
|
is equivalent to
|
|
|
|
----
|
|
raise StopIteration(42)
|
|
----
|
|
|
|
See https://www.python.org/dev/peps/pep-0380/#formal-semantics[PEP 380] for more information.
|
|
|
|
|
|
Note also that https://www.python.org/doc/sunset-python-2/[official python 2 support will be stopped on January 1, 2020], thus there is no point in having this rule enabled by default.
|
|
|
|
endif::env-github,rspecator-view[]
|