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

Handle python 3.6 f-strings without error #2622

Merged
merged 17 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,11 @@ def visit_Str(self, n: ast35.Str) -> Union[UnicodeExpr, StrExpr]:
else:
return UnicodeExpr(n.s)

# JoinedStr(expr* values)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole method should be inside something like if hasattr(ast35, 'JoinedStr'): so that mypy will still work with the previous version of typed_ast.

@with_line
def visit_JoinedStr(self, n: ast35.JoinedStr) -> StrExpr:
return StrExpr(n.values[0])

# Bytes(bytes s)
@with_line
def visit_Bytes(self, n: ast35.Bytes) -> Union[BytesExpr, StrExpr]:
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,15 @@ b'%c' % (123)
[case testUnicodeInterpolation_python2]
u'%s' % (u'abc',)


-- F-String
-- --------


[case testFStringParseOk]
a = f'foobar'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need # flags: --fast-parser here (see how other tests use it) else you get a SyntaxError.

Can you also add tests showing that the expressions in {} are actually type-checked?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS. To quickly run just this test, try: pytest -n0 -k testFStringParseOk

Copy link
Contributor Author

@achauve achauve Dec 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip, better than python runtests.py testcheck that I was using.

I've just added another test to check type checking of expressions but I can't get it working.
If I put the content in a test_f_string.py file and run mypy --fast-parser --python-version 3.6 --show-traceback test_f_strings.py it runs without any error.

However running the test with pytest -n0 -k testFStringParseOk gives me:

___________________________________________________ testFStringTypecheckExpression ____________________________________________________
data: /Users/achauve/dev/github.com/achauve/mypy/test-data/unit/check-expressions.test:1169:
../../mypy/test/testcheck.py:113: in run_case
    self.run_case_once(testcase)
../../mypy/test/testcheck.py:190: in run_case_once
    assert_string_arrays_equal(output, a, msg.format(testcase.file, testcase.line))
../../mypy/test/helpers.py:85: in assert_string_arrays_equal
    raise AssertionFailure(msg)
E   mypy.myunit.AssertionFailure: Invalid type checker output (/Users/achauve/dev/github.com/achauve/mypy/test-data/unit/check-expressions.test, line 1169)
-------------------------------------------------------- Captured stderr call ---------------------------------------------------------
Expected:
Actual:
  main:3: error: Variable annotation syntax is only suppoted in Python 3.6, use type comment instead (diff)

It doesn't use python 3.6 syntax. How can I specify it in the tests?



-- Lambdas
-- -------

Expand Down