-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect errors for fstrings + generators #3385
Comments
Strangely, this is fine: def stringify5(it: Iterator[Tuple[int, int]]) -> Iterator[str]:
return (f"({x}, {y})" for x, y in it) ... but this is not: class Point:
def __init__(self, x, y) -> None:
self.x = x
self.y = y
def __str__(self) -> str:
return f"({self.x}, {self.y})"
def stringify6(it: Iterator[Point]) -> Iterator[str]:
return (f"{pt}" for pt in it) So it seems like it's the unpacking syntax ( |
It looks like it doesn't have anything to do with the unpacking; it seems to pertain to the handling of def stringify(it: Iterator[int]) -> Iterator[str]:
return (f"asdf {x}" for x in it) According to GreenTreeSnakes' FormattedValue docs:
In this most recent example, using an fstring with tokens besides the formatted value produces a JoinedStr token. Maybe mypy handles JoinedStr correctly but not FormattedValue? |
Yes, it looks like the bug is that mypy interprets |
Fixes python#3385. The old code evaluated each FormattedValue as an expression potentially returning the type of the value, rather than a string. But that's wrong, because a FormattedValue can exist on its own when it's not being joined to any other format strings. The new code evaluates each FormattedValue by synthesizing '{}'.format(expr) and then, if necessary, joins them in JoinedStr using ''.join(items).
Fixes python#3385. The old code evaluated each FormattedValue as an expression potentially returning the type of the value, rather than a string. But that's wrong, because a FormattedValue can exist on its own when it's not being joined to any other format strings. The new code evaluates each FormattedValue by synthesizing '{}'.format(expr) and then, if necessary, joins them in JoinedStr using ''.join(items).
Fixes #3385. The old code evaluated each FormattedValue as an expression potentially returning the type of the value, rather than a string. But that's wrong, because a FormattedValue can exist on its own when it's not being joined to any other format strings. The new code evaluates each FormattedValue by synthesizing '{}'.format(expr) and then, if necessary, joins them in JoinedStr using ''.join(items).
mypy version: 0.511
python version: 3.6.1
Given this valid program:
I get errors for the first two variations:
It seems that mypy doesn't recognize that fstrings are produce a
str
. Interestingly, the yield error didn't show up in my old mypy version 0.501.The text was updated successfully, but these errors were encountered: