Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
khyox opened this issue Jun 16, 2017 · 6 comments
Closed

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

khyox opened this issue Jun 16, 2017 · 6 comments

Comments

@khyox
Copy link

khyox commented Jun 16, 2017

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.

@ethanhs
Copy link
Collaborator

ethanhs commented Jun 16, 2017

@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?

@ambv
Copy link
Contributor

ambv commented Jun 16, 2017

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

@FichteFoll
Copy link

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.

@ethanhs
Copy link
Collaborator

ethanhs commented Sep 15, 2017

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.

@ethanhs
Copy link
Collaborator

ethanhs commented Sep 15, 2017

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

@gvanrossum
Copy link
Member

gvanrossum commented Sep 15, 2017

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants