pyright icon indicating copy to clipboard operation
pyright copied to clipboard

TypeVar not assignable to itself in steam.py

Open Gobot1234 opened this issue 1 year ago • 2 comments

Describe the bug I think I've managed to get myself down to the last few type bugs in my library and the last one that's confusing me is a TypeVar that's not assignable to itself when joining types to the return types.

image but it is joinable when I use cast even though it should be for the same TypeVar. Unfortunately I can't figure out a minimal repro for it (sorry to keep sending these things I can't find minimal repros for).

Code

  1. git clone https://github.com/Gobot1234/steam.py
  2. poetry install
  3. open steam/chat.py scroll to a function involving MemberT

VS Code extension or command-line pyright 1.1.328

Gobot1234 avatar Sep 23 '23 12:09 Gobot1234

This is because you have a snarl of circular dependencies in your code.

  • The type MemberT has a bound of Member.
  • Member is parameterized by GroupT.
  • GroupT has a bound of Group | None.
  • Group uses GroupChannel as a type argument.
  • GroupChannel derives from Chat.
  • Chat derives from Channel which is parameterized with ChatMessageT.
  • ChatMessageT has a bound of GroupMessage | ClanMessage.
  • GroupMessage derives from ChatMessage.
  • ChatMessage is parameterized by MemberT. And we're in a cycle.

Pyright already handles many forms of circular dependencies, but it doesn't handle this one. I'll look for ways to extend the logic to handle this.

I noticed that you're importing a bunch of symbols under a TYPE_CHECKING conditional check. I'm guessing you did this to try to avoid runtime import circularities. That's just masking the fact that your code is not well layered. I recommend looking for ways to refactor your code so you can eliminate these TYPE_CHECKING conditions and eliminate circular dependencies. Consider enabling the reportImportCycles check and eliminating the errors it reports.

erictraut avatar Sep 24 '23 05:09 erictraut

I'll see if I can get rid of them going forwards but I feel like there's not much I can do bar put everything in one massive file

Gobot1234 avatar Sep 24 '23 16:09 Gobot1234

I'm going to close this as "won't fix". Try to eliminate the circular dependencies within your code.

erictraut avatar May 21 '24 07:05 erictraut