Thanks to visit codestin.com
Credit goes to github.com

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
Next Next commit
Avoid unnecessary work when checking deferred functions
  • Loading branch information
ilevkivskyi committed Feb 22, 2026
commit d473e25f7f9dcda6c3b8b788e78887394e197d6f
12 changes: 12 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3154,6 +3154,17 @@ def visit_block(self, b: Block) -> None:
self.binder.unreachable()
return
for s in b.body:
if (
self.current_node_deferred
# In these cases we need to continue checking to make sure all
# expressions are present in type map (even if they are just Any).
and not self.options.mypyc
and not self.options.export_types
):
# With current deferral logic there is no point continuing to
# type-check current function, as we will not infer more types,
# will not show errors, and each expression is inferred as Any.
return
if self.binder.is_unreachable():
if not self.should_report_unreachable_issues():
break
Expand Down Expand Up @@ -4713,6 +4724,7 @@ def check_simple_assignment(
and inferred is not None
and inferred.type is not None
and not inferred.is_final
and not self.current_node_deferred
):
new_inferred = remove_instance_last_known_values(rvalue_type)
if not is_same_type(inferred.type, new_inferred):
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-redefine2.test
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,21 @@ def f2() -> None:
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"

[case testNewRedefineNoWideningDeferred]
# flags: --allow-redefinition-new --local-partial-types
def deferred() -> None:
c: C
x = 1
if int():
x = c.x
reveal_type(x) # N: Revealed type is "builtins.int"

class C:
def __init__(self) -> None:
self.x = defer()

def defer() -> int: ...

[case testNewRedefineTryStatement]
# flags: --allow-redefinition-new --local-partial-types
class E(Exception): pass
Expand Down