Skip to content

false positive "name not defined" with list comprehension in function arguments default value #9346

Open
@perrinjerome

Description

@perrinjerome
  • Are you reporting a bug, or opening a feature request?

This is a bug I believe

  • Please insert below the code you are checking with mypy
def a():
  pass

def b(
    bb=[list_comprehension_argument for list_comprehension_argument in "abc" 
          if list_comprehension_argument]
  ):
  pass
  • What is the actual behavior/output?
mypy repro.py
repro.py:5: error: Name 'list_comprehension_argument' is not defined
repro.py:6: error: Name 'list_comprehension_argument' is not defined
Found 2 errors in 1 file (checked 1 source file)
  • What is the behavior/output you expect?

I believe there should be no error reported.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

this is on mypy from git master, mypy 0.790+dev.2b704df25ca299e6936927be8d41d2ab945cee2a

  • What are the mypy flags you are using? (For example --strict-optional)

This is using the default flags.


This only seem to happens if there's another function like this one, before the function with list comprehension in arguments:

def a():
  pass

When removing this other function or putting it after the one with list comprehension in argument, this false positive is not reported, I mean no error are reported for:

def b(
    bb=[list_comprehension_argument for list_comprehension_argument in "abc" 
          if list_comprehension_argument]
  ):
  pass

def a():
  pass

or

def b(
    bb=[list_comprehension_argument for list_comprehension_argument in "abc" 
          if list_comprehension_argument]
  ):
  pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions