Skip to content
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

Sort blocks by line number of the first opcode #1786

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions pytype/blocks/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def __getitem__(self, index_or_slice):
def __iter__(self):
return self.code.__iter__()

def __lt__(self, other: Self) -> bool:
return (self.code[0].line, self.id) < (other.code[0].line, other.id)

def __gt__(self, other: Self) -> bool:
return (self.code[0].line, self.id) > (other.code[0].line, other.id)


class OrderedCode:
"""Code object which knows about instruction ordering.
Expand Down
14 changes: 14 additions & 0 deletions pytype/tests/test_flow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ def f() -> int:
f()
""")

def test_try_in_loop(self):
self.Check("""
def may_fail():
pass

while True:
err = None
try:
may_fail()
except ValueError as e:
err = str(e)
assert_type(err, str|None)
""")


if __name__ == "__main__":
test_base.main()
17 changes: 11 additions & 6 deletions pytype/typegraph/cfg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ def compute_predecessors(


class OrderableNode(PredecessorNode, Protocol):
id: int

def __lt__(self, other: "OrderableNode", /) -> bool:
...

def __gt__(self, other: "OrderableNode", /) -> bool:
...


_OrderableNode = TypeVar("_OrderableNode", bound=OrderableNode)
Expand All @@ -287,8 +292,9 @@ def order_nodes(nodes: Sequence[_OrderableNode]) -> list[_OrderableNode]:
process both the branches before that node).

Args:
nodes: A list of nodes or blocks. They have two attributes: "id" (an int to
enable deterministic sorting) and "outgoing" (a list of nodes).
nodes: A list of nodes or blocks. They have an attribute `outgoing` (a list
of nodes) and define rich comparisons method `__lt__` and `__gt__` to
enable deterministic sorting.

Returns:
A list of nodes in the proper order.
Expand All @@ -308,9 +314,8 @@ def order_nodes(nodes: Sequence[_OrderableNode]) -> list[_OrderableNode]:
while queue:
# Find node with minimum amount of predecessors that's connected to a node
# we already processed.
_, _, node = min(
(len(predecessors), node.id, node)
for node, predecessors in queue.items()
_, node = min(
(len(predecessors), node) for node, predecessors in queue.items()
)
del queue[node]
if node in seen:
Expand Down
Loading