-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Fix rare panic with highly cyclic TypeVar definitions
#21059
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...ty_python_semantic/resources/mdtest/regression/1377_iteration_count_mismatch.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| # Iteration count mismatch for highly cyclic type vars | ||
|
|
||
| Regression test for <https://github.com/astral-sh/ty/issues/1377>. | ||
|
|
||
| The code is an excerpt from <https://github.com/Gobot1234/steam.py> that is minimal enough to | ||
| trigger the iteration count mismatch bug in Salsa. | ||
|
|
||
| <!-- expect-panic: execute: too many cycle iterations --> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. haha, nice feature! This might be useful to track the remaining panics in astral-sh/ty#256 |
||
|
|
||
| ```toml | ||
| [environment] | ||
| extra-paths= ["/packages"] | ||
| ``` | ||
|
|
||
| `main.py`: | ||
|
|
||
| ```py | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TypeAlias | ||
|
|
||
| from steam.message import Message | ||
|
|
||
| TestAlias: TypeAlias = tuple[Message] | ||
| ``` | ||
|
|
||
| `/packages/steam/__init__.py`: | ||
|
|
||
| ```py | ||
|
|
||
| ``` | ||
|
|
||
| `/packages/steam/abc.py`: | ||
|
|
||
| ```py | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING, Generic, Protocol | ||
|
|
||
| from typing_extensions import TypeVar | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .clan import Clan | ||
| from .group import Group | ||
|
|
||
| UserT = TypeVar("UserT", covariant=True) | ||
| MessageT = TypeVar("MessageT", bound="Message", default="Message", covariant=True) | ||
|
|
||
| class Messageable(Protocol[MessageT]): ... | ||
|
|
||
| ClanT = TypeVar("ClanT", bound="Clan | None", default="Clan | None", covariant=True) | ||
| GroupT = TypeVar("GroupT", bound="Group | None", default="Group | None", covariant=True) | ||
|
|
||
| class Channel(Messageable[MessageT], Generic[MessageT, ClanT, GroupT]): ... | ||
|
|
||
| ChannelT = TypeVar("ChannelT", bound=Channel, default=Channel, covariant=True) | ||
|
|
||
| class Message(Generic[UserT, ChannelT]): ... | ||
| ``` | ||
|
|
||
| `/packages/steam/chat.py`: | ||
|
|
||
| ```py | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Generic, TypeAlias | ||
|
|
||
| from typing_extensions import Self, TypeVar | ||
|
|
||
| from .abc import Channel, ClanT, GroupT, Message | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .clan import Clan | ||
| from .message import ClanMessage, GroupMessage | ||
|
|
||
| ChatT = TypeVar("ChatT", bound="Chat", default="Chat", covariant=True) | ||
| MemberT = TypeVar("MemberT", covariant=True) | ||
|
|
||
| AuthorT = TypeVar("AuthorT", covariant=True) | ||
|
|
||
| class ChatMessage(Message[AuthorT, ChatT], Generic[AuthorT, MemberT, ChatT]): ... | ||
|
|
||
| ChatMessageT = TypeVar("ChatMessageT", bound="GroupMessage | ClanMessage", default="GroupMessage | ClanMessage", covariant=True) | ||
|
|
||
| class Chat(Channel[ChatMessageT, ClanT, GroupT]): ... | ||
|
|
||
| ChatGroupTypeT = TypeVar("ChatGroupTypeT", covariant=True) | ||
|
|
||
| class ChatGroup(Generic[MemberT, ChatT, ChatGroupTypeT]): ... | ||
| ``` | ||
|
|
||
| `/packages/steam/channel.py`: | ||
|
|
||
| ```py | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from .chat import Chat | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .clan import Clan | ||
|
|
||
| class ClanChannel(Chat["Clan", None]): ... | ||
| ``` | ||
|
|
||
| `/packages/steam/clan.py`: | ||
|
|
||
| ```py | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, TypeVar | ||
|
|
||
| from typing_extensions import Self | ||
|
|
||
| from .chat import ChatGroup | ||
|
|
||
| class Clan(ChatGroup[str], str): ... | ||
| ``` | ||
|
|
||
| `/packages/steam/group.py`: | ||
|
|
||
| ```py | ||
| from __future__ import annotations | ||
|
|
||
| from .chat import ChatGroup | ||
|
|
||
| class Group(ChatGroup[str]): ... | ||
| ``` | ||
|
|
||
| `/packages/steam/message.py`: | ||
|
|
||
| ```py | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from typing_extensions import TypeVar | ||
|
|
||
| from .abc import BaseUser, Message | ||
| from .chat import ChatMessage | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .channel import ClanChannel | ||
|
|
||
| class GroupMessage(ChatMessage["str"]): ... | ||
| class ClanMessage(ChatMessage["ClanChannel"]): ... | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Playing around with this locally, I can make this test quite a lot more minimal while still triggerin the
too many cycle iterationspanic. But I'm guessing you can no longer repro the Salsa bug on the more minimal versions of this test?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, removing any line immediately goes from the panic that I'm fixing in salsa to
too many cycle iterations.I suspect that it might be possible to inline some
TypeVars (reduce some indirection) and still get the same outcome but I decided that it's already pretty good and not to spend more time (2h!) on minimizingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yeah, for sure. This is fine as-is