Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
Propagate type narrowing to nested functions#15133
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
e4ecb9105e3958f3c87ca01e044deecc877030457e79688597603f24a81856ac503b3ca601098fe051319d0b478639a9a16dbf9f1d46324077b67df339ae6456c3f3a24faaa5ecc2d3467098aa25cafbd40f3b534bfb5f10c4dce44fe08c2dc6516ec323e06c350905File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1040,7 +1040,7 @@ x: Optional[List[int]] | ||
| if 3 in x: | ||
| pass | ||
| [casetestNarrowedVariableInNestedFunctionBasic] | ||
| from typing import Optional | ||
| def can_narrow(x: Optional[str]) -> None: | ||
| @@ -1069,3 +1069,34 @@ x: Optional[str] = "x" | ||
| def cannot_narrow_top_level() -> None: | ||
| ||
| reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" | ||
| [case testNarrowedVariableInNestedFunction2] | ||
| from typing import Optional | ||
| class C: | ||
| a: Optional[str] | ||
| def attribute_narrowing(c: C) -> None: | ||
| # This case is not supported at the moment. | ||
| c.a = "x" | ||
| def nested() -> str: | ||
| return c.a # E: Incompatible return value type (got "Optional[str]", expected "str") | ||
| nested() | ||
| def assignment_in_for(x: Optional[str]) -> None: | ||
| if x is None: | ||
| x = "e" | ||
| def nested() -> str: | ||
| return x # E: Incompatible return value type (got "Optional[str]", expected "str") | ||
| for x in ["x"]: | ||
| pass | ||
| def foo(): pass | ||
| def assignment_in_with(x: Optional[str]) -> None: | ||
| if x is None: | ||
| x = "e" | ||
| def nested() -> str: | ||
| return x # E: Incompatible return value type (got "Optional[str]", expected "str") | ||
| with foo() as x: | ||
| pass | ||