Skip to content

Wrong file blamed for a mixture of NamedTuple and passing a function instead of type. #8511

Closed

Description

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

  • Please insert below the code you are checking with mypy,

    • Create an empty dir/init.py.
    • Create dir/file1.py with:
import typing

def foo():
  pass


Type1 = typing.NamedTuple('Type1', [
    ('foo', foo),
])
  • Create file2.py (in the root dir) with:
from typing import NamedTuple

from dir.file1 import Type1

Type2 = NamedTuple('Type2', [
    ('x', Type1),
])
  • Run: mypy file2.py --follow-imports=silent

  • What is the actual behavior/output?

The error message is:
file2.py:8: error: Name 'foo' is not defined

-> Note that file2.py has no line 8 (it the problematic line in file1.py) and no mention of foo.

  • What is the behavior/output you expect?
    • the error message should point to file1.py or say something about Type1 in file2.py being invalid.
    • foo is well defined in this case, making the error message more cryptic.

When reducing this case I've found that removing --follow-import-silent gives an error message about file1.py which is actionable, but the other message is still present and is wrong, since the line number refers to a line in file1.py.

  • What are the versions of mypy and Python you are using?

mypy 0.761 on Python 3.5.2

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

--follow-imports=silent

Activity

  1. msullivan commented on Mar 16, 2020

    @msullivan
    Collaborator

    Oh this looks nasty. Anything that gives errors in the wrong file is really bad!

  2. added 2 commits that reference this issue on Mar 17, 2020
    9ce419f
    4affc8a
  3. mikolajz commented on Mar 17, 2020

    @mikolajz
    Author

    Yes, it was quite tricky to find that the error was because in a completely different file someone used numpy.array instead of numpy.ndarray.

  4. added a commit that references this issue on Mar 17, 2020
    7f8ea4a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions