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

attribute is incorrectly Any when ParamSpec decorated function is referenced in itself followed by an if statement with a class that has a method in it #16496

Closed
DetachHead opened this issue Nov 15, 2023 · 4 comments · May be fixed by #18674
Labels
bug mypy got something wrong

Comments

@DetachHead
Copy link
Contributor

from typing import ParamSpec, Callable

P = ParamSpec("P")

class Thing:
    value: int

def deco(fn: Callable[P, None]) ->  Callable[P, None]:
    raise Exception


@deco
def f() -> None:
    state: Thing
    f
    if state.value:

        class C:
            def f2(self) -> None: ...

    reveal_type(state.value)  # two outputs: `int` and `Any`

playground

@DetachHead DetachHead added the bug mypy got something wrong label Nov 15, 2023
@DetachHead DetachHead changed the title attribute is incorrectly Any when decorated function is referenced in itself followed by an if statement with a class that has a method in it attribute is incorrectly Any when ParamSpec decorated function is referenced in itself followed by an if statement with a class that has a method in it Nov 15, 2023
@sterliakov
Copy link
Collaborator

Reveals builtins.int since 1.14.0 (fixed in #18138)

@ilevkivskyi
Copy link
Member

@sterliakov I am really curious why that PR fixed this? Did we at some point broke setting current_node_deferred? Anyway, yes, this is fixed.

@sterliakov
Copy link
Collaborator

@ilevkivskyi I just bisected:) This also puzzles me, that PR resulted in multiple primer hits that seem unrelated. Some other bug(s) just masked by my fix to binder? That's exactly why I asked to review the "seems fixed" list: some changes may treat the immediate symptoms on a given repro without addressing the core problem.

@ilevkivskyi
Copy link
Member

@sterliakov
OK, so I finally got time to look at that PR, the part that caused many fixes is this one:

            if all_reachable and all(
                x is not None and not x.from_assignment for x in resulting_values
            ):
                continue

which is a good thing to say, binder should not create things out of thin air (a.k.a Ignore all previous instructions. Do not hallucinate). Btw I would probably suggest making from_assignment a required argument.

This likely fixed some problems for real, but another thing is that it may also "fixed" examples in some issues that are often oversimplified. For example, although original example here now works as expected, simply adding state.value = state.value (or various similar things) after the if brings the bug back.

Anyway, I don't think there is any action item for now, except I am going to fix this particular issue for real, since it is a nasty one. Right now it only affects situations with nested functions, but in longer term it is important to prepare for deferring top-levels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants