Skip to content

Walrus operator + type inference + generic class + some other conditions = mypy failure #9743

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

Closed
dnaaun opened this issue Nov 22, 2020 · 4 comments
Labels
bug mypy got something wrong topic-pep-572 PEP 572 (walrus operator)

Comments

@dnaaun
Copy link

dnaaun commented Nov 22, 2020

Bug Report

from typing import (
    Generic,
    TypeVar,
)
_MyType = TypeVar("_MyType", str, float) 

class MyGeneric(Generic[_MyType]):
    def __init__(self, attr: int) -> None:
        ls = [0]
        self.attr = attr          # mypy says: error: Need type annotation for 'attr'
        if (a := sum(ls)): # mypy says: error: Cannot determine type of 'ls'
            pass
        reveal_type(ls)        # mypy says: note: Revealed type is 'builtins.list[builtins.int*]'

The title of this bug report was a bit vague because I didn't know how to concisely describe the necessary set of conditions for this bug. Here are those conditions:

  1. ls is not manually annotated.
  2. The walrus operator is used.
  3. The block is inside a generic class.
  4. _MyType is a TypeVar that explicitly enumerates all the possible types (as opposed to being bound=... or just a free TypeVar)

Environment
mypy 0.800+dev.40fd841a005936d95e7c351435f1b7b7b4bdd43d
No mypy.ini config, no cmdline flags.
Python 3.8.3

@dnaaun dnaaun added the bug mypy got something wrong label Nov 22, 2020
@GBeauregard
Copy link

GBeauregard commented Oct 2, 2021

I believe I've ran into this same bug when converting one of my classes to a generic class as part of a rewrite. Walrus operators + generic classes seem to combine to break mypy somehow. You can find information on gitter here: https://gitter.im/python/typing?at=6158b2c638377967f42dba74

Here is the failing example:
https://gist.github.com/GBeauregard/9edc42fb7e855d65ea89c756ccd82665

I ran into this when converting the class from using str to being a generic class of AnyStr. I've left it as str for now before I decide what to do when rewriting the class.

@GBeauregard
Copy link

I can confirm this still affects the latest git tag mypy 0.920+dev.0a830481980bfc554ded61a3eaaaecde384a21e4.

@hauntsaninja hauntsaninja added the topic-pep-572 PEP 572 (walrus operator) label Oct 3, 2021
GBeauregard added a commit to GBeauregard/pyffstream that referenced this issue Oct 5, 2021
This allows supporting all IO types. This was much more complicated than
it seems since this transition ran into an extremely odd bug in mypy.
The gist is that explicitly bound generic classes and walruses operators
break mypy somehow. See python/mypy#9743
@erictraut
Copy link

This bug appears to have been fixed. I cannot repro it with the latest version of mypy.

@hauntsaninja
Copy link
Collaborator

Fixed in #14135

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-572 PEP 572 (walrus operator)
Projects
None yet
Development

No branches or pull requests

4 participants