Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Add support for comments containing Unicode #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
29 changes: 20 additions & 9 deletions flake8_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,26 @@ def run(self) -> Iterator[_Flake8Error]:
# Always put the file in a separate temporary directory to avoid
# unexpected clashes with other .py and .pyi files in the same original
# directory.
with TemporaryDirectory(prefix='flake8mypy_') as d:
with NamedTemporaryFile(
'w', encoding='utf8', prefix='tmpmypy_', suffix='.py', dir=d
) as f:
self.filename = f.name
for line in self.lines:
f.write(line)
f.flush()
yield from self._run()
try:
with TemporaryDirectory(prefix='flake8mypy_') as d:
with NamedTemporaryFile(
'wt', prefix='tmpmypy_', suffix='.py', dir=d
) as f:
self.filename = f.name
for line in self.lines:
f.write(line)
f.flush()
yield from self._run()
except UnicodeEncodeError:
with TemporaryDirectory(prefix='flake8mypy_') as d:
with NamedTemporaryFile(
'wb', prefix='tmpmypy_', suffix='.py', dir=d
) as f:
self.filename = f.name
for line in self.lines:
f.write(line.encode(r'utf8'))
Copy link
Owner

Choose a reason for hiding this comment

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

line.encode() here might fail exactly like the f.write() line above.

f.flush()
yield from self._run()

def _run(self) -> Iterator[_Flake8Error]:
mypy_cmdline = self.build_mypy_cmdline(self.filename, self.options.mypy_config)
Expand Down