Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1758,9 +1758,6 @@ def visit_try_stmt(self, s: TryStmt) -> Type:
self.binder.try_frames.add(len(self.binder.frames) - 2)
self.accept(s.body)
self.binder.try_frames.remove(len(self.binder.frames) - 2)
if s.else_body:
self.accept(s.else_body)
self.breaking_out = False
changed, frame_on_completion = self.binder.pop_frame()
completed_frames.append(frame_on_completion)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that self.breaking_out = False should be left here. I can try to come up with a test case, but if nothing breaks, I'd leave it.

Expand Down Expand Up @@ -1794,6 +1791,14 @@ def visit_try_stmt(self, s: TryStmt) -> Type:
changed, frame_on_completion = self.binder.pop_frame()
completed_frames.append(frame_on_completion)

# Do the else block similar to the way we do except blocks.
if s.else_body:
self.binder.push_frame()
self.accept(s.else_body)
self.breaking_out = False
changed, frame_on_completion = self.binder.pop_frame()
completed_frames.append(frame_on_completion)

self.binder.update_from_options(completed_frames)

if s.finally_body:
Expand Down
18 changes: 18 additions & 0 deletions mypy/test/data/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,24 @@ else:
object(None) # E: Too many arguments for "object"
[builtins fixtures/exception.py]

[case testRedefinedFunctionInTryWithElse]
def f() -> None: pass
try:
pass
except BaseException:
f2 = f
else:
def f2() -> str: pass
try:
pass
except BaseException:
f3 = f
else:
def f3() -> None: pass
[builtins fixtures/exception.py]
[out]
main:7: error: Incompatible redefinition (original type Callable[[], None], redefinition with type Callable[[], str])

[case testExceptWithoutType]
import typing
try:
Expand Down