79 lines
1.9 KiB
Plaintext
79 lines
1.9 KiB
Plaintext
== Why is this an issue?
|
|
|
|
As soon as the ``++yield++`` keyword is used the enclosing method or function becomes a generator. Thus ``++yield++`` should never be used in a function or method which is not intended to be a generator.
|
|
|
|
|
|
This rule raises an issue when ``++yield from++`` or ``++yield++`` are used in a function or method which is not a generator because:
|
|
|
|
* the function/method's return type annotation is not https://docs.python.org/3/library/typing.html#typing.Generator[``++typing.Generator[...]++``]
|
|
* it is a special method which can never be a generator (ex: ``++__init__++``).
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,python]
|
|
----
|
|
class A:
|
|
def __init__(self, value):
|
|
self.value = value
|
|
yield value # Noncompliant
|
|
|
|
def mylist2() -> List[str]:
|
|
yield ['string'] # Noncompliant. Return should be used instead of yield
|
|
|
|
def generator_ok() -> Generator[int, float, str]:
|
|
sent = yield 42
|
|
return '42'
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,python]
|
|
----
|
|
class A:
|
|
def __init__(self, value):
|
|
self.value = value
|
|
|
|
def mylist2() -> List[str]:
|
|
return ['string']
|
|
|
|
def generator_ok() -> Generator[int, float, str]:
|
|
sent = yield 42
|
|
return '42'
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://docs.python.org/3/library/typing.html[Python documentation - Support for type hints]
|
|
* https://docs.python.org/3/reference/datamodel.html#special-method-names[Python documentation - Special method names]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
* Remove this "yield" statement
|
|
* Replace this "yield" keyword with "return" or change the return type annotation.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
Primary: the "yield" keyword
|
|
|
|
Secondary: function/method's return type annotation if there is one
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== is related to: S2734
|
|
|
|
endif::env-github,rspecator-view[]
|