Skip to content

Commit

Permalink
ci: update mypy config and fix some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed Jan 18, 2025
1 parent ee66b16 commit ad2b265
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
9 changes: 6 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ pretty = True
show_error_context = True
show_column_numbers = True
show_error_end = True
warn_redundant_casts = True
warn_unused_ignores = True

check_untyped_defs = True

# see https://mypy.readthedocs.io/en/stable/error_code_list2.html
warn_redundant_casts = True
strict_equality = True
enable_error_code = possibly-undefined
warn_unused_ignores = True
enable_error_code = deprecated,redundant-expr,possibly-undefined,truthy-bool,truthy-iterable,ignore-without-code,unused-awaitable

# an example of suppressing
# [mypy-my.config.repos.pdfannots.pdfannots]
Expand Down
2 changes: 1 addition & 1 deletion src/cachew/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ def cached_items():
new_hash: SourceHash = json.dumps(new_hash_d)
logger.debug(f'new hash: {new_hash}')

marshall = CachewMarshall(Type_=cls)
marshall: CachewMarshall[Any] = CachewMarshall(Type_=cls)

with BackendCls(cache_path=db_path, logger=logger) as backend:
old_hash = backend.get_old_hash()
Expand Down
11 changes: 6 additions & 5 deletions src/cachew/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def python_type(self):
def process_literal_param(self, value, dialect):
raise NotImplementedError() # make pylint happy

def process_result_value(self, value: Optional[str], dialect) -> Optional[date]: # type: ignore
def process_result_value(self, value: Optional[str], dialect) -> Optional[date]: # type: ignore[explicit-override,override]
res = super().process_result_value(value, dialect)
if res is None:
return None
Expand Down Expand Up @@ -246,6 +246,7 @@ def strip_optional(cls) -> tuple[type, bool]:

def strip_generic(tp):
"""
>>> from typing import List
>>> strip_generic(List[int])
<class 'list'>
>>> strip_generic(str)
Expand Down Expand Up @@ -345,7 +346,7 @@ def make(tp: type[NT], name: Optional[str] = None) -> 'NTBinder[NT]':
span = sum(f.span for f in fields) + (1 if optional else 0)
return NTBinder(
name=name,
type_=tp,
type_=tp, # type: ignore[arg-type]
span=span,
primitive=primitive,
optional=optional,
Expand Down Expand Up @@ -455,17 +456,17 @@ def flatten(self, level=0):
def test_mypy_annotations() -> None:
# mypy won't handle, so this has to be dynamic
vs = []
for t in Types.__args__: # type: ignore
for t in Types.__args__: # type: ignore[attr-defined]
(arg,) = t.__args__
vs.append(arg)

def types(ts):
return sorted(ts, key=lambda t: str(t))

assert types(vs) == types(Values.__args__) # type: ignore
assert types(vs) == types(Values.__args__) # type: ignore[attr-defined]

for p in PRIMITIVE_TYPES:
assert p in Values.__args__ # type: ignore
assert p in Values.__args__ # type: ignore[attr-defined]


@parametrize(
Expand Down
10 changes: 5 additions & 5 deletions src/cachew/tests/marshall.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ def union_hook(data, type_):
for i in range(count):
jsons[i] = to_json(objects[i])

strs: list[bytes] = [None for _ in range(count)] # type: ignore
strs: list[bytes] = [None for _ in range(count)] # type: ignore[misc]
with profile(test_name + ':json_dump'), timer(f'json dump {count} objects of type {Type}'):
for i in range(count):
# TODO any orjson options to speed up?
strs[i] = orjson.dumps(jsons[i]) # pylint: disable=no-member
strs[i] = orjson.dumps(jsons[i])

db = Path('/tmp/cachew_test/db.sqlite')
if db.parent.exists():
Expand All @@ -132,7 +132,7 @@ def union_hook(data, type_):
conn.executemany('INSERT INTO data (value) VALUES (?)', [(s,) for s in strs])
conn.close()

strs2: list[bytes] = [None for _ in range(count)] # type: ignore
strs2: list[bytes] = [None for _ in range(count)] # type: ignore[misc]
with profile(test_name + ':sqlite_load'), timer(f'sqlite load {count} objects of type {Type}'):
with sqlite3.connect(db) as conn:
i = 0
Expand All @@ -148,7 +148,7 @@ def union_hook(data, type_):
for s in strs:
fw.write(s + b'\n')

strs3: list[bytes] = [None for _ in range(count)] # type: ignore
strs3: list[bytes] = [None for _ in range(count)] # type: ignore[misc]
with profile(test_name + ':jsonl_load'), timer(f'jsonl load {count} objects of type {Type}'):
i = 0
with cache.open('rb') as fr:
Expand All @@ -163,7 +163,7 @@ def union_hook(data, type_):
with profile(test_name + ':json_load'), timer(f'json load {count} objects of type {Type}'):
for i in range(count):
# TODO any orjson options to speed up?
jsons2[i] = orjson.loads(strs2[i]) # pylint: disable=no-member
jsons2[i] = orjson.loads(strs2[i])

objects2 = [None for _ in range(count)]
with profile(test_name + ':deserialize'), timer(f'deserializing {count} objects of type {Type}'):
Expand Down
4 changes: 2 additions & 2 deletions src/cachew/tests/test_cachew.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def inner(_it, _timer{init}):
_t1 = _timer()
return _t1 - _t0, retval
"""
timeit.template = template # type: ignore
timeit.template = template # type: ignore[attr-defined]

timer = timeit.Timer(lambda: len(list(data())))
t, cnt = cast(tuple[float, int], timer.timeit(number=1))
Expand Down Expand Up @@ -973,7 +973,7 @@ def orig2():
settings.THROW_ON_ERROR = throw

with ctx:
fun = cachew(cache_path=lambda: 1 + 'bad_path_provider')(orig) # type: ignore
fun = cachew(cache_path=lambda: 1 + 'bad_path_provider')(orig) # type: ignore[arg-type,misc,operator]
assert list(fun()) == [123]
assert list(fun()) == [123]

Expand Down

0 comments on commit ad2b265

Please sign in to comment.