-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
The logic for checking the python version is moved to where the string interpolation occurs, so the input to check_str_interpolation is acceptable (based on ByteExpr in Python >= 3.5)
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 |
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.