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

A corner-case where type cannot be inferred #9023

Closed
lkct opened this issue Sep 18, 2024 · 2 comments
Closed

A corner-case where type cannot be inferred #9023

lkct opened this issue Sep 18, 2024 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@lkct
Copy link

lkct commented Sep 18, 2024

(sorry I'm not sure how to write a more informative title for this case. feel free to modify :)

Describe the bug

As in the code below, the reveal_type should work in the self_list += ... line, but the actual behaviour is nothing for that line. Also, mouse hovering (in pyright playground) gives Unknown for both another_self and another_self.list_int; nevertheless, mouse hovering on type(another_self) correctly produces (variable) another_self: Self@Foo.

I have tried my best to minimise the code sample, and here are some points I want to note:

  • The try-block seems useless, but if you remove it, or even remove only the if flag1 sub-block, or only the continue statement, the behaviour would become normal.
  • Directly assigning self to another_self instead of popping it from the list (inferred as list[Self]) would also make it work.
  • Using either append[...] or extend([...]) works, only +=[...] fails.
  • Changing type(another_self) to type(self) also makes it work (even still using another_self inside reveal_type).

Code or Screenshots

Code sample in pyright playground

from typing import reveal_type

class Foo:
    list_int: list[int]

    def __init__(self, arg1: int):
        self.list_int = [arg1]

    def bar(self, flag1: bool):
        self_list = [self]

        while self_list:
            another_self = self_list.pop()

            try:
                pass
            except OSError:
                if flag1:
                    pass
                continue

            reveal_type(another_self.list_int)  # Type of "another_self.list_int" is "list[int]"
            self_list += [type(another_self)(reveal_type(another_self.list_int))]  # nothing
            reveal_type(another_self.list_int)  # Type of "another_self.list_int" is "list[int]"

VS Code extension or command-line
pyright latest (1.381) as provided by the playground

A bit difference on VS Code

There might be some strange interaction with my local environment, so this can possibly be ignored, in favour of the more stable environment provided by the playground. However, it's also possible that some different bugs exist.

My local env: VS Code 1.93.1 (38c31bc7), extension: Python v2024.14.1, Pylance v2024.9.1

The code above behaves slightly differently in VS Code. The reveal_type still yields nothing in the += line, but mouse hovering can give (variable) list_int: list[int]. With a bit more complication as below, I can still make hovering produce Unknown.

from typing import reveal_type


class Foo:
    list_int: list[int]

    def __init__(self, arg1: int):
        self.list_int = [arg1]

    def bar(self, flag1: bool):
        self_list = [self]

        while self_list:
            another_self = self_list.pop()

            try:
                pass
            except OSError:
                if flag1:
                    pass
                continue

            # --- Added the below ---
            some_list = [1]

            if flag1:
                yield another_self
            # --- Added the above ---

            reveal_type(another_self.list_int)
            self_list += [type(another_self)(reveal_type(another_self.list_int)) for _ in some_list]  # Modified this line
            reveal_type(another_self.list_int)
@lkct lkct added the bug Something isn't working label Sep 18, 2024
erictraut added a commit that referenced this issue Sep 18, 2024
… call to `type` is used within a loop. This addresses #9023.
erictraut added a commit that referenced this issue Sep 18, 2024
… call to `type` is used within a loop. This addresses #9023. (#9024)
@erictraut
Copy link
Collaborator

Thanks for the bug report and the great code sample. I was able to repro the problem and identify the root cause. This will be addressed in the next release.

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

This is addressed in pyright 1.1.382.

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

2 participants