MCPcopy Index your code
hub / github.com/python/mypy / check_ifstmt_for_overloads

Function check_ifstmt_for_overloads

mypy/nativeparse.py:1730–1769  ·  view source on GitHub ↗

Check if IfStmt contains only overloads with the same name. Return overload_name if found, None otherwise.

(
    stmt: IfStmt, current_overload_name: str | None = None
)

Source from the content-addressed store, hash-verified

1728
1729
1730def check_ifstmt_for_overloads(
1731 stmt: IfStmt, current_overload_name: str | None = None
1732) -> str | None:
1733 """Check if IfStmt contains only overloads with the same name.
1734 Return overload_name if found, None otherwise.
1735 """
1736 # Check that block only contains a single Decorator, FuncDef, or OverloadedFuncDef.
1737 # Multiple overloads have already been merged as OverloadedFuncDef.
1738 if not (
1739 len(stmt.body[0].body) == 1
1740 and (
1741 isinstance(stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
1742 or current_overload_name is not None
1743 and isinstance(stmt.body[0].body[0], FuncDef)
1744 )
1745 or len(stmt.body[0].body) > 1
1746 and isinstance(stmt.body[0].body[-1], OverloadedFuncDef)
1747 and all(is_stripped_if_stmt(if_stmt) for if_stmt in stmt.body[0].body[:-1])
1748 ):
1749 return None
1750
1751 overload_name = cast(Decorator | FuncDef | OverloadedFuncDef, stmt.body[0].body[-1]).name
1752 if stmt.else_body is None or stmt.else_body.is_unreachable:
1753 return overload_name
1754
1755 if len(stmt.else_body.body) == 1:
1756 # For elif: else_body contains an IfStmt itself -> do a recursive check.
1757 if (
1758 isinstance(stmt.else_body.body[0], (Decorator, FuncDef, OverloadedFuncDef))
1759 and stmt.else_body.body[0].name == overload_name
1760 ):
1761 return overload_name
1762 if (
1763 isinstance(stmt.else_body.body[0], IfStmt)
1764 and check_ifstmt_for_overloads(stmt.else_body.body[0], current_overload_name)
1765 == overload_name
1766 ):
1767 return overload_name
1768
1769 return None
1770
1771
1772def get_executable_if_block_with_overloads(

Callers 1

fix_function_overloadsFunction · 0.85

Calls 4

lenFunction · 0.85
isinstanceFunction · 0.85
allFunction · 0.85
is_stripped_if_stmtFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…