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

Generic class is not imported as a generic class #6377

Closed
A5rocks opened this issue Nov 8, 2023 · 1 comment
Closed

Generic class is not imported as a generic class #6377

A5rocks opened this issue Nov 8, 2023 · 1 comment
Labels
as designed Not a bug, working as intended bug Something isn't working

Comments

@A5rocks
Copy link

A5rocks commented Nov 8, 2023

Describe the bug
I'm seeing inconsistent behavior between defining a generic class and importing it. (Or well, someone else did, I'm just pretty sure what's happening is a pyright bug. python-trio/trio#2873)

Code or Screenshots

Unfortunately the first few attempts I did to try to spit out something that I thought was the cause (inheriting from Tuple, implied generics) didn't yield this same error. So, here's something that depends on trio==0.23.1:

# pyright: strict
from trio._channel import MemorySendChannel, MemoryReceiveChannel, open_memory_channel
import typing

T = typing.TypeVar("T")

class omc(typing.Tuple[MemorySendChannel[T], MemoryReceiveChannel[T]]):
    def __new__(  # type: ignore[misc]  # "must return a subtype"
        cls, max_buffer_size: int | float
    ) -> tuple[MemorySendChannel[T], MemoryReceiveChannel[T]]:
        ...

    def __init__(self, max_buffer_size: int | float):
        ...

reveal_type(omc)
reveal_type(open_memory_channel)

Pyright, in response spits out:

C:\Users\A5rocks\Documents\trio\empty\pyright-repro.py
  C:\Users\A5rocks\Documents\trio\empty\pyright-repro.py:16:13 - information: Type of "omc" is "type[omc[Unknown]]"
  C:\Users\A5rocks\Documents\trio\empty\pyright-repro.py:17:13 - information: Type of "open_memory_channel" is "type[open_memory_channel]"
0 errors, 0 warnings, 2 informations

... Which seems wrong given omc is literally copy-pasted from open_memory_channel's definition, with Tuple -> typing.Tuple and replacing the body of __new__ with ....

VS Code extension or command-line
Installed via pip and pip show pyright reports version 1.1.334.

@A5rocks A5rocks added the bug Something isn't working label Nov 8, 2023
@erictraut
Copy link
Collaborator

Pyright is working correctly here, so I don't consider this a bug.

The problem is in the trio/_channel.py file. It declares a class called open_memory_channel. It derives from the type Tuple[MemorySendChannel[T], MemoryReceiveChannel[T]]. The problem is that MemorySendChannel and MemoryReceiveChannel are not defined at that point in the file, so they are evaluated as Unknown. The resulting class is therefore not considered generic. If you move the declaration of the open_memory_channel class later in the file (after MemorySendChannel and MemoryReceiveChannel have been defined, then it will work as you expect.

@erictraut erictraut added the as designed Not a bug, working as intended label Nov 8, 2023
@erictraut erictraut closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants