Skip to content

Use pyversion variable to change bytes checking for python 3 less than 3.5 #2191

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

Merged
merged 17 commits into from
Oct 5, 2016

Conversation

buckbaskin
Copy link
Contributor

This is based on a TODO from pull request #2168 to fix the behavior for checking bytes in Python 3 when the minor version is less than 3.5. Per #2168, bytes formatting in Python 3 is only supported in 3.5 and up.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Sorry to have led you down the garden path, I think there's a much simpler approach. (But now you know your way around more of the code!)

@@ -79,6 +79,7 @@ class TypeChecker(NodeVisitor[Type]):
Type check mypy source files that have been semantically analyzed.
"""

pyversion = None
Copy link
Member

Choose a reason for hiding this comment

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

Please add a type annotation here

@@ -88,6 +88,7 @@ class ExpressionChecker:
This class works closely together with checker.TypeChecker.
"""

pyversion = None # type: Tuple[int, int]
Copy link
Member

@gvanrossum gvanrossum Sep 28, 2016

Choose a reason for hiding this comment

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

And here you can use self.chk.options.

@@ -353,7 +353,7 @@ def __init__(self, data_dir: str,
self.semantic_analyzer = SemanticAnalyzer(lib_path, self.errors)
self.modules = self.semantic_analyzer.modules
self.semantic_analyzer_pass3 = ThirdPass(self.modules, self.errors)
self.type_checker = TypeChecker(self.errors, self.modules)
self.type_checker = TypeChecker(self.errors, self.modules, options.python_version)
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't need this -- you can access the version already via self.options.python_version.

@@ -49,21 +50,24 @@ class StringFormatterChecker:
def __init__(self,
exprchk: 'mypy.checkexpr.ExpressionChecker',
chk: 'mypy.checker.TypeChecker',
msg: MessageBuilder) -> None:
msg: MessageBuilder,
pyversion: Tuple[int, int]) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

You can just pull the version out of chk.options.

def check_str_interpolation(self,
str: Union[StrExpr, BytesExpr, UnicodeExpr],
replacements: Node) -> Type:
"""Check the types of the 'replacements' in a string interpolation
expression: str % replacements
"""
if isinstance(str, BytesExpr) and self.pyversion[0] == 3 and self.pyversion[1] < 5:
assert False
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is the right approach. It's probably better to move the version check to whatever calls check_str_interpolation(), so that it doesn't even get called for bytes if pyversion < 3.5, and then the fallback logic in the caller (which just maps '%' to a call to __mod__) will take over.

@gvanrossum gvanrossum changed the title Use pyversion variable to change bytes checking for python 3 less than 3.5 [needs changes] Use pyversion variable to change bytes checking for python 3 less than 3.5 Sep 29, 2016
@gvanrossum gvanrossum merged commit 30bd4c8 into python:master Oct 5, 2016
@gvanrossum
Copy link
Member

gvanrossum commented Oct 5, 2016

So I forgot to remove the "[needs changes]" and ended up amending the commit locally and doing a "git push -f". Hopefully this won't inconvenience too many people... New commit is 7e2b3b4

@gvanrossum gvanrossum changed the title [needs changes] Use pyversion variable to change bytes checking for python 3 less than 3.5 Use pyversion variable to change bytes checking for python 3 less than 3.5 Oct 5, 2016
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

Successfully merging this pull request may close these issues.

2 participants