Use pyversion variable to change bytes checking for python 3 less than 3.5#2191
Use pyversion variable to change bytes checking for python 3 less than 3.5#2191gvanrossum merged 17 commits intopython:masterfrom
Conversation
gvanrossum
left a comment
There was a problem hiding this comment.
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!)
mypy/checker.py
Outdated
| Type check mypy source files that have been semantically analyzed. | ||
| """ | ||
|
|
||
| pyversion = None |
There was a problem hiding this comment.
Please add a type annotation here
mypy/checkexpr.py
Outdated
| This class works closely together with checker.TypeChecker. | ||
| """ | ||
|
|
||
| pyversion = None # type: Tuple[int, int] |
There was a problem hiding this comment.
And here you can use self.chk.options.
mypy/build.py
Outdated
| 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.
You shouldn't need this -- you can access the version already via self.options.python_version.
mypy/checkstrformat.py
Outdated
| chk: 'mypy.checker.TypeChecker', | ||
| msg: MessageBuilder) -> None: | ||
| msg: MessageBuilder, | ||
| pyversion: Tuple[int, int]) -> None: |
There was a problem hiding this comment.
You can just pull the version out of chk.options.
mypy/checkstrformat.py
Outdated
| 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.
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.