Skip to content

Walrus operators unrecognised in dictionary initialisation of a class attribute #8379

Open
@masasakano

Description

@masasakano

It seems Walrus operators (in Python 3.8+) are not recognised by mypy when used in initialising a dictionary of a class attribute. Here is a test code.

class TryWalrus:
    CKEY = { 
        'KY': (p:='a_'),
         'ANOTHER': p+'content',
        }

print(TryWalrus.CKEY['ANOTHER']) # => a_content

This outputs the following:

$ mypy try_walrus.py
try_walrus.py:4: error: Name 'p' is not defined
Found 1 error in 1 file (checked 1 source file)

If the variable CKEY is out of the class definition, i.e. if directly in Main, it does not raise an error.

I tested it with Python 3.8.1 and mypy version 0.770+dev.8888b1aedf6d4ba543ae12f538f190ce5e65c20d (yesterday's head).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-572PEP 572 (walrus operator)topic-runtime-semanticsmypy doesn't model runtime semantics correctly

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions