Skip to content

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

Closed

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.

Activity

  1. emmatyping commented on Jun 16, 2017

    @emmatyping
    Member

    @ambv you were working on something related to this, no? It was a CPython issue as I recall? Or was that a different issue related to f-strings?

  2. ambv commented on Jun 16, 2017

    @ambv
    Contributor

    Yes, this is bpo-30465 and the fix is in python/cpython#1800 but I won't have time before July to finish it.

  3. FichteFoll commented on Sep 15, 2017

    @FichteFoll

    The referenced patch/PR has been merged into master and the 3.6 branch, so hopefully this will be resolved with the next CPython release.

  4. emmatyping commented on Sep 15, 2017

    @emmatyping
    Member

    The actual fix for mypy will happen when typed_ast is updated, which should be when the next Python version is released, but may be sooner.

  5. emmatyping commented on Sep 15, 2017

    @emmatyping
    Member

    Also, this is an issue with typed_ast, so I am closing this.

  6. gvanrossum commented on Sep 15, 2017

    @gvanrossum
    Member

    If it's fixed in master (or 3.6) upstream it can be fixed in typed_ast, there's no need to wait for a CPython release. But there needs to be a separate PR for typed_ast (it doesn't automatically get copied from CPython nor does it somehow invoke the CPython version).

    I opened an issue against typed_ast (python/typed_ast#56) to copy the fix there (but I'm not personally going to do the busy-work of creating the PR).

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions