-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Allow redefinitions in except/else/finally #18515
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
Merged
sobolevn
merged 3 commits into
python:master
from
sterliakov:bugfix/st-allow-redefinition-try-clauses
Jan 26, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,7 @@ def h(a: Iterable[int]) -> None: | |
|
|
||
| [case testCannotRedefineLocalWithinTry] | ||
| # flags: --allow-redefinition | ||
| def g(): pass | ||
| def f() -> None: | ||
| try: | ||
| x = 0 | ||
|
|
@@ -102,7 +103,52 @@ def f() -> None: | |
| y | ||
| y = '' | ||
|
|
||
| def g(): pass | ||
| [case testRedefineLocalWithinTryClauses] | ||
| # flags: --allow-redefinition | ||
| def fn_str(_: str) -> int: ... | ||
| def fn_int(_: int) -> None: ... | ||
|
|
||
| def in_block() -> None: | ||
| try: | ||
| a = "" | ||
| a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
| fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int" | ||
| except: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please, also test |
||
| b = "" | ||
| b = fn_str(b) | ||
| fn_int(b) | ||
| else: | ||
| c = "" | ||
| c = fn_str(c) | ||
| fn_int(c) | ||
| finally: | ||
| d = "" | ||
| d = fn_str(d) | ||
| fn_int(d) | ||
| reveal_type(a) # N: Revealed type is "builtins.str" | ||
| reveal_type(b) # N: Revealed type is "builtins.int" | ||
| reveal_type(c) # N: Revealed type is "builtins.int" | ||
| reveal_type(d) # N: Revealed type is "builtins.int" | ||
|
|
||
| def across_blocks() -> None: | ||
| try: | ||
| a = "" | ||
| except: | ||
| pass | ||
| else: | ||
| a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
| reveal_type(a) # N: Revealed type is "builtins.str" | ||
|
|
||
| [case testRedefineLocalExceptVar] | ||
| # flags: --allow-redefinition | ||
| def fn_exc(_: Exception) -> str: ... | ||
|
|
||
| def exc_name() -> None: | ||
| try: | ||
| pass | ||
| except RuntimeError as e: | ||
| e = fn_exc(e) | ||
| [builtins fixtures/exception.pyi] | ||
|
|
||
| [case testRedefineLocalWithinWith] | ||
| # flags: --allow-redefinition | ||
|
|
@@ -274,7 +320,6 @@ def f() -> None: | |
| # E: Incompatible types in assignment (expression has type "int", variable has type "TypeVar") | ||
| reveal_type(x) # N: Revealed type is "typing.TypeVar" | ||
| y = 1 | ||
| # NOTE: '"int" not callable' is due to test stubs | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This note no longer applies, necessary stubs were connected in #17384. |
||
| y = TypeVar('y') # E: Cannot redefine "y" as a type variable \ | ||
| # E: Incompatible types in assignment (expression has type "TypeVar", variable has type "int") | ||
| def h(a: y) -> y: return a # E: Variable "y" is not valid as a type \ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.