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

NamedTuple Subclass Thinking It Returns a Tuple #9147

Closed
Py-mon opened this issue Jun 26, 2024 · 5 comments
Closed

NamedTuple Subclass Thinking It Returns a Tuple #9147

Py-mon opened this issue Jun 26, 2024 · 5 comments
Assignees
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@Py-mon
Copy link

Py-mon commented Jun 26, 2024

Environment data

  • Language Server version: v2024.6.1
  • OS and version: Windows 11
  • Python version (& distribution if applicable, e.g. Anaconda): 3.11

Code Snippet

from typing import NamedTuple


class Vector(NamedTuple("Vector", [("y", int), ("x", int)])):
    def __add__(self, vector: tuple[int, int]):
        cls = type(self)
        vector = super().__new__(cls, *vector)
        return type(self)(self.y + vector.y, self.x + vector.x)


print(Vector(1, 2) + (3, 1))

image

Expected behavior

When hovering over vector, it is the type Vector

Actual behavior

When hovering over vector, it is the type tuple[int, int]

Logs

2024-06-25 19:40:40.068 [info] Send text to terminal: �
2024-06-25 19:40:40.277 [info] Send text to terminal: cls
2024-06-25 19:40:40.277 [info] Send text to terminal:  c:; cd 'c:\Users\jacob\OneDrive\Documents\Programming\Python\Pyframe'; & 'c:\Users\jacob\AppData\Local\Programs\Python\Python311\python.exe' 'c:\Users\jacob\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '55472' '--' 'C:\Users\jacob\OneDrive\Documents\Programming\Python\Pyframe\rep.py' 
@debonte
Copy link
Collaborator

debonte commented Oct 3, 2024

@Py-mon, sorry for the long delay on this.

When hovering over vector, it is the type tuple[int, int]

This is by design, since you've annotated the vector parameter as tuple[int, int]. If I'm misunderstanding the issue here, please let me know.

def __add__(self, vector: tuple[int, int]):

@debonte debonte closed this as completed Oct 3, 2024
@Py-mon
Copy link
Author

Py-mon commented Oct 4, 2024

@debonte The type of vector changes.
vector = super().__new__(cls, *vector)

@debonte
Copy link
Collaborator

debonte commented Oct 4, 2024

I see. I agree that's weird. I'm not super familiar with NamedTuple, but it seems like Vector is a subtype of tuple[int, int] given that Pyright considers the assignment legal. So I'm not sure why the type of vector isn't narrowed there.

Transferring to Pyright.

@debonte debonte reopened this Oct 4, 2024
@debonte debonte transferred this issue from microsoft/pylance-release Oct 4, 2024
@erictraut erictraut added the bug Something isn't working label Oct 6, 2024
erictraut added a commit that referenced this issue Oct 6, 2024
…he narrowed type is `Self` or another bound type variable. This addresses #9147.
erictraut added a commit that referenced this issue Oct 6, 2024
…he narrowed type is `Self` or another bound type variable. This addresses #9147. (#9154)
@erictraut
Copy link
Collaborator

Yes, the type of vector should be narrowed on assignment in this case. Pyright wasn't handling this narrowing case correctly because the narrowed type is a type variable (Self) rather than a concrete type.

This will be fixed in the next release of pyright.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Oct 6, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.384

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants