Skip to content

Wrong reported location for undefined references in f-strings #3562

@khyox

Description

@khyox

Repro:

"""mypy test: location for undefined references in f-strings"""

from typing import List

my_list: List[str] = ['Anna', 'Jonathan', 'Sandra', 'Joseph']
print(f'My list has {len(my_list)} elements.')

print('The list has %i elements.' % len(my_wrong_list))
print(f'The list has {len(another_wrong_list)} elements.')
print(f'The list has {missing_integer} elements.')
print('The list has {0:d} elements.'.format(len(yet_another_wrong_list)))

I get (mypy 0.520-dev):

> mypy --python-version 3.6 --show-column-numbers --check-untyped-defs test_mypy.py
test_mypy.py:1:5: error: Name 'another_wrong_list' is not defined
test_mypy.py:8:40: error: Name 'my_wrong_list' is not defined
test_mypy.py:10:6: error: Name 'missing_integer' is not defined
test_mypy.py:11:48: error: Name 'yet_another_wrong_list' is not defined

I'd expect:

mypy --python-version 3.6 --show-column-numbers --check-untyped-defs test_mypy.py
test_mypy.py:8:40: error: Name 'my_wrong_list' is not defined
test_mypy.py:9:26: error: Name 'another_wrong_list' is not defined
test_mypy.py:10:22: error: Name 'missing_integer' is not defined
test_mypy.py:11:48: error: Name 'yet_another_wrong_list' is not defined

So, for references in built-in functions in f-strings, both line and column are wrong (line is always reported as being the 1st, so these messages become the first ones reported by mypy) and for plain references in f-strings, just the reported column is wrong. Hope this helps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions