ambv / flake8-mypy

A plugin for flake8 integrating Mypy.
MIT License
101 stars 17 forks source link

Fails to Recognize "coding: utf-8-unix" #29

Open DevynCJohnson opened 5 years ago

DevynCJohnson commented 5 years ago

Flake8-MyPy fails to recognize Python scripts that use the magic comment containing coding: utf-8-unix as a UTF8 file. This bug has persisted for several versions and I had been fixing it myself ( https://github.com/ambv/flake8-mypy/pull/12). Although, my fix no longer works in the newest version of flake8-mypy. The fix has already been applied to MyPy itself ( https://github.com/python/mypy/pull/5085 ), but this very helpful Flake8 plugin still lacks the fix.

flake8 --version
3.7.7 (flake8-mypy: 17.8.0, flake8-pyi: 19.3.0, mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1, radon: 3.0.1, warn-symbols: 1.1.1) CPython 3.6.7 on Linux

mypy --version
mypy 0.701
DevynCJohnson commented 5 years ago

Even if I remove the magic comment, Flake8-MyPy will crash due to an encoding issue.

I found a solution that resolves the crash, but a solution is still needed to make Flake8-MyPy recognize various file-encoding like the other linters.

--- /home/collier/flake8_mypy.py    2019-05-12 08:54:38.430373728 -0500
+++ /usr/local/lib/python3.6/dist-packages/flake8_mypy.py   2019-05-12 08:47:43.138356450 -0500
@@ -185,11 +185,11 @@
         # directory.
         with TemporaryDirectory(prefix='flake8mypy_') as d:
             with NamedTemporaryFile(
-                'w', prefix='tmpmypy_', suffix='.py', dir=d
+                'wb', prefix='tmpmypy_', suffix='.py', dir=d
             ) as f:
                 self.filename = f.name
                 for line in self.lines:
-                    f.write(line)
+                    f.write(str(line).encode(r'utf-8'))
                 f.flush()
                 yield from self._run()