Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 2, 2025

TIFA (Type Inference and Flow Analysis) was not correctly handling try/except/finally constructs, leading to missed variable initialization issues. The visitor used the default generic_visit method which doesn't properly track control flow through exception handling blocks.

Problem

Consider this code:

try:
    x = 1
except:
    pass  # x not defined in this branch
print(x)  # Should be flagged as potentially uninitialized

Previously, TIFA would not detect that x might be uninitialized because it wasn't analyzing the different execution paths through try/except blocks.

Solution

Added proper AST visitor methods for try/except/finally constructs:

  • visit_Try: Handles the main try statement by creating separate execution paths for:

    • Try body (including else clause if no exception occurs)
    • Each exception handler
    • Finally clause (always executes)
  • visit_except_handler: Handles individual except clauses including:

    • Exception type checking
    • Exception binding (except Exception as e:)
    • Exception handler body execution

The implementation properly merges all possible execution paths to ensure variable initialization is tracked across all branches.

Key Features

  • ✅ Detects variables that may be uninitialized in some execution paths
  • ✅ Handles exception binding with as clause
  • ✅ Supports else and finally clauses
  • ✅ Works with nested try/except constructs
  • ✅ Maintains compatibility with existing TIFA functionality

Testing

Added comprehensive test cases covering:

  • Variables defined in both try and except blocks
  • Variables defined only in try block
  • Variables defined only in except block
  • Finally clause variable definitions
  • Exception binding scenarios

All tests pass and no regressions were introduced (526 total tests, same 16 expected failures as baseline).

Fixes #157.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Try/Except in TIFA Implement proper try/except/finally handling in TIFA visitor Sep 2, 2025
Copilot AI requested a review from acbart September 2, 2025 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Try/Except in TIFA

2 participants