false negative on sending value to inferred Generator type
Describe the bug
when Generator is inferred for function return type, it's second type parameter is None. but value of yield expression can be assigned to non-None type.
To Reproduce
from typing_extensions import reveal_type
def f():
x: int = yield 3
yield x
reveal_type(f)
g = f()
reveal_type(next(g))
reveal_type(g.send(None))
t.py:7:13 - information: Type of "f" is "() -> Generator[int, None, None]"
t.py:10:13 - information: Type of "next(g)" is "int"
t.py:11:13 - information: Type of "g.send(None)" is "int" # <----
$ python t.py
Runtime type is 'function'
Runtime type is 'int'
Runtime type is 'NoneType' # <----
Expected behavior
one of followings:
- infer
fas() -> Generator[int, int, None]from lhs type of assignment operator and reportg.send(None)as error - infer
fas() -> Generator[int, None, None](same as current behavior) and reportx: int = yield ...as error
VS Code extension or command-line
command-line tool, 1.1.283
Pyright always infers None for the second type argument for an inferred Generator return type. It would probably be more correct to infer Unknown in this case since there's no practical way to know whether None is the right answer. But inferring Unknown would mean that all strictly-typed programs would need to explicitly declare the return type of their Generator, which would be a significant hassle.
I think there are three options here:
- Leave the behavior as is. This admittedly creates a typing hole with the possibility of false negatives.
- Continue to infer the second
Generatortype argument asNonebut enforce this inferred type within the function body. This would generate a type error, but it could be confusing. It would be resolved by providing an explicitGeneratorreturn type annotation. - Infer the second
Generatortype argument asUnknown. As mentioned above, this will generate a lot of noise for strictly-typed programs and will require that the return type of every generator function be explicitly annotated to avoid strict-mode errors.
I'll need to think more about the best approach.
I personally think 2 is the best one.
(and it will be helpful if message like "annotate Generator type explicitly!" is shown on type errors around yield expression)
I tried implementing both 2 and 3.
Option 2 is problematic because it's inconsistent with the way return type inference works in every other case within pyright. In all other cases, the inferred return type is based on the body of the function. If analysis of the body is unable to produce a fully-known type, then Unknown (an implied Any) is produced. Option 2 reverses that and says "I'm going to produce a known inferred return type and then enforce that inferred type within the body of the function". That is backward, and upon further thought, I don't think that we should introduce this inconstency.
As I feared, option 3 produces a huge amount of noise within strictly-typed code bases. It produced 21 false positive errors in my team's code base alone.
For these reasons, I think that option 1 (leave the behavior as is) is the preferred option.
If you were reporting a false positive error, I think it would be a higher priority to fix, but since it's a false negative and there's an easy workaround (namely, add a return type annotation and don't rely on inference), I think this is a reasonable approach. We could revisit this in the future if we get additional signal from pyright users that this is a problem.