Skip to content

Commit a81d373

Browse files
committed
Fix various linting errors
1 parent 4514593 commit a81d373

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

delphin/ace.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ def _make_response(lines, run) -> Tuple[interface.Response, List[str]]:
668668
return response, content_lines
669669

670670

671-
def _sexpr_data(line: str) -> Iterator[Dict[str, Any]]:
671+
def _sexpr_data(line: str) -> Iterator[Tuple[str, Any]]:
672672
while line:
673673
try:
674674
expr = util.SExpr.parse(line)
@@ -679,8 +679,12 @@ def _sexpr_data(line: str) -> Iterator[Dict[str, Any]]:
679679
if len(expr.data) != 2:
680680
logger.error('Malformed output from ACE: %s', line)
681681
break
682+
683+
key, val = expr.data
684+
assert isinstance(key, str)
685+
yield key, val
686+
682687
line = expr.remainder.lstrip()
683-
yield expr.data
684688

685689

686690
def _tsdb_response(response: interface.Response,

delphin/tsdb.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import re
1212
from pathlib import Path
1313
from collections import OrderedDict
14-
from gzip import open as gzopen
14+
from gzip import open as gzopen, GzipFile
1515
import tempfile
1616
import shutil
1717
from datetime import datetime, date
@@ -740,9 +740,6 @@ def _get_paths(dir: util.PathLike, name: str) -> Tuple[Path, Path, bool]:
740740
return tx_path, gz_path, use_gz
741741

742742

743-
# Note: the return type should have TextIO instead of IO[str], but
744-
# there's a bug in the type checker. Replace when mypy no longer
745-
# complains about TextIO.
746743
def open(dir: util.PathLike,
747744
name: str,
748745
encoding: Optional[str] = None) -> IO[str]:
@@ -865,8 +862,8 @@ def write(dir: util.PathLike,
865862
# now copy the temp file to the destination
866863
f_tmp.seek(0)
867864
if gzip:
868-
with gzopen(dest, mode) as f_out:
869-
shutil.copyfileobj(f_tmp, f_out)
865+
with GzipFile(dest, mode=mode) as gz_out:
866+
shutil.copyfileobj(f_tmp, gz_out)
870867
else:
871868
with dest.open(mode=mode) as f_out:
872869
shutil.copyfileobj(f_tmp, f_out)

delphin/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
from typing import (
7+
Any,
78
Union,
89
Iterable,
910
Iterator,
@@ -265,12 +266,17 @@ def _vf2_candidates(
265266
# S-expressions
266267
# e.g. (:n-inputs . 3) or (S (NP (NNS Dogs)) (VP (VBZ bark)))
267268

268-
_Val = Union[str, int, float]
269+
_Atom = Union[str, int, float]
270+
_SExpr = Union[_Atom, '_Cons']
271+
# The following would be nice, but Mypy doesn't do recursive types yet:
272+
# https://github.com/python/mypy/issues/731
273+
# _Cons = Union[Tuple[_SExpr, _SExpr], List[_SExpr]]
274+
_Cons = Union[Tuple[Any, Any], List[Any]]
269275

270276

271277
class SExprResult(NamedTuple):
272278
"""The result of parsing an S-Expression."""
273-
data: Union[Tuple[_Val, _Val], List[_Val]]
279+
data: _Cons
274280
remainder: str
275281

276282

tests/util_test.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# coding: utf-8
22

3+
import codecs
4+
35
from delphin.util import safe_int, SExpr, detect_encoding, LookaheadIterator
46

5-
import pytest, codecs
7+
import pytest
8+
69

710
def test_safe_int():
811
assert safe_int('1') == 1
@@ -58,6 +61,7 @@ def test_SExpr_format():
5861
# assert unescape_string('\\xe3\\x81\\x82') == 'あ'
5962
# assert unescape_string('\\N{HIRAGANA LETTER A}') == 'あ'
6063

64+
6165
@pytest.fixture
6266
def empty_file(tmp_path):
6367
f = tmp_path / 'empty.txt'
@@ -116,7 +120,7 @@ def shiftjis_file(tmp_path):
116120
def eucjp_file(tmp_path):
117121
f = tmp_path / 'eucjp.txt'
118122
f.write_text(u'; coding: euc_jp\n'
119-
u'a=\"\"', encoding = 'euc_jp')
123+
u'a=\"\"', encoding='euc_jp')
120124
return str(f)
121125

122126

@@ -142,9 +146,11 @@ def invalid3_file(tmp_path):
142146
return str(f)
143147

144148

145-
def test_detect_encoding(empty_file, nocomment_file, utf8_file, utf8var1_file,
149+
def test_detect_encoding(
150+
empty_file, nocomment_file, utf8_file, utf8var1_file,
146151
utf8var2_file, shiftjis_file, eucjp_file, latin1_file, invalid1_file,
147-
invalid2_file, invalid3_file):
152+
invalid2_file, invalid3_file
153+
):
148154
assert detect_encoding(empty_file) == 'utf-8'
149155
assert detect_encoding(nocomment_file) == 'utf-8'
150156
assert detect_encoding(utf8_file) == 'utf-8'

0 commit comments

Comments
 (0)